C# 使用C将多个Excel图表添加到Word中#
我使用C#自动将多个直方图添加到word中,我创建了一个循环,在每次迭代中,程序在单独的页面中添加一个图表。代码很好,但是由于某种原因Excel崩溃,程序停止 有什么建议吗C# 使用C将多个Excel图表添加到Word中#,c#,excel,ms-word,C#,Excel,Ms Word,我使用C#自动将多个直方图添加到word中,我创建了一个循环,在每次迭代中,程序在单独的页面中添加一个图表。代码很好,但是由于某种原因Excel崩溃,程序停止 有什么建议吗 foreach (KeyValuePair<int, int> unique_year_item in uniqe_years_dic) { Microsoft.Office.Interop.Word.Paragraph para_fig4 = document.Content.Paragraphs.
foreach (KeyValuePair<int, int> unique_year_item in uniqe_years_dic)
{
Microsoft.Office.Interop.Word.Paragraph para_fig4 = document.Content.Paragraphs.Add(ref missing);
styleHeading2 = "Heading 2";
para_fig3.Range.set_Style(ref styleHeading2);
para_fig3.Range.Text = "Reports by Zone per for year "+ unique_year_item.Key.ToString() ;
para_fig3.Range.InsertParagraphAfter();
Dictionary<string, int> compliants_by_zone_per_year = new Dictionary<string, int>();
for (int i = 0; i < Global.sub_table.Count(); i++)
{
DateTime d =DateTime.Parse(Global.sub_table[i][2]);
if(unique_year_item.Key == d.Year)
{
string zone_key = Global.sub_table[i][1] + Global.sub_table[i][0];
if (!compliants_by_zone_per_year.ContainsKey(zone_key))
compliants_by_zone_per_year.Add(zone_key, 1);
else
compliants_by_zone_per_year[zone_key] += 1;
}
}
object oRng_fig_4 = document.Bookmarks.get_Item(ref oEndOfDoc).Range;
Microsoft.Office.Interop.Word.InlineShape wrdInlineShape_fig4 = document.InlineShapes.AddOLEObject(classtype, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, oRng_fig_4);
Thread.Sleep(3000);
if (wrdInlineShape_fig4.OLEFormat.ProgID == "Excel.Chart.8")
{
// Word doesn't keep all of its embedded objects in the running state all the time.
// In order to access the interface you first have to ensure the object is in the running state,
// ie: OLEFormat.Activate() (or something)
object verb = Microsoft.Office.Interop.Word.WdOLEVerb.wdOLEVerbHide;
wrdInlineShape_fig4.OLEFormat.DoVerb(ref verb);
Microsoft.Office.Interop.Excel.Workbook obook = (Microsoft.Office.Interop.Excel.Workbook)wrdInlineShape_fig4.OLEFormat.Object;
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)obook.Worksheets["Sheet1"];
for (int i = 1; i <= 7; i++)
{
for (int c = 1; c <= 4; c++)
{
((Microsoft.Office.Interop.Excel.Range)sheet.Cells[i, c]).Value = "";
((Microsoft.Office.Interop.Excel.Range)sheet.Cells[i, c]).Value = "";
}
}
((Microsoft.Office.Interop.Excel.Range)sheet.Cells[1, 1]).Value = "Zone name";
((Microsoft.Office.Interop.Excel.Range)sheet.Cells[1, 2]).Value = "Number of Comp.";
int cell_index = 2;
foreach (KeyValuePair<string, int> item in compliants_by_zone_per_year)
{
((Microsoft.Office.Interop.Excel.Range)sheet.Cells[cell_index, 1]).Value = Global.dictionary_zone_name_ID[item.Key];// rn.Next(10, 50);
((Microsoft.Office.Interop.Excel.Range)sheet.Cells[cell_index, 2]).Value = item.Value;
cell_index++;
}
wrdInlineShape_fig4.Width = 400;
wrdInlineShape_fig4.Height = 450;
obook.ActiveChart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlBarClustered;
obook.ActiveChart.HasTitle = true;
obook.ActiveChart.ChartTitle.Text = "Reports by Zone per for year " + unique_year_item.Key.ToString();
obook.ActiveChart.ChartTitle.Shadow = true;
obook.ActiveChart.ChartTitle.Border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
Microsoft.Office.Interop.Excel.Series oSeries;
oSeries = (Microsoft.Office.Interop.Excel.Series)obook.ActiveChart.SeriesCollection(1);
obook.ActiveChart.SetSourceData(sheet.get_Range("A:A", "B:B"));
wrdInlineShape_fig4.ConvertToShape();
document.Words.Last.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak);
}
}
foreach(uniqe\u years\u dic中KeyValuePair唯一的\u year\u项目)
{
Microsoft.Office.Interop.Word.paragration para_fig4=文档.内容.段落.添加(缺少参考);
styleHeading2=“标题2”;
第3段范围设置样式(参考样式标题2);
para_fig3.Range.Text=“每年按区域报告”+唯一的_year_item.Key.ToString();
第3段。范围。插入段落后();
字典合规性(按地区划分)每年=新字典();
对于(int i=0;i 对于(int i=1;我曾尝试调试并查看Excel是否在代码中的特定点崩溃。我认为问题不在Excel本身。上面的代码不会产生常规行为。有时我看到Excel在屏幕上运行,程序返回以下错误之一:1-(HRESULT的异常:0x80010001(RPC_E_CALL_REJECTED)2-错误HRESULT E_FAIL已从对COM组件的调用返回。这可能有助于此链接非常有用。现在我了解了问题。此外,跟踪上面链接中提到的问题,我在此处找到了更多信息:我还添加了Thread.Sleep(xxx)这就解决了问题。这不是理想的解决方案,但至少程序正在运行。