using NPOI.SS.UserModel;
using NPOI.SS.UserModel.Charts;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;
class Program
{
static void Main(string[] args)
{
string filePath = "C:\\Users\\lenovo\\Downloads\\testChart.xlsx";
// Create a new Excel workbook
IWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet("ChartSheet");
// Create some sample data
int startDataRow = 1;
int rowIndex = startDataRow;
int columnQtt = 2;
int tipoId = 1;
string currentTipo = "Sample Tipo";
for (int i = 0; i < 5; i++)
{
IRow row = sheet.CreateRow(rowIndex++);
row.CreateCell(0).SetCellValue("Category " + i);
row.CreateCell(1).SetCellValue(new Random().Next(1, 100)); // Sample value
}
// Create chart
CreateChart(columnQtt, rowIndex, tipoId, currentTipo, sheet, startDataRow);
// Write the workbook content to a file
using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
{
workbook.Write(fileStream);
}
}
private static void CreateChart(int columnQtt, int rowIndex, int tipoId, string currentTipo, ISheet sheet, int startDataRow)
{
XSSFDrawing drawing = (XSSFDrawing)sheet.CreateDrawingPatriarch();
XSSFClientAnchor anchor = (XSSFClientAnchor)drawing.CreateAnchor(0, 0, 0, 0, columnQtt + 1, startDataRow - 2, columnQtt + 10, startDataRow + 12);
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);
int endDataRow = rowIndex - 1;
IChartDataSource<string> categoryAxis = DataSources.FromStringCellRange(sheet, new CellRangeAddress(startDataRow, endDataRow, 0, 0));
IChartDataSource<double> valueAxis = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(startDataRow, endDataRow, columnQtt - 1, columnQtt - 1));
var serie = barChartData.AddSeries(categoryAxis,valueAxis);
serie.SetTitle(currentTipo);
chart.Plot(barChartData, bottomAxis, leftAxis);
}
}
This is NPOI package Bar chart(Horizontal Bar Chart) coding. I want Column Chart(Vertical Bar Chart) which is not available so i am tring to reverse parameters of Bar chart to convert it into Column Chart.
var serie = barChartData.AddSeries(valueAxis,categoryAxis); //reversed parameters
It is throwing error like
System.ArgumentException: 'Value data source must be numeric.'
My Requirement is that I want to Generate Column chart in excel using NPOI nuget package, the way i am trying is possible to get outcome or does NPOI support Column Chart ? Is there any new Release sample link to refer ?
Have you tried to use CreateColumnChartData
instead of CreateBarChartData
?
Only change line
IBarChartData<string, double> barChartData = chart.ChartDataFactory.CreateBarChartData<string, double>();
to
IColumnChartData<string, double> columnChartData = chart.ChartDataFactory.CreateColumnChartData<string, double>();
I'm using NPOI 2.6.2