我想以编程方式创建docx文件。我需要在其中包含excel图表,然后我必须将其导出为pdf。我使用C#4.0和Microsoft.Office.Interop。
问题是在导出的pdf文件中,1/5部分图表被削减。 以下是问题的一个示例:
2shared.com link to the pdf file
如果我尝试保存在xps中,也会发生这种情况。
如果我将excel图表复制并粘贴到word中,那么它之后会以某种方式工作......
还有另一个问题。图表随机缩小。这取决于excel图表是否在脚本编写过程中可见(这张图片是从2010年的单词中捕获的,我用红色圈出了问题):
我使用AddOLEObject方法创建excel图表作为InlineShapes,classtype:“Excel.Chart.8”。我将它们的大小设置为200x200(wrdInlineShape.Width,wrdInlineShape.Height)
Adam说我应该包含源代码。这很长,但是你走了:
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();
}
答案 0 :(得分:1)
我找到了解决方法。
我将我刚刚制作的excel图表剪切到剪贴板,然后将其粘贴到同一个地方,然后等待2秒钟,继续下一个图表。 剪切/粘贴似乎将图表转换为另一种类型的word对象,通过pdf正确导出。
wrdInlineShape.Range.Cut();
shapeBookMark.Range.Paste();
Thread.Sleep(2000);
(shapeBookMark是我放置图表的原始范围。需要等待2秒,否则Word会因错误信息而冻结)