private void _btnGenerate_Click(object sender, EventArgs e) { try { if (!String.IsNullOrEmpty(this.SourcePath) && !String.IsNullOrEmpty(this.TemplatePath)) { ExcelDataExtractTemplate template = ExcelDataExtractTemplate.Load(this.TemplatePath); ExcelGraphs excelGraphs = new ExcelGraphs(); var dataCollection = excelGraphs.ExtractDataByTemplate(this.SourcePath, template); List <Byte[]> graphDataList = new List <Byte[]>(); foreach (var data in dataCollection) { Byte[] graphData = this._graphDraw.DrawGraph(data.Item1, data.Item2, data.Item3, data.Item4, null); graphDataList.Add(graphData); } excelGraphs.DrawGraphsCollection(this.SourcePath, template, graphDataList.ToArray()); Process.Start(this.SourcePath); } } catch (Exception exc) { MessageBox.Show(exc.Message); } }
/// <summary> /// 将图像数据根据指定模板,绘制到指定路径的Excel文件中 /// </summary> /// <param name="filePath">操作的文件的路径</param> /// <param name="template">模板对象</param> /// <param name="graphDatas">图像数据的集合</param> public void DrawGraphsCollection(String filePath, ExcelDataExtractTemplate template, Byte[][] graphDatas, PictureType type = DefaultPictureType) { IWorkbook workBook = this.OpenExcel(filePath); ISheet sheet = this.OpenSheet(workBook, template.SheetNum); this.DrawGraphsCollection(workBook, sheet, template, graphDatas, type); File.Delete(filePath); using (FileStream stream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite)) { workBook.Write(stream); } }
/// <summary> /// 通过指定的数据提取模板,从文件中提取数据 /// </summary> /// <param name="filePath">指定文件</param> /// <param name="template">数据提取模板</param> /// <returns>返回从Excel中提取的数据</returns> public IEnumerable <Tuple <String, String, IEnumerable <String>, IEnumerable <Double> > > ExtractDataByTemplate( String filePath, ExcelDataExtractTemplate template) { List <Tuple <String, String, IEnumerable <String>, IEnumerable <Double> > > result = new List <Tuple <String, String, IEnumerable <String>, IEnumerable <Double> > >(); IWorkbook workBook = this.OpenExcel(filePath); ISheet sheet = null; if (workBook == null) { return(result); } sheet = this.OpenSheet(workBook, template.SheetNum); foreach (ExcelGraphCategory category in template.ExcelGraphCategories) { String title = String.Empty; String unit = String.Empty; List <String> xdataList = new List <String>(); List <Double> ydataList = new List <Double>(); Int32 count = category.XData.Length; title = this.GetCellData(sheet, category.Name); if (category.Unit != null) { unit = this.GetCellData(sheet, category.Unit.Value); } for (Int32 i = 0; i < count; ++i) { Point xdataPoint = category.XData[i]; Point ydataPoint = category.YData[i]; try { String xdata = this.GetCellData(sheet, xdataPoint); Double ydata = this.GetCellDataDouble(sheet, ydataPoint); xdataList.Add(xdata); ydataList.Add(ydata); } catch (Exception exc) { throw new Exception(ErrorOfCannotGetCellData, exc); } } result.Add(Tuple.Create(title, unit, xdataList as IEnumerable <String>, ydataList as IEnumerable <Double>)); } return(result); }
/// <summary> /// 将一组图像数据绘制到指定的工作表中 /// </summary> /// <param name="workBook">指定的工作簿对象</param> /// <param name="sheet">指定的工作表对象</param> /// <param name="template">数据提取模板对象</param> /// <param name="graphDatas">图表数据集合</param> public void DrawGraphsCollection(IWorkbook workBook, ISheet sheet, ExcelDataExtractTemplate template, Byte[][] graphDatas, PictureType type) { Int32 beginX = template.StartPoint.X; Int32 beginY = template.StartPoint.Y == (-1) ? sheet.LastRowNum + this._defaultOffsetRow : template.StartPoint.Y; Int32 graphWidth = template.Width; Int32 graphHeight = template.Height; Int32 graphInterval = template.IntervalWithMulti; Int32 count = graphDatas.Length; for (Int32 i = 0; i < count; ++i) { Point point = new Point(beginX, beginY); Byte[] graphData = graphDatas[i]; this.DrawGraphs(workBook, sheet, point, graphWidth, graphHeight, graphData, type); beginY += graphHeight + graphInterval; } }