C#调用Excel,拷贝图表到其他Excel文档中
作者:互联网
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); excel.Visible = blExcelVisible;//设置调用引用的 Excel文件是否可见 excel.DisplayAlerts = false; Workbook workbook = excel.Workbooks.Open(strReportPath); Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[15]; Workbook workbookDetail = excel.Workbooks.Open(strPath); Microsoft.Office.Interop.Excel.Worksheet WorksheetDetail = (Microsoft.Office.Interop.Excel.Worksheet)workbookDetail.Worksheets[15]; //复制子文档的Worksheet到主文档去 WorksheetDetail.Copy(worksheet); Microsoft.Office.Interop.Excel.Worksheet worksheetChart = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets["画图"];
double left = 125; double top = 593; for (int i = 1; i < 13; i++) { //找到需要移动的图表 ChartObject chartObjectOld = (ChartObject)worksheetChart.ChartObjects("Char" + i); //left = chartObjectOld.Left; //top = chartObjectOld.Top; Chart chart = chartObjectOld.Chart; //移动Char图表 chart.Location(XlChartLocation.xlLocationAsObject, strReportName); //新图表实例化 Microsoft.Office.Interop.Excel.Worksheet worksheetChartSalt = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[strReportName]; ChartObject chartObject = (ChartObject)worksheetChartSalt.ChartObjects("Char" + i); //设置新图表位置 chartObject.Left = left; chartObject.Top = top; if (i % 2 == 0) { top = top + 175; left = left - 299; } else { left = left + 299; } } excel.DisplayAlerts = false; //删除图表 ((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets["画图"]).Delete(); excel.DisplayAlerts = true;//workbook.SaveAs(strSavePath, Type.Missing, "", "", Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, 1, false, Type.Missing, Type.Missing, Type.Missing); workbook.Save(); workbook.Close(false, System.Reflection.Missing.Value, Missing.Value); workbookDetail.Close(false, System.Reflection.Missing.Value, Missing.Value); excel.Workbooks.Close(); excel.Quit(); workbook = null; workbookDetail = null; worksheet = null; WorksheetDetail = null; worksheetChart = null; Kill(excel); GC.Collect();
标签:Office,Missing,C#,excel,Excel,Interop,文档,Microsoft 来源: https://www.cnblogs.com/weifeng123/p/13710793.html