我想以编程方式创建 docx 文件。我需要在其中包含 excel 图表,然后我必须将其导出为 pdf。为此,我使用 C# 4.0 和 Microsoft.Office.Interop。
问题是在导出的 pdf 文件中,图表的 1/5 部分被削减了。这是问题的一个例子:
如果我尝试保存在 xps 中,也会发生这种情况。
如果我将excel图表复制并粘贴到word中,然后它会以某种方式工作......
还有另一个问题。图表是随机缩小的。这取决于在脚本编写过程中excel图表是否可见(这张图片是从word 2010中捕获的,我用红色圈出了问题):
我将 Excel 图表创建为 InlineShapes,使用 AddOLEObject 方法 classtype: "Excel.Chart.8" 。我将它们的大小设置为 200x200(wrdInlineShape.Width,wrdInlineShape.Height)
亚当说我应该包括源代码。它很长,但给你:
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
app.Visible = true;
Word.Document doc = app.Documents.Open(@"D:\programozás\OHV\template.docx");
string classtype = "Excel.Chart.8";
object oEndOfDoc = "\\endofdoc";
Bookmark titleBookMark = doc.Bookmarks.get_Item("title");
titleBookMark.Range.Text = "I love OHV";
for (int xi = 1; xi <= 2; xi++)
{
// Microsoft.Office.Interop.Word.Range wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;
Bookmark shapeBookMark = doc.Bookmarks.get_Item("shape" + xi );
Bookmark subtitleBookMark = doc.Bookmarks.get_Item("title" + xi);
subtitleBookMark.Range.Text = "Super Subject";
Word.InlineShape wrdInlineShape = doc.InlineShapes.AddOLEObject(classtype, Range: shapeBookMark.Range);
if (wrdInlineShape.OLEFormat.ProgID == classtype)
{
object verb = Word.WdOLEVerb.wdOLEVerbHide;
wrdInlineShape.OLEFormat.DoVerb(ref verb);
Random rn = new Random();
Excel.Workbook obook = (Excel.Workbook)wrdInlineShape.OLEFormat.Object;
Excel.Worksheet sheet = (Excel.Worksheet)obook.Worksheets["Sheet1"];
for (int i = 1; i <= 5; i++)
{
((Microsoft.Office.Interop.Excel.Range)sheet.Cells[i, 1]).Value = i + ".";
for (int c = 2; c <= 3; c++)
{
((Microsoft.Office.Interop.Excel.Range)sheet.Cells[i, c]).Value = rn.Next(1, 5);
((Microsoft.Office.Interop.Excel.Range)sheet.Cells[i, c]).Value = rn.Next(1, 5);
}
((Microsoft.Office.Interop.Excel.Range)sheet.Cells[i, 4]).Value = "";
}
wrdInlineShape.Width = 200;
wrdInlineShape.Height = 200;
Excel.Chart chart = obook.ActiveChart;
obook.ActiveChart.ApplyLayout(9, Excel.XlChartType.xlColumnClustered);
obook.ActiveChart.ChartTitle.Caption = "BMEVIHVA109 - Jelek és Rendszerek\n XY kurzus - Dr Erős Péter";
obook.ActiveChart.ChartTitle.Font.Size = 6;
chart.ChartArea.Fill.ForeColor.SchemeColor = 41;
Console.WriteLine(chart.ChartArea.Fill.ForeColor.RGB);
chart.ChartArea.Fill.BackColor.SchemeColor = 23;
Console.WriteLine(chart.ChartArea.Fill.BackColor.RGB);
chart.ChartArea.Fill.TwoColorGradient(Microsoft.Office.Core.MsoGradientStyle.msoGradientHorizontal, 1);
obook.ActiveChart.SetSourceData(sheet.get_Range("A1", "B5"));
obook.ActiveChart.Legend.Delete();
chart.ChartArea.RoundedCorners = true;
Excel.Axis axis = (Excel.Axis)chart.Axes(
Excel.XlAxisType.xlCategory,
Excel.XlAxisGroup.xlPrimary);
axis.HasTitle = true;
axis.AxisTitle.Text = "54 kérdőív";
axis.AxisTitle.Font.Size = 6;
axis.HasMajorGridlines = true;
axis = (Excel.Axis)chart.Axes(
Excel.XlAxisType.xlValue,
Excel.XlAxisGroup.xlPrimary);
axis.HasTitle = false;
axis.HasMajorGridlines = true;
axis.MaximumScale = 5;
axis.MinimumScale = 0;
wrdInlineShape.Width = 200;
wrdInlineShape.Height = 200;
Microsoft.Office.Interop.Word.Range tableRange = doc.Bookmarks.get_Item("table" + xi).Range;
// sheet.UsedRange.Copy();
doc.SetDefaultTableStyle("Light List - Accent 4", false);
Table table = doc.Tables.Add(tableRange, 3, 2);
Object style = "Table Grid 1";
table.set_Style(ref style);
table.Cell(1, 1).Merge(table.Cell(1, 2));
table.Cell(1, 1).Range.Text = "Válaszok száma";
table.Cell(1, 1).Range.Font.Bold = 2;
table.Cell(2, 1).Range.Text = "This works lol";
table.Cell(2, 2).Range.Text = "This works lol 2 2";
table.Cell(3, 1).Range.Text = "This works lol";
table.Cell(3, 2).Range.Text = "This works lol 2 2";
table.Cell(4, 1).Range.Text = "This works lol";
table.Cell(4, 2).Range.Text = "This works lol 2 2";
table.Borders.OutsideLineWidth = WdLineWidth.wdLineWidth225pt;
}
}
// doc.InlineShapes.AddPicture(@"D:\programozás\OHV\OHV\OHV\bin\Debug\Capture.JPG", false, true);
doc.SaveAs2(@"D:\programozás\OHV\Doksi.docx");
Console.WriteLine("Exit");
try
{
//Novacode.DocX docx = Novacode.DocX.Load(@"D:\programozás\OHV\Doksi.docx");
//docx.SaveAs(@"D:\programozás\OHV\Doksi.pdf");
doc.ExportAsFixedFormat(@"D:\programozás\OHV\Doksi.pdf",WdExportFormat.wdExportFormatPDF);
doc.Close();
app.Quit();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}