net core OpenXml Excel 饼状图
时间: 2023-08-26 16:07:12 浏览: 207
你可以使用 OpenXml SDK 和 .NET Core 来创建 Excel 文档和饼状图。以下是一个示例:
1. 首先,你需要安装 OpenXml SDK。可以通过 NuGet 包管理器来安装。
2. 创建一个新的 Excel 文档,并添加一个工作表。你可以使用以下代码:
```csharp
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
// 创建 Excel 文档
SpreadsheetDocument document = SpreadsheetDocument.Create("PieChart.xlsx", SpreadsheetDocumentType.Workbook);
// 添加一个工作簿
WorkbookPart workbookPart = document.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
// 添加一个工作表
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
// 保存文档
workbookPart.Workbook.Save();
document.Close();
```
3. 添加数据到工作表中。你可以使用以下代码:
```csharp
// 获取工作表 SheetData
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
// 添加数据行
Row row = new Row();
Cell cell1 = new Cell() { CellValue = new CellValue("Apple"), DataType = CellValues.String };
Cell cell2 = new Cell() { CellValue = new CellValue("50"), DataType = CellValues.Number };
row.Append(cell1, cell2);
sheetData.Append(row);
row = new Row();
cell1 = new Cell() { CellValue = new CellValue("Orange"), DataType = CellValues.String };
cell2 = new Cell() { CellValue = new CellValue("20"), DataType = CellValues.Number };
row.Append(cell1, cell2);
sheetData.Append(row);
row = new Row();
cell1 = new Cell() { CellValue = new CellValue("Banana"), DataType = CellValues.String };
cell2 = new Cell() { CellValue = new CellValue("30"), DataType = CellValues.Number };
row.Append(cell1, cell2);
sheetData.Append(row);
```
4. 创建一个饼状图,并将其添加到工作表中。你可以使用以下代码:
```csharp
// 添加一个图表部件
DrawingsPart drawingsPart = worksheetPart.AddNewPart<DrawingsPart>();
drawingsPart.WorksheetDrawing = new WorksheetDrawing();
// 创建一个图表
ChartPart chartPart = drawingsPart.AddNewPart<ChartPart>();
chartPart.ChartSpace = new ChartSpace();
chartPart.ChartSpace.AppendChild(new EditingLanguage() { Val = new StringValue("en-US") });
Chart chart = chartPart.ChartSpace.AppendChild(new Chart());
// 创建一个图表标题
Title chartTitle = chart.AppendChild(new Title());
chartTitle.AppendChild(new ChartText(new RichText(new BodyProperties(), new Paragraph(new Run(new Text("Fruit Sales"))))));
Layout layout = chart.AppendChild(new Layout());
ManualLayout manualLayout = layout.AppendChild(new ManualLayout());
manualLayout.Top = new Top() { Val = 0.2 };
manualLayout.Left = new Left() { Val = 0.1 };
manualLayout.Width = new Width() { Val = 0.8 };
manualLayout.Height = new Height() { Val = 0.8 };
// 创建一个图表区域
PlotArea plotArea = chart.AppendChild(new PlotArea());
Layout layout1 = plotArea.AppendChild(new Layout());
ManualLayout manualLayout1 = layout1.AppendChild(new ManualLayout());
manualLayout1.Top = new Top() { Val = 0 };
manualLayout1.Left = new Left() { Val = 0 };
manualLayout1.Width = new Width() { Val = 1 };
manualLayout1.Height = new Height() { Val = 1 };
// 创建一个饼状图
PieChart pieChart = plotArea.AppendChild(new PieChart());
PieChartSeries pieChartSeries = pieChart.AppendChild(new PieChartSeries(new Index() { Val = new UInt32Value(0) }));
StringReference stringReference = new StringReference();
StringCache stringCache = new StringCache();
StringPoint stringPoint = new StringPoint() { Index = new UInt32Value(0U) };
NumericPoint numericPoint = new NumericPoint() { Index = new UInt32Value(0U) };
stringPoint.AppendChild(new NumericValue("1"));
numericPoint.AppendChild(new NumericValue("50"));
stringReference.AppendChild(stringCache);
stringCache.AppendChild(stringPoint);
pieChartSeries.AppendChild(new SeriesText(stringReference));
DataLabels dataLabels = pieChartSeries.AppendChild(new DataLabels());
DataLabel dataLabel = dataLabels.AppendChild(new DataLabel());
dataLabel.AppendChild(new ShowLegendKey() { Val = false });
dataLabel.AppendChild(new ShowValue() { Val = true });
dataLabel.AppendChild(new ShowCategoryName() { Val = false });
dataLabel.AppendChild(new ShowPercent() { Val = true });
dataLabel.AppendChild(new Separator() { Val = new StringValue(", ") });
```
5. 将图表与工作表单元格关联,并保存文档。你可以使用以下代码:
```csharp
// 在工作表中添加图表
ChartPart chartPart = drawingsPart.ChartParts.First();
TwoCellAnchor twoCellAnchor = drawingsPart.WorksheetDrawing.AppendChild(new TwoCellAnchor());
twoCellAnchor.Append(new FromMarker(new ColumnId("0"), new ColumnOffset("0"), new RowId("6"), new RowOffset("0")));
twoCellAnchor.Append(new ToMarker(new ColumnId("7"), new ColumnOffset("0"), new RowId("22"), new RowOffset("0")));
twoCellAnchor.Append(new GraphicFrame(new Graphic(new GraphicData(chartPart.ChartSpace.OuterXml)))));
worksheetPart.Worksheet.Save();
// 关闭文档
document.Close();
```
以上代码创建了一个包含饼状图的 Excel 文档。你可以根据自己的需求修改代码。
阅读全文