private void AddBugChart( ExcelInterop.Worksheet sheet, int chartStartRow, string chartStartCol, int chartEndRow, string chartEndCol, string bugDataSource, string chartTitle) { ExcelInterop.Range bugChartRange = sheet.Range[sheet.Cells[chartStartRow, chartStartCol], sheet.Cells[chartEndRow, chartEndCol]]; ExcelInterop.ChartObjects charts = sheet.ChartObjects(Type.Missing) as ExcelInterop.ChartObjects; Utility.AddNativieResource(charts); ExcelInterop.ChartObject bugChartObject = charts.Add(0, 0, bugChartRange.Width, bugChartRange.Height); Utility.AddNativieResource(bugChartObject); ExcelInterop.Chart bugChart = bugChartObject.Chart;//设置图表数据区域。 Utility.AddNativieResource(bugChart); ExcelInterop.Range datasource = sheet.get_Range(bugDataSource);//不是:"B14:B25","F14:F25" Utility.AddNativieResource(datasource); bugChart.SetSourceData(datasource); bugChart.ChartType = ExcelInterop.XlChartType.xlColumnClustered; //bugChart.ChartWizard(datasource, XlChartType.xlColumnClustered, Type.Missing, XlRowCol.xlColumns, 1, 1, false, chartTitle, "", "", Type.Missing); bugChart.ApplyDataLabels();//图形上面显示具体的值 //将图表移到数据区域之下。 bugChartObject.Left = Convert.ToDouble(bugChartRange.Left) + 20; bugChartObject.Top = Convert.ToDouble(bugChartRange.Top) + 20; bugChartObject.Locked = false; bugChartObject.Select(); bugChartObject.Activate(); }
/// <summary> /// 导出excel中的chart对象到word中 /// </summary> /// <param name="cht">excel中的chart对象</param> /// <param name="range">此时word文档中的全局range的位置或者范围</param> /// <remarks>由局部安全的原则,在进行绘图前,将另起一行,并将段落样式设置为“图片”样式</remarks> private void Export_ExcelChart(Chart cht, ref Word.Range range) { cht.Application.ScreenUpdating = false; try { // 下面复制Chart的操作中,如果监测曲线图所使用的Chart模板有问题,则可能会出错。 Excel.ChartObject chtObj = cht.Parent as Excel.ChartObject; chtObj.Activate(); chtObj.Copy(); // 或者用 cht.ChartArea.Copy()都可以。 } catch (Exception ex) { MessageBox.Show( "导出监测曲线图\"" + cht.Application.Caption.ToString() + "\"出错(请检查是否是用户使用的Chart模板有问题),跳过此图的导出。" + "\r\n" + ex.Message + "\r\n" + "报错位置:" + ex.TargetSite.Name, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); //刷新excel屏幕 cht.Application.ScreenUpdating = true; return; } //设置word.range的格式 Word.Range with_1 = range; //新起一行,并设置新段落的段落样式为图片 NewLine(range, ParagraphStyle.picture); //进行粘贴,下面也可以用:DataType:=23 with_1.PasteSpecial(DataType: Word.WdPasteDataType.wdPasteOLEObject, Placement: Word.WdOLEPlacement.wdInLine); Word.InlineShape shp = default(Word.InlineShape); range.Select(); range.Application.Selection.MoveLeft(Unit: Word.WdUnits.wdCharacter, Count: 1, Extend: Word.WdMovementType.wdExtend); shp = range.Application.Selection.InlineShapes[1]; //约束图形的宽度,将其限制在word页面的正文宽度之内 WidthRestrain(shp, ContentWidth); //刷新excel屏幕 cht.Application.ScreenUpdating = true; }
public void ExportChartsAsOneImage(string item, string imageFile, int imagesPerRow, int splitBoardWidth, Color splitBoarderColor) { try { if (string.IsNullOrEmpty(item) || string.IsNullOrEmpty(imageFile)) { throw new ArgumentNullException($"The parameter item or imageFile is null"); } Excel.Worksheet xlWorkSheet = (Excel.Worksheet)_xlWorkBook.Worksheets.Item[item]; Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing); IList <string> chartFiles = new List <string>(); string folder = Path.GetDirectoryName(imageFile); if (folder == null) { throw new ArgumentNullException($"Image file should be specified the full path"); } string format = Path.GetExtension(imageFile).TrimStart('.'); for (int j = 1; j <= xlCharts.Count; j++) { var chartFile = Path.Combine(folder, $"Temp Chart {j}.{format}"); Excel.ChartObject chart = (Excel.ChartObject)xlCharts.Item(j); chart.Activate(); chart.Chart.Export(chartFile, format, true); chartFiles.Add(chartFile); TempCreatedFiles.Add(chartFile); } var chartImage = chartFiles.FirstOrDefault(); if (chartImage == null) { throw new ArgumentNullException($"No chart found"); } var rows = (int)Math.Ceiling(chartFiles.Count / (decimal)imagesPerRow); var imageBitmap = new Bitmap(chartImage); var combinedImageWidth = imageBitmap.Width * imagesPerRow + (imagesPerRow - 1) * splitBoardWidth; var combinedImageHeight = imageBitmap.Height * rows + (imagesPerRow - 1) * splitBoardWidth; var combinedImage = new Bitmap(combinedImageWidth, combinedImageHeight); // copy images row by row int combinedImageY = 0; for (int row = 0; row < rows; row++) { // copy images one after one in one row for (int colomn = 0; colomn < imagesPerRow; colomn++) { int imageIndex = colomn + row * imagesPerRow; var image = new Bitmap(chartFiles[imageIndex]); var combinedImageX = (imageBitmap.Width + splitBoardWidth) * colomn; for (int y = 0; y < image.Height; y++) { // copy image for (int x = 0; x < image.Width; x++) { combinedImage.SetPixel(combinedImageX + x, combinedImageY + y, image.GetPixel(x, y)); } // draw vertical boarder for (int i = 0; i < splitBoardWidth; i++) { var boarderX = combinedImageX + imageBitmap.Width + i; if (boarderX < combinedImageWidth) { combinedImage.SetPixel(boarderX, combinedImageY + y, splitBoarderColor); } } } } combinedImageY += imageBitmap.Height; // draw horizon boarder for (int i = 0; i < splitBoardWidth; i++) { if (combinedImageY < combinedImageHeight) { for (int x = 0; x < combinedImageWidth; x++) { combinedImage.SetPixel(x, combinedImageY, splitBoarderColor); } combinedImageY++; } } } combinedImage.Save(imageFile); } finally { _xlWorkBook.Close(true, _misValue, _misValue); } }