如何使用 OpenXml SDK 将图表从 Excel 复制到 Word?
How to copy a Chart from Excel to Word with OpenXml SDK?
我有一个 Excel 文件,其中包含一个 sheet 和一张图表。我正在尝试使用 OpenXml SDK 复制此图表并将其粘贴到新的 Word 文档中。我不想要图表的图像,而是一个可编辑的图表对象,不引用 Excel 文件。
这是我试过的:
using (SpreadsheetDocument document = SpreadsheetDocument.Open("Test1.xlsx", true))
{
var bkPart = document.WorkbookPart;
var workbook = bkPart.Workbook;
var s = workbook.Sheets.FirstOrDefault();
var wsPart = bkPart.WorksheetParts.FirstOrDefault();
var dp = wsPart.DrawingsPart;
var dWs = dp.WorksheetDrawing;
var cp = dp.ChartParts.FirstOrDefault();
var plotArea = cp.ChartSpace.Descendants<PlotArea>().FirstOrDefault();
using (var docx = WordprocessingDocument.Open("Test1.docx", true))
{
docx.MainDocumentPart.Document.Body.RemoveAllChildren();
var cpw = docx.MainDocumentPart.AddPart(cp);
var p = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
var r = new Run();
var d = new Drawing();
var i = new Inline();
var g = new Graphic();
var gd = new GraphicData();
var c = new DocumentFormat.OpenXml.Drawing.Charts.Chart();
docx.MainDocumentPart.Document.Body.Append(p);
p.Append(r);
r.Append(d);
d.Append(i);
i.Append(g);
g.Append(gd);
gd.Append(c);
// I am stuck here
}
}
我尝试从 Excel 获取 ChartPart 并将其添加到 Word 文档,然后创建一个引用插入的新 ChartPart 的新图表。不幸的是,我卡在了这一步。我如何获取并使用此参考来创建图表?否则还有其他方法可以将图表从 Excel 复制到 Word 吗?
提前致谢。
我找到了将图表从 Excel 复制到 Word 的解决方法:
- 在 Word 文档上创建一个新的 ChartPart。
- 向此 ChartPart 添加从 Excel 克隆的 ChartSpace。
- 将新图表添加到引用新 ChartPart 的 Word 文档。
这是代码:
using (SpreadsheetDocument document = SpreadsheetDocument.Open("Test1.xlsx", true))
{
var bkPart = document.WorkbookPart;
var workbook = bkPart.Workbook;
var s = workbook.Sheets.FirstOrDefault();
var wsPart = bkPart.WorksheetParts.FirstOrDefault();
var dp = wsPart.DrawingsPart;
var dWs = dp.WorksheetDrawing;
var cp = dp.ChartParts.FirstOrDefault();
using (var docx = WordprocessingDocument.Open("Test1.docx", true))
{
MainDocumentPart mainPart = docx.MainDocumentPart;
ChartPart chartPart = mainPart.AddNewPart<ChartPart>("rId777");
chartPart.ChartSpace = (ChartSpace)cp.ChartSpace.Clone();
var paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph() { RsidParagraphAddition = "00C75AEB", RsidRunAdditionDefault = "000F3EFF" };
Run run = new Run();
Drawing drawing = new Drawing();
Inline inline = new Inline();
inline.Append(new Extent() { Cx = 5274310L, Cy = 3076575L });
DocProperties docPros = new DocProperties() { Id = (UInt32Value)1U, Name = "Chart7" };
inline.Append(docPros);
Graphic g = new Graphic();
var graphicData = new GraphicData() { Uri = "http://schemas.openxmlformats.org/drawingml/2006/chart" };
var chartReference = new ChartReference() { Id = "rId777" };
graphicData.Append(chartReference);
g.Append(graphicData);
inline.Append(g);
drawing.Append(inline);
run.Append(drawing);
paragraph.Append(run);
mainPart.Document.Body.Append(paragraph);
}
}
我有一个 Excel 文件,其中包含一个 sheet 和一张图表。我正在尝试使用 OpenXml SDK 复制此图表并将其粘贴到新的 Word 文档中。我不想要图表的图像,而是一个可编辑的图表对象,不引用 Excel 文件。
这是我试过的:
using (SpreadsheetDocument document = SpreadsheetDocument.Open("Test1.xlsx", true))
{
var bkPart = document.WorkbookPart;
var workbook = bkPart.Workbook;
var s = workbook.Sheets.FirstOrDefault();
var wsPart = bkPart.WorksheetParts.FirstOrDefault();
var dp = wsPart.DrawingsPart;
var dWs = dp.WorksheetDrawing;
var cp = dp.ChartParts.FirstOrDefault();
var plotArea = cp.ChartSpace.Descendants<PlotArea>().FirstOrDefault();
using (var docx = WordprocessingDocument.Open("Test1.docx", true))
{
docx.MainDocumentPart.Document.Body.RemoveAllChildren();
var cpw = docx.MainDocumentPart.AddPart(cp);
var p = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
var r = new Run();
var d = new Drawing();
var i = new Inline();
var g = new Graphic();
var gd = new GraphicData();
var c = new DocumentFormat.OpenXml.Drawing.Charts.Chart();
docx.MainDocumentPart.Document.Body.Append(p);
p.Append(r);
r.Append(d);
d.Append(i);
i.Append(g);
g.Append(gd);
gd.Append(c);
// I am stuck here
}
}
我尝试从 Excel 获取 ChartPart 并将其添加到 Word 文档,然后创建一个引用插入的新 ChartPart 的新图表。不幸的是,我卡在了这一步。我如何获取并使用此参考来创建图表?否则还有其他方法可以将图表从 Excel 复制到 Word 吗?
提前致谢。
我找到了将图表从 Excel 复制到 Word 的解决方法:
- 在 Word 文档上创建一个新的 ChartPart。
- 向此 ChartPart 添加从 Excel 克隆的 ChartSpace。
- 将新图表添加到引用新 ChartPart 的 Word 文档。
这是代码:
using (SpreadsheetDocument document = SpreadsheetDocument.Open("Test1.xlsx", true))
{
var bkPart = document.WorkbookPart;
var workbook = bkPart.Workbook;
var s = workbook.Sheets.FirstOrDefault();
var wsPart = bkPart.WorksheetParts.FirstOrDefault();
var dp = wsPart.DrawingsPart;
var dWs = dp.WorksheetDrawing;
var cp = dp.ChartParts.FirstOrDefault();
using (var docx = WordprocessingDocument.Open("Test1.docx", true))
{
MainDocumentPart mainPart = docx.MainDocumentPart;
ChartPart chartPart = mainPart.AddNewPart<ChartPart>("rId777");
chartPart.ChartSpace = (ChartSpace)cp.ChartSpace.Clone();
var paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph() { RsidParagraphAddition = "00C75AEB", RsidRunAdditionDefault = "000F3EFF" };
Run run = new Run();
Drawing drawing = new Drawing();
Inline inline = new Inline();
inline.Append(new Extent() { Cx = 5274310L, Cy = 3076575L });
DocProperties docPros = new DocProperties() { Id = (UInt32Value)1U, Name = "Chart7" };
inline.Append(docPros);
Graphic g = new Graphic();
var graphicData = new GraphicData() { Uri = "http://schemas.openxmlformats.org/drawingml/2006/chart" };
var chartReference = new ChartReference() { Id = "rId777" };
graphicData.Append(chartReference);
g.Append(graphicData);
inline.Append(g);
drawing.Append(inline);
run.Append(drawing);
paragraph.Append(run);
mainPart.Document.Body.Append(paragraph);
}
}