简体   繁体   English

C# NPOI 绘制折线图到 Excel 当单元格值在 0.001 到 9.999 范围内(小数)

[英]C# NPOI draw line chart to Excel when cell value on range 0.001 to 9.999 (fractional numbers)

C# NPOI draw line chart to Excel when cell value on range 0.001 to 9.999 (fractional numbers) C# NPOI 绘制折线图到 Excel 当单元格值在 0.001 到 9.999 范围内(小数)

I can draw chart when cell value is similar (0, 1, 2, ... 9) or other integer value but when i try use (0,293 or other fractional numbers) i saw error:当单元格值相似(0、1、2、... 9)或其他 integer 值时,我可以绘制图表,但是当我尝试使用(0,293 或其他小数)时,我看到了错误:

"Removed part: /xl/drawings/drawing1.xml part. (Drawing figure)" “删除部分:/xl/drawings/drawing1.xml 部分。(图纸)”
(when open Excel file). (当打开 Excel 文件时)。

I tried searched solution in google and github but dont found simular situation.我尝试在 google 和 github 中搜索解决方案,但没有找到类似的情况。 Maybe some one faced with this issue.也许有人面临这个问题。

Thanks for any help.谢谢你的帮助。

Updated added report files Good report Repot with error message更新了添加的报告文件带有错误消息的良好报告报告

**Added code for explanation issues:** **为解释问题添加了代码:**

 class Program { const int NUM_OF_ROWS = 3; const int NUM_OF_COLUMNS = 10; static void CreateChart(IDrawing drawing, ISheet sheet, IClientAnchor anchor) { 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.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom); IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left); 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)); data.AddSeries(xs, ys1); chart.Plot(data, bottomAxis, leftAxis); } static void Main(string[] args) { IWorkbook wb = new XSSFWorkbook(); ISheet sheet = wb.CreateSheet("linechart"); // 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); //This generate graph //cell.SetCellValue(colIndex * (rowIndex + 1)); //This make error when open Excel file cell.SetCellValue(colIndex * (rowIndex + 1) + 0.1); } } IDrawing drawing = sheet.CreateDrawingPatriarch(); IClientAnchor anchor1 = drawing.CreateAnchor(0, 0, 0, 0, 0, 5, 10, 8); CreateChart(drawing, sheet, anchor1); //Write to Excel file using (FileStream fs =File.Create("test1.xlsx")) { wb.Write(fs); } } }

I found solution - not perfect but it is works.我找到了解决方案 - 不是完美的,但它是有效的。

  1. Set cell type to string.将单元格类型设置为字符串。
  2. Draw chart (with default value(0) becouse we use string cell type for draw)绘制图表(使用默认值(0),因为我们使用字符串单元格类型进行绘制)
  3. Update cell type to numeric then chart will auto refresh with numeric data.将单元格类型更新为数字,然后图表将使用数字数据自动刷新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM