Beispiel #1
0
        static void CreateChart(IDrawing drawing, ISheet sheet, IClientAnchor anchor, string serie1, string serie2)
        {
            IChart       chart  = drawing.CreateChart(anchor);
            IChartLegend legend = chart.GetOrCreateLegend();

            legend.Position = LegendPosition.TopRight;

            ILineChartData <double, double> data = chart.ChartDataFactory.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);

            leftAxis.SetCrosses(AxisCrosses.AutoZero);

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

            var s1 = data.AddSeries(xs, ys1);

            s1.SetTitle(serie1);
            var s2 = data.AddSeries(xs, ys2);

            s2.SetTitle(serie2);

            chart.Plot(data, bottomAxis, leftAxis);
        }
Beispiel #2
0
        private static void CreateChart(ISheet sheet, IDrawing drawing, IClientAnchor anchor, string serieTitle, int startDataRow, int endDataRow, int columnIndex)
        {
            XSSFChart chart = (XSSFChart)drawing.CreateChart(anchor);

            IBarChartData <string, double> barChartData = chart.ChartDataFactory.CreateBarChartData <string, double>();
            IChartLegend legend = chart.GetOrCreateLegend();

            legend.Position = LegendPosition.Bottom;

            IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom);

            bottomAxis.MajorTickMark = AxisTickMark.None;
            IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);

            leftAxis.Crosses = AxisCrosses.AutoZero;
            leftAxis.SetCrossBetween(AxisCrossBetween.Between);


            IChartDataSource <string> categoryAxis = DataSources.FromStringCellRange(sheet, new CellRangeAddress(startDataRow, endDataRow, 0, 0));
            IChartDataSource <double> valueAxis    = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(startDataRow, endDataRow, columnIndex, columnIndex));
            var serie = barChartData.AddSeries(categoryAxis, valueAxis);

            serie.SetTitle(serieTitle);

            chart.Plot(barChartData, bottomAxis, leftAxis);
        }
Beispiel #3
0
        public void TestOneSeriePlot()
        {
            IWorkbook     wb      = new XSSFWorkbook();
            ISheet        sheet   = new SheetBuilder(wb, plotData).Build();
            IDrawing      Drawing = sheet.CreateDrawingPatriarch();
            IClientAnchor anchor  = Drawing.CreateAnchor(0, 0, 0, 0, 1, 1, 10, 30);
            IChart        chart   = Drawing.CreateChart(anchor);

            IChartAxis bottomAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Bottom);
            IChartAxis leftAxis   = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);

            IScatterChartData <string, double> scatterChartData =
                chart.ChartDataFactory.CreateScatterChartData <string, double>();

            IChartDataSource <String>            xs     = DataSources.FromStringCellRange(sheet, CellRangeAddress.ValueOf("A1:J1"));
            IChartDataSource <double>            ys     = DataSources.FromNumericCellRange(sheet, CellRangeAddress.ValueOf("A2:J2"));
            IScatterChartSeries <string, double> series = scatterChartData.AddSeries(xs, ys);

            Assert.IsNotNull(series);

            Assert.AreEqual(1, scatterChartData.GetSeries().Count);
            Assert.IsTrue(scatterChartData.GetSeries().Contains(series));

            chart.Plot(scatterChartData, bottomAxis, leftAxis);
        }
Beispiel #4
0
        public void TestIobExceptionOnInvalidIndex()
        {
            IWorkbook                 wb            = new HSSFWorkbook();
            ISheet                    sheet         = new SheetBuilder(wb, numericCells).Build();
            CellRangeAddress          rangeAddress  = CellRangeAddress.ValueOf("A2:E2");
            IChartDataSource <double> numDataSource = DataSources.FromNumericCellRange(sheet, rangeAddress);
            IndexOutOfRangeException  exception     = null;

            try
            {
                numDataSource.GetPointAt(-1);
            }
            catch (IndexOutOfRangeException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            exception = null;
            try
            {
                numDataSource.GetPointAt(numDataSource.PointCount);
            }
            catch (IndexOutOfRangeException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);
        }
Beispiel #5
0
        static void CreateChart(IDrawing drawing, ISheet sheet, IClientAnchor anchor, string serie1, string serie2, bool enableMajorGridline = false)
        {
            XSSFChart chart = (XSSFChart)drawing.CreateChart(anchor);

            chart.SetTitle("Test 1");
            IChartLegend legend = chart.GetOrCreateLegend();

            legend.Position = LegendPosition.TopRight;

            ILineChartData <double, double> data = chart.ChartDataFactory.CreateLineChartData <double, double>();

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

            leftAxis.Crosses = AxisCrosses.AutoZero;

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

            var s1 = data.AddSeries(xs, ys1);

            s1.SetTitle(serie1);
            var s2 = data.AddSeries(xs, ys2);

            s2.SetTitle(serie2);

            chart.Plot(data, bottomAxis, leftAxis);
            //add major gridline, available since NPOI 2.5.5
            var plotArea = chart.GetCTChart().plotArea;

            plotArea.catAx[0].AddNewMajorGridlines();
            plotArea.valAx[0].AddNewMajorGridlines();
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            IWorkbook wb             = new XSSFWorkbook();
            ISheet    sheet          = wb.CreateSheet("linechart");
            int       NUM_OF_ROWS    = 3;
            int       NUM_OF_COLUMNS = 10;

            // Create a row and put some cells in it. Rows are 0 based.
            IRow  row;
            ICell cell;

            for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++)
            {
                row = sheet.CreateRow((short)rowIndex);
                for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++)
                {
                    cell = row.CreateCell((short)colIndex);
                    cell.SetCellValue(colIndex * (rowIndex + 1));
                }
            }

            IDrawing      drawing = sheet.CreateDrawingPatriarch();
            IClientAnchor anchor  = drawing.CreateAnchor(0, 0, 0, 0, 0, 5, 10, 15);

            IChart       chart  = drawing.CreateChart(anchor);
            IChartLegend legend = chart.GetOrCreateLegend();

            legend.Position = LegendPosition.TopRight;

            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);

            leftAxis.SetCrosses(AxisCrosses.AutoZero);

            IChartDataSource <double> xs  = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
            IChartDataSource <double> ys1 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
            IChartDataSource <double> ys2 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(2, 2, 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);

            using (FileStream fs = File.Create("test.xlsx"))
            {
                wb.Write(fs);
            }
        }
Beispiel #7
0
        public void GenaretionChart()
        {
            FileStream RfileStream = new FileStream("D:\\test.xlsx", FileMode.Open, FileAccess.Read);
            //建立讀取資料的FileStream
            XSSFWorkbook wb = new XSSFWorkbook(RfileStream);
            //讀取檔案內的Workbook物件
            ISheet Wsheet = wb.GetSheetAt(1);
            //選擇圖表存放的sheet
            ISheet Rsheet = wb.GetSheetAt(0);
            //選擇資料來源的sheet
            IDrawing drawing = Wsheet.CreateDrawingPatriarch();
            //sheet產生drawing物件
            IClientAnchor clientAnchor = drawing.CreateAnchor(0, 0, 0, 0, 0, 0, 5, 10);
            //設定圖表位置
            IChart chart = drawing.CreateChart(clientAnchor);
            //產生chart物件
            IChartLegend legend = chart.GetOrCreateLegend();

            //還沒研究出這行在做甚麼
            legend.Position = LegendPosition.TopRight;
            ILineChartData <double, double> data = chart.ChartDataFactory.CreateLineChartData <double, double>();
            //產生存放資料的物件(資料型態為double)
            IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom);
            //設定X軸
            IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);

            //設定Y軸
            bottomAxis.Crosses = AxisCrosses.AutoZero;
            //設定X軸數值開始為0
            leftAxis.Crosses = AxisCrosses.AutoZero;
            //設定Y軸數值開始為0
            IChartDataSource <double> xs = DataSources.FromNumericCellRange(Rsheet, new CellRangeAddress(0, 4, 0, 0));
            //取得要讀取sheet的資料位置(CellRangeAddress(first_row,end_row, first_column, end_column))
            //x軸資料
            IChartDataSource <double> ys1 = DataSources.FromNumericCellRange(Rsheet, new CellRangeAddress(0, 4, 1, 1));
            //第一條y軸資料
            IChartDataSource <double> ys2 = DataSources.FromNumericCellRange(Rsheet, new CellRangeAddress(0, 4, 2, 2));

            //第二條y軸資料
            data.AddSeries(xs, ys1);
            data.AddSeries(xs, ys2);
            //加入到data
            chart.Plot(data, bottomAxis, leftAxis);
            //加入到chart
            FileStream WfileStream = new FileStream("D:\\test.xlsx", FileMode.Create, FileAccess.Write);

            //建立寫入資料的FileStream
            wb.Write(WfileStream);
            //將workbook寫入資料
            RfileStream.Close();
            //關閉FileStream
            WfileStream.Close();
            //關閉FileStream
        }
Beispiel #8
0
        public void Main()
        {
            IWorkbook wb             = new XSSFWorkbook();
            ISheet    sheet          = wb.CreateSheet("Sheet 1");
            int       NUM_OF_ROWS    = 3;
            int       NUM_OF_COLUMNS = 10;

            // Create a row and put some cells in it. Rows are 0 based.
            IRow  row;
            ICell cell;

            for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++)
            {
                row = sheet.CreateRow((short)rowIndex);
                for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++)
                {
                    cell = row.CreateCell((short)colIndex);
                    cell.SetCellValue(colIndex * (rowIndex + 1));
                }
            }

            IDrawing      drawing = sheet.CreateDrawingPatriarch();
            IClientAnchor anchor  = drawing.CreateAnchor(0, 0, 0, 0, 0, 5, 10, 15);

            IChart       chart  = drawing.CreateChart(anchor);
            IChartLegend legend = chart.GetOrCreateLegend();

            legend.Position = (LegendPosition.TopRight);

            IScatterChartData <double, double> data = chart.ChartDataFactory.CreateScatterChartData <double, double>();

            IValueAxis bottomAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Bottom);
            IValueAxis leftAxis   = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);

            leftAxis.Crosses = AxisCrosses.AutoZero;

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


            data.AddSeries(xs, ys1);
            data.AddSeries(xs, ys2);
            chart.Plot(data, bottomAxis, leftAxis);

            // Write the output to a file
            FileStream sw = File.Create("test.xlsx");

            wb.Write(sw);
            sw.Close();
        }
Beispiel #9
0
        private static void FillChartDataCells(ISheet sheet, int rowNum, IReadOnlyList <double> iterationsList, IChartDataSource <string> xAxis, ILineChartData <string, double> data)
        {
            var row = sheet.CreateRow(rowNum);

            for (int i = 0; i < iterationsList.Count; i++)
            {
                row.CreateCell(i).SetCellValue(iterationsList[i]);
            }

            var yAxis = DataSources.FromNumericCellRange(sheet,
                                                         new CellRangeAddress(rowNum, rowNum, 0, iterationsList.Count - 1));

            data.AddSeries(xAxis, yAxis).SetTitle(((Methods)(rowNum - 1)).ToString());
        }
Beispiel #10
0
        private IWorkbook CreateWorkbookWithChart()
        {
            IWorkbook wb             = new XSSFWorkbook();
            ISheet    sheet          = wb.CreateSheet("linechart");
            int       NUM_OF_ROWS    = 3;
            int       NUM_OF_COLUMNS = 10;

            // Create a row and Put some cells in it. Rows are 0 based.
            IRow  row;
            ICell cell;

            for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++)
            {
                row = sheet.CreateRow((short)rowIndex);
                for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++)
                {
                    cell = row.CreateCell((short)colIndex);
                    cell.SetCellValue(colIndex * (rowIndex + 1));
                }
            }

            IDrawing      Drawing = sheet.CreateDrawingPatriarch();
            IClientAnchor anchor  = Drawing.CreateAnchor(0, 0, 0, 0, 0, 5, 10, 15);

            IChart       chart  = Drawing.CreateChart(anchor);
            IChartLegend legend = chart.GetOrCreateLegend();

            legend.Position = (/*setter*/ LegendPosition.TopRight);

            ILineChartData <double, double> data = chart.ChartDataFactory.CreateLineChartData <double, double>();

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

            leftAxis.Crosses = (/*setter*/ AxisCrosses.AutoZero);

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

            data.AddSeries(xs, ys1);
            data.AddSeries(xs, ys2);

            chart.Plot(data, bottomAxis, leftAxis);

            return(wb);
        }
Beispiel #11
0
        public void TestNumericCellDataSource()
        {
            IWorkbook                 wb            = new HSSFWorkbook();
            ISheet                    sheet         = new SheetBuilder(wb, numericCells).Build();
            CellRangeAddress          numCellRange  = CellRangeAddress.ValueOf("A2:E2");
            IChartDataSource <double> numDataSource = DataSources.FromNumericCellRange(sheet, numCellRange);

            Assert.IsTrue(numDataSource.IsReference);
            Assert.IsTrue(numDataSource.IsNumeric);
            Assert.AreEqual(numericCells[0].Length, numDataSource.PointCount);
            for (int i = 0; i < numericCells[0].Length; ++i)
            {
                Assert.AreEqual(((double)numericCells[0][i]) * 2,
                                numDataSource.GetPointAt(i), 0.00001);
            }
        }
        static void CreateChart(ISheet sheet)
        {
            XSSFDrawing      drawing = (XSSFDrawing)sheet.CreateDrawingPatriarch();
            XSSFClientAnchor anchor  = (XSSFClientAnchor)drawing.CreateAnchor(0, 0, 0, 0, 3, 0, 10, 15);

            IChart       chart  = drawing.CreateChart(anchor);
            IChartLegend legend = chart.GetOrCreateLegend();

            legend.Position = LegendPosition.Bottom;

            IBarChartData <string, double> data = chart.ChartDataFactory.CreateBarChartData <string, double>();

            IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom);

            bottomAxis.MajorTickMark = AxisTickMark.None;
            IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);

            leftAxis.Crosses = AxisCrosses.AutoZero;
            leftAxis.SetCrossBetween(AxisCrossBetween.Between);

            IChartDataSource <string> categories = DataSources.FromStringCellRange(sheet, new CellRangeAddress(0, GetPracas().Count() - 1, 0, 0));

            // Abono
            IChartDataSource <double> xAbono = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, GetPracas().Count() - 1, 1, 1));
            // Isento
            IChartDataSource <double> xIsento = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, GetPracas().Count() - 1, 2, 2));
            // Violação
            IChartDataSource <double> xViolacao = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, GetPracas().Count() - 1, 3, 3));

            var s1 = data.AddSeries(categories, xAbono);

            s1.SetTitle("Abono");

            var s2 = data.AddSeries(categories, xIsento);

            s2.SetTitle("Isento");

            var s3 = data.AddSeries(categories, xViolacao);

            s3.SetTitle("Violação");

            chart.Plot(data, bottomAxis, leftAxis);
        }
Beispiel #13
0
        public void TestMixedCellDataSource()
        {
            IWorkbook                 wb             = new HSSFWorkbook();
            ISheet                    sheet          = new SheetBuilder(wb, mixedCells).Build();
            CellRangeAddress          mixedCellRange = CellRangeAddress.ValueOf("A1:F1");
            IChartDataSource <String> strDataSource  = DataSources.FromStringCellRange(sheet, mixedCellRange);
            IChartDataSource <double> numDataSource  = DataSources.FromNumericCellRange(sheet, mixedCellRange);

            for (int i = 0; i < mixedCells[0].Length; ++i)
            {
                if (i % 2 == 0)
                {
                    Assert.IsNull(strDataSource.GetPointAt(i));
                    Assert.AreEqual(((double)mixedCells[0][i]),
                                    numDataSource.GetPointAt(i), 0.00001);
                }
                else
                {
                    Assert.IsNaN(numDataSource.GetPointAt(i));
                    Assert.AreEqual(mixedCells[0][i], strDataSource.GetPointAt(i));
                }
            }
        }
Beispiel #14
0
        static void CreateExcel(List <List <double> > inputArray, double axisCoordinateData)
        {
            string parth = $"D:\\test\\Result{axisCoordinateData.ToString()}.xlsx";

            //XSSFWorkbook wb1 = null;
            using (var stream = new FileStream(parth, FileMode.Create, FileAccess.ReadWrite))
            //using (var stream = new FileStream(@"D:\test\Result.xlsx", FileMode.Create, FileAccess.ReadWrite))
            {
                //https://stackoverflow.com/questions/47793744/generate-excel-with-merged-header-using-npoi
                //https://stackoverflow.com/questions/32723483/adding-a-specific-autofilter-on-a-column
                //https://www.leniel.net/2009/10/npoi-with-excel-table-and-dynamic-chart.html
                //https://coderoad.ru/56089507/%D0%9A%D0%B0%D0%BA-%D1%81%D0%BE%D0%B7%D0%B4%D0%B0%D1%82%D1%8C-LineChart-%D0%BA%D0%BE%D1%82%D0%BE%D1%80%D1%8B%D0%B9-%D1%81%D0%BE%D0%B4%D0%B5%D1%80%D0%B6%D0%B8%D1%82-%D0%B4%D0%B2%D0%B0-CategoryAxis-%D0%B8%D1%81%D0%BF%D0%BE%D0%BB%D1%8C%D0%B7%D1%83%D1%8F-apache-POI
                //https://www.csharpcodi.com/vs2/1431/NPOI/src/NPOI.OOXML/XSSF/UserModel/Charts/XSSFLineChartData.cs/
                //https://itnan.ru/post.php?c=1&p=525492
                //https://overcoder.net/q/3405494/%D0%BA%D0%B0%D0%BA-%D1%81%D0%BE%D0%B7%D0%B4%D0%B0%D1%82%D1%8C-%D0%BB%D0%B8%D0%BD%D0%B5%D0%B9%D0%BD%D1%83%D1%8E-%D0%B4%D0%B8%D0%B0%D0%B3%D1%80%D0%B0%D0%BC%D0%BC%D1%83-%D0%B2%D0%BC%D0%B5%D1%81%D1%82%D0%B5-%D1%81-%D0%B4%D0%B0%D0%BD%D0%BD%D1%8B%D0%BC%D0%B8-%D0%B2-%D1%82%D0%B0%D0%B1%D0%BB%D0%B8%D1%86%D0%B5-excel-%D1%81-%D0%BF%D0%BE%D0%BC%D0%BE%D1%89%D1%8C%D1%8E
                //https://gist.github.com/Bykiev/2912494f5a3e4e6f91e02c12a6d6a82d
                //wb1 = new XSSFWorkbook(file);
                var wb = new XSSFWorkbook();
                //var wb = new ;
                var sheet = wb.CreateSheet("Test wall");
                //creating cell style for header
                var bStylehead = wb.CreateCellStyle();
                bStylehead.BorderBottom        = BorderStyle.Thin;
                bStylehead.BorderLeft          = BorderStyle.Thin;
                bStylehead.BorderRight         = BorderStyle.Thin;
                bStylehead.BorderTop           = BorderStyle.Thin;
                bStylehead.Alignment           = HorizontalAlignment.Center;
                bStylehead.VerticalAlignment   = VerticalAlignment.Center;
                bStylehead.FillBackgroundColor = HSSFColor.Green.Index;
                //var cellStyle =
                //var cellStyle = CreateCellStyleForHeader(wb);



                var Drawing = sheet.CreateDrawingPatriarch();

                //IClientAnchor anchor = Drawing.CreateAnchor(0, 0, 0, 0, 8, 1, 18, 16);
                var anchor = Drawing.CreateAnchor(0, 0, 0, 0, 8, 1, 18, 16);
                var chart  = Drawing.CreateChart(anchor);
                //var chart =
                //IChart chart = Drawing.CreateChart(anchor);
                IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom);
                IChartAxis leftAxis   = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);

                var chartData =
                    chart.ChartDataFactory.CreateLineChartData <double, double>();
                var lenCellRange             = inputArray.Count + 1;
                IChartDataSource <double> xs = DataSources.FromNumericCellRange(sheet, CellRangeAddress.ValueOf($"B2:B{lenCellRange}"));
                IChartDataSource <double> ys = DataSources.FromNumericCellRange(sheet, CellRangeAddress.ValueOf($"G2:G{lenCellRange}"));
                //IChartDataSource<double> ys = DataSources.FromNumericCellRange(sheet, CellRangeAddress.ValueOf("G2:G20"));
                var series = chartData.AddSeries(xs, ys);
                series.SetTitle("test");
                //chart.GetOrCreateLegend();

                chart.Plot(chartData, bottomAxis, leftAxis);



                var row = sheet.CreateRow(0);
                row.CreateCell(0, CellType.String).SetCellValue("x");
                row.CreateCell(1, CellType.String).SetCellValue("y");
                row.CreateCell(2, CellType.String).SetCellValue("z");
                row.CreateCell(3, CellType.String).SetCellValue("Hx");
                row.CreateCell(4, CellType.String).SetCellValue("Hy");
                row.CreateCell(5, CellType.String).SetCellValue("Hz");
                row.CreateCell(6, CellType.String).SetCellValue("Hsum");
                row.Cells[0].CellStyle = bStylehead;

                //filling the data
                var rowsCounter = 1;

                string fileName = @"C:\Users\Master\Documents\C_sharp\Work\wall_test.output";
                var    fileData = ReadFile(fileName);

                foreach (var rowData in inputArray)
                {
                    var rowD     = sheet.CreateRow(rowsCounter++);
                    var dCounter = 0;
                    foreach (var d in rowData)
                    {
                        rowD.CreateCell(dCounter++, CellType.Numeric).SetCellValue(Double.Parse(d.ToString().Replace(@".", @",")));
                    }
                }



                wb.Write(stream);

                wb.Close();
                //file.Close();
            }
        }
        private XSSFWorkbook CreateBarchart()
        {
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet    sheet    = (XSSFSheet)workbook.CreateSheet("WorkForceAnalytics");

            XSSFCellStyle styleHeader = (XSSFCellStyle)workbook.CreateCellStyle();

            styleHeader.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            //styleHeader.SetFont(getNewXSSFFont(workbook, styleHeader));

            XSSFCellStyle style = (XSSFCellStyle)workbook.CreateCellStyle();

            style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;

            XSSFRow row1 = (XSSFRow)sheet.CreateRow(0);

            row1.CreateCell(0).SetCellValue("");

            List <string> lstdatase = new List <string>(4)
            {
                "A1",
                "B1",
                "C1",
                "D1",
                "E1"
            };

            for (int i = 1; i < 5; i++)
            {
                row1.CreateCell(i).SetCellValue(lstdatase[i - 1].ToString());
                row1.GetCell(i).CellStyle = styleHeader;
            }

            int           rowvalue = 1;
            List <string> lstdata  = new List <string>(8)
            {
                "A",
                "B",
                "C",
                "D",
                "E",
                "F",
                "G",
                "H"
            };

            int d = 10;

            XSSFDrawing      drawing = (XSSFDrawing)sheet.CreateDrawingPatriarch();
            XSSFClientAnchor anchor  = (XSSFClientAnchor)drawing.CreateAnchor(0, 0, 0, 0, 6, 1, 15, 18);

            IChart       chart  = drawing.CreateChart(anchor);
            IChartLegend legend = chart.GetOrCreateLegend();

            legend.Position = (LegendPosition.Bottom);

            IBarChartData <string, double> data = chart.ChartDataFactory.CreateBarChartData <string, double>();

            IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom);
            IValueAxis leftAxis   = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);

            leftAxis.Crosses = AxisCrosses.AutoZero;
            leftAxis.SetCrossBetween(AxisCrossBetween.Between);

            IChartDataSource <string> xs = DataSources.FromStringCellRange(sheet, new NPOI.SS.Util.CellRangeAddress(0, 0, 1, 5 - 1));

            for (int ii = 0; ii < 8; ii++)
            {
                XSSFRow rownew = (XSSFRow)sheet.CreateRow(rowvalue);
                rownew.CreateCell(0).SetCellValue(lstdata[ii].ToString());

                for (int i = 1; i < 5; i++)
                {
                    rownew.CreateCell(i).SetCellValue(d * 0.1);
                    d++;
                    rownew.GetCell(i).CellStyle = style;
                }
                rowvalue++;


                IChartDataSource <double> ys = DataSources.FromNumericCellRange(sheet, new NPOI.SS.Util.CellRangeAddress(ii + 1, ii + 1, 1, 5 - 1));
                data.AddSeries(xs, ys).SetTitle(lstdata[ii].ToString());
            }

            chart.Plot(data, bottomAxis, leftAxis);

            sheet.ForceFormulaRecalculation = true;
            return(workbook);
        }
Beispiel #16
0
        public void Run()
        {
            IWorkbook wb    = new XSSFWorkbook();
            ISheet    sheet = wb.CreateSheet("linechart");

            IRow  row;
            ICell cell;


            // line chart
            for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++)
            {
                row = sheet.CreateRow((short)rowIndex);
                for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++)
                {
                    cell = row.CreateCell((short)colIndex);
                    cell.SetCellValue(colIndex * (rowIndex + 1));
                }
            }

            IDrawing      drawing = sheet.CreateDrawingPatriarch();
            IClientAnchor anchor1 = drawing.CreateAnchor(0, 0, 0, 0, 0, 5, 10, 15);

            CreateChart(drawing, sheet, anchor1, "title1", "title2");
            IClientAnchor anchor2 = drawing.CreateAnchor(0, 0, 0, 0, 0, 20, 10, 35);

            CreateChart(drawing, sheet, anchor2, "s1", "s2");



            // ScatterChart
            ISheet sheet1 = wb.CreateSheet("ScatterChart");

            for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++)
            {
                row = sheet1.CreateRow((short)rowIndex);
                for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++)
                {
                    cell = row.CreateCell((short)colIndex);
                    cell.SetCellValue(colIndex * (rowIndex + 1));
                }
            }

            drawing = sheet1.CreateDrawingPatriarch();
            IClientAnchor anchor = drawing.CreateAnchor(0, 0, 0, 0, 0, 5, 10, 15);

            IChart       chart  = drawing.CreateChart(anchor);
            IChartLegend legend = chart.GetOrCreateLegend();

            legend.Position = (LegendPosition.TopRight);

            IScatterChartData <double, double> data = chart.ChartDataFactory.CreateScatterChartData <double, double>();

            IValueAxis bottomAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Bottom);
            IValueAxis leftAxis   = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);

            leftAxis.Crosses = AxisCrosses.AutoZero;

            IChartDataSource <double> xs  = DataSources.FromNumericCellRange(sheet1, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
            IChartDataSource <double> ys1 = DataSources.FromNumericCellRange(sheet1, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
            IChartDataSource <double> ys2 = DataSources.FromNumericCellRange(sheet1, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));


            data.AddSeries(xs, ys1);
            data.AddSeries(xs, ys2);
            chart.Plot(data, bottomAxis, leftAxis);


            using (FileStream fs = File.Create(@"C:\00.Dev\temp\ChartTest.xlsx"))
            {
                wb.Write(fs);
            }
        }