簡體   English   中英

如何將折線圖繪制到 EXCEL 使用單元格值 XX(例如 double 0,1 - 1,1 - ... - x,1)

[英]How draw line chart to EXCEL use cell value X.X (for example double 0,1 - 1,1 - … - x,1)

如何將折線圖繪制到 EXCEL 使用單元格值 XX(例如 double 0,1 - 1,1 -... - x,1)

我可以在沒有分數(0、1、2、... 9)的單元格值時繪制圖表,但是當單元格值有(0,1 或其他小數)時,我在打開 excel 文件時看到錯誤:

“刪除部分:/xl/drawings/drawing1.xml 部分。(圖紙)”

我一整天都找不到解決方案。 也許有人面臨這個問題。

謝謝你的幫助。

測試文件正確錯誤

**解釋問題的代碼:**

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("test.xlsx"))
        {
            wb.Write(fs);
        }
    }

我找到了解決方案 - 不是完美的,但它是有效的。

  1. 將單元格類型設置為字符串。
  2. 繪制圖表(使用默認值(0),因為我們使用字符串單元格類型進行繪制)
  3. 將單元格類型更新為數字,然后圖表將使用數字數據自動刷新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM