Quantcast
Channel: npoi Discussions Rss Feed
Viewing all articles
Browse latest Browse all 637

New Post: Adding more than one chart to worksheet results in corrupt .XLSX file

$
0
0
I am trying to create an Excel worksheet containing multiple charts. However, when I add a second chart the resulting .XLSX file is corrupt when I try opening it.

Here is a code sample that shows the issue (modified from the LineChart example):
 static void Main(string[] args)
        {
            int NUM_OF_CHARTS = 2;

            IWorkbook wb = new XSSFWorkbook();
            ISheet sheet = wb.CreateSheet("linechart");

            IDrawing drawing = sheet.CreateDrawingPatriarch();

            for (int i = 0; i < NUM_OF_CHARTS; i++)
            {
                CreateChart(sheet, drawing, i);
            }

            using (FileStream fs = File.Create(String.Format("test-{0} charts-{1}.xlsx", NUM_OF_CHARTS, DateTime.Now.Ticks)))
            {
                wb.Write(fs);
            }
        }

        static IChart CreateChart(ISheet sheet, IDrawing drawing, int factor)
        {
            int NUM_OF_ROWS = 3;
            int NUM_OF_COLUMNS = 10;

            Random r = new Random((int)DateTime.Now.Ticks);

            System.Threading.Thread.Sleep(r.Next(50, 100));

            // Create a row and put some cells in it. Rows are 0 based.
            IRow row;
            ICell cell;
            for (int rowIndex = 0 + (NUM_OF_ROWS * factor); rowIndex < NUM_OF_ROWS * (1 + factor); rowIndex++)
            {
                row = sheet.CreateRow((short)rowIndex);
                for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++)
                {
                    cell = row.CreateCell((short)colIndex);
                    if (rowIndex == 0 + NUM_OF_ROWS * factor)
                    {
                        cell.SetCellValue(colIndex);
                    }
                    else
                    {
                        cell.SetCellValue(r.Next(0,100));
                    }
                }
            }

            IClientAnchor anchor = drawing.CreateAnchor(0, 0, 0, 0, 10, 0 + (10 * factor), 20, 10 + (10 * factor));

            IChart chart = drawing.CreateChart(anchor);
            ILineChartData<double, double> data = chart.GetChartDataFactory().CreateLineChartData<double, double>();

            // Use a category axis for the bottom axis.
            IChartAxis bottomAxis = chart.GetChartAxisFactory().CreateCategoryAxis(AxisPosition.Bottom);
            IValueAxis leftAxis = chart.GetChartAxisFactory().CreateValueAxis(AxisPosition.Left);

            IChartDataSource<double> xs = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0 + factor * NUM_OF_ROWS, 0 + factor * NUM_OF_ROWS, 0, NUM_OF_COLUMNS - 1));
            IChartDataSource<double> ys1 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(1 + factor * NUM_OF_ROWS, 1 + factor * NUM_OF_ROWS, 0, NUM_OF_COLUMNS - 1));
            IChartDataSource<double> ys2 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(2 + factor * NUM_OF_ROWS, 2 + factor * NUM_OF_ROWS, 0, NUM_OF_COLUMNS - 1));

            var s1 = data.AddSerie(xs, ys1);
            s1.SetTitle("title1");
            var s2 = data.AddSerie(xs, ys2);
            s2.SetTitle("title2");

            chart.Plot(data, bottomAxis, leftAxis);

            return chart;
        }
Any ideas?

Viewing all articles
Browse latest Browse all 637

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>