Last active
November 3, 2022 06:21
-
-
Save tedliou/9aa6140a568a5025973ef65003d01d7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using NPOI.HSSF.UserModel; | |
using NPOI.SS.UserModel; | |
using NPOI.XSSF.UserModel; | |
using NPOI.SS.Util; | |
using NPOI.SS.UserModel.Charts; | |
Test(); | |
void Test() | |
{ | |
var exampleData = new List<ExampleData> | |
{ | |
new ExampleData(22, 174), | |
new ExampleData(150, 172), | |
new ExampleData(180, 140) | |
}; | |
var workbook = new XSSFWorkbook(); | |
var sheet = workbook.CreateSheet("Family List"); | |
var header = sheet.CreateRow(0); | |
var type = typeof(ExampleData); | |
var properties = type.GetProperties().Select(x => x.Name).ToList(); | |
var propertyCount = properties.Count(); | |
header.CreateCell(0).SetCellValue("ID"); | |
for (int i = 1; i <= propertyCount; i++) | |
{ | |
header.CreateCell(i).SetCellValue(properties[i - 1]); | |
} | |
for (int i = 0; i < exampleData.Count; i++) | |
{ | |
var data = exampleData[i]; | |
var row = sheet.CreateRow(i + 1); | |
row.CreateCell(0).SetCellValue(i + 1); | |
row.CreateCell(1).SetCellValue(data.A); | |
row.CreateCell(2).SetCellValue(data.B); | |
} | |
// Try to draw a line graph | |
var drawing = sheet.CreateDrawingPatriarch(); | |
var anchor = drawing.CreateAnchor(0, 0, 0, 0, 4, 0, 16, 16); | |
var chart = (XSSFChart)drawing.CreateChart(anchor); | |
chart.SetTitle("Line graph"); | |
var ledge = chart.GetOrCreateLegend(); | |
ledge.Position = NPOI.SS.UserModel.Charts.LegendPosition.TopRight; | |
var chartData = chart.ChartDataFactory.CreateLineChartData<double, double>(); | |
var xAxis = chart.ChartAxisFactory.CreateCategoryAxis(NPOI.SS.UserModel.Charts.AxisPosition.Bottom); | |
var yAxis = chart.ChartAxisFactory.CreateValueAxis(NPOI.SS.UserModel.Charts.AxisPosition.Left); | |
yAxis.Crosses = AxisCrosses.AutoZero; | |
yAxis.IsVisible = true; | |
var xSource = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(1, exampleData.Count, 0, 0)); | |
var y1Source = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(1, exampleData.Count, 1, 1)); | |
var y2Source = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(1, exampleData.Count, 2, 2)); | |
var s1 = chartData.AddSeries(xSource, y1Source); | |
s1.SetTitle("Age"); | |
var s2 = chartData.AddSeries(xSource, y2Source); | |
s2.SetTitle("Height"); | |
chart.Plot(chartData, xAxis, yAxis); | |
var fileStream = File.Create($"FamilyList-{DateTime.Now.ToString("HHmmss")}.xlsx"); | |
workbook.Write(fileStream); | |
Console.WriteLine("Done!"); | |
} | |
public class ExampleData | |
{ | |
public int A { get; set; } = 0; | |
public int B { get; set; } = 0; | |
public ExampleData(int age, int height) | |
{ | |
A = age; | |
B = height; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment