C#操作EXCEL.doc_第1页
C#操作EXCEL.doc_第2页
C#操作EXCEL.doc_第3页
C#操作EXCEL.doc_第4页
C#操作EXCEL.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

关于C#操作EXCEL,生成图表的全面应用之一(利用Microsoft.Office.Interop.Excel)2008-09-19 10:124893人阅读评论(14)收藏举报近来我在开发一个运用C#生成EXCEL文档的程序,其中要根据数据生成相应的图表,该图表对颜色和格式都有严格的要求,在百度和谷歌中搜索了所有的相关信息,只有部分介绍,具体格式的介绍没有,经过我不断的实践和探索,终于完成了这项艰巨的任务。 有两种实现方式,一种是利用OWC11组件完成,一种运用Excel完成! 运用OWC11的完成,适合生成一个图形文件,之后不能不在文件中编辑;运用Excel则更适合利用EXCEL文件中的数据直接在文件中加入图表,方便以后编辑!两种我都尝试了,由于我更适合于使用第二种,所以我开发的较完善。在这里公布源码供大家参考使用! 开发者:Gailzhao EMail:说明,在窗口,添加一个按钮,名称为Btn,我只写代码部分(我的数据中是日期对应的两种数据,如下表)在EXCEL文档中生成如下数据(部分),在数据右侧根据数据生成图表基金净值指数图表日期开放式基金偏股型基金2008-1-24236.99445158.04562008-1-34246.88615172.52882008-1-44277.93345214.28672008-1-74325.52525276.84322008-1-84306.32725252.3962 添加以下引用:using System.IO;using System.Runtime.InteropServices;using Excel = Microsoft.Office.Interop.Excel; private void Btn_Click(object sender, EventArgs e) /创建一个EXCEL文档 CreateExcel(标题,文档.XLS, 工作表名);private void CreateExcel(string title, string fileName, string sheetNames) /待生成的文件名称 string FileName = fileName; string FilePath = strCurrentPath + FileName; FileInfo fi = new FileInfo(FilePath); if (fi.Exists) /判断文件是否已经存在,如果存在就删除! fi.Delete(); if (sheetNames != null & sheetNames != ) Excel.Application m_Excel = new Excel.Application();/创建一个Excel对象(同时启动EXCEL.EXE进程) m_Excel.SheetsInNewWorkbook = 1;/工作表的个数 Excel._Workbook m_Book = (Excel._Workbook)(m_Excel.Workbooks.Add(Missing.Value);/添加新工作簿 Excel._Worksheet m_Sheet; #region 处理 DataSet ds= ScData.ListData(exec Vote_2008.dbo.P_VoteResult_Update + int.Parse(fdate); if (ds.Tables.Count= 0) MessageBox.Show(没有最新数据!); return; DataTableToSheet(title, ds.Tables0, m_Sheet, m_Book, 0); #endregion #region 保存Excel,清除进程 m_Book.SaveAs(FilePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); /m_Excel.ActiveWorkbook._SaveAs(FilePath, Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null); m_Book.Close(false, Missing.Value, Missing.Value); m_Excel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Book); System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Excel); m_Book = null; m_Sheet = null; m_Excel = null; GC.Collect(); /this.Close();/关闭窗体 #endregion #region 将DataTable中的数据写到Excel的指定Sheet中 / / 将DataTable中的数据写到Excel的指定Sheet中 / / / public void DataTableToSheet(string title, DataTable dt, Excel._Worksheet m_Sheet, Excel._Workbook m_Book, int startrow) /以下是填写EXCEL中数据 Excel.Range range = m_Sheet.get_Range(m_Sheet.Cells1, 1, m_Sheet.Cells1, 2); range.MergeCells = true; /合并单元格 range.Font.Bold = true; /加粗单元格内字符 /写入题目 m_Sheet.Cellsstartrow, startrow = title; int rownum = dt.Rows.Count;/行数 int columnnum = dt.Columns.Count;/列数 int num = rownum + 2; /得到数据中的最大行数 /写入列标题 for (int j = 0; j columnnum; j+) int bt_startrow = startrow + 1; /将字段名写入文档 m_Sheet.Cellsbt_startrow, 1 + j = dt.Columnsj.ColumnName; /单元格内背景色 m_Sheet.get_Range(m_Sheet.Cellsbt_startrow, 1 + j, m_Sheet.Cellsbt_startrow, 1 + j).Interior.ColorIndex = 15; /逐行写入数据 for (int i = 0; i rownum; i+) for (int j = 0; j columnnum; j+) m_Sheet.Cellsstartrow + 2 + i, 1 + j = dt.Rowsij.ToString(); m_Sheet.Columns.AutoFit(); /在当前工作表中根据数据生成图表 CreateChart(m_Book, m_Sheet, num); private void CreateChart(Excel._Workbook m_Book,Excel._Worksheet m_Sheet,int num) Excel.Range oResizeRange; Excel.Series oSeries; m_Book.Charts.Add(Missing.Value, Missing.Value, 1, Missing.Value); m_Book.ActiveChart.ChartType = Excel.XlChartType.xlLine;/设置图形 /设置数据取值范围 m_Book.ActiveChart.SetSourceData(m_Sheet.get_Range(A2, C + num.ToString(), Excel.XlRowCol.xlColumns); /m_Book.ActiveChart.Location(Excel.XlChartLocation.xlLocationAutomatic, title); /以下是给图表放在指定位置 m_Book.ActiveChart.Location(Excel.XlChartLocation.xlLocationAsObject, m_Sheet.Name); oResizeRange = (Excel.Range)m_Sheet.Rows.get_Item(10, Missing.Value); m_Sheet.Shapes.Item(Chart 1).Top = (float)(double)oResizeRange.Top; /调图表的位置上边距 oResizeRange = (Excel.Range)m_Sheet.Columns.get_Item(6, Missing.Value); /调图表的位置左边距 / m_Sheet.Shapes.Item(Chart 1).Left = (float)(double)oResizeRange.Left; m_Sheet.Shapes.Item(Chart 1).Width = 400; /调图表的宽度 m_Sheet.Shapes.Item(Chart 1).Height = 250; /调图表的高度 m_Book.ActiveChart.PlotArea.Interior.ColorIndex = 19; /设置绘图区的背景色 m_Book.ActiveChart.PlotArea.Border.LineStyle = Excel.XlLineStyle.xlLineStyleNone;/设置绘图区边框线条 m_Book.ActiveChart.PlotArea.Width = 400; /设置绘图区宽度 /m_Book.ActiveChart.ChartArea.Interior.ColorIndex = 10; /设置整个图表的背影颜色 /m_Book.ActiveChart.ChartArea.Border.ColorIndex = 8;/ 设置整个图表的边框颜色 m_Book.ActiveChart.ChartArea.Border.LineStyle = Excel.XlLineStyle.xlLineStyleNone;/设置边框线条 m_Book.ActiveChart.HasDataTable = false; /设置Legend图例的位置和格式 m_Book.ActiveChart.Legend.Top = 20.00; /具体设置图例的上边距 m_Book.ActiveChart.Legend.Left = 60.00;/具体设置图例的左边距 m_Book.ActiveChart.Legend.Interior.ColorIndex = Excel.XlColorIndex.xlColorIndexNone; m_Book.ActiveChart.Legend.Width = 150; m_Book.ActiveChart.Legend.Font.Size = 9.5; /m_Book.ActiveChart.Legend.Font.Bold = true; m_Book.ActiveChart.Legend.Font.Name = 宋体; /m_Book.ActiveChart.Legend.Position = Excel.XlLegendPosition.xlLegendPositionTop;/设置图例的位置 m_Book.ActiveChart.Legend.Border.LineStyle = Excel.XlLineStyle.xlLineStyleNone;/设置图例边框线条 /设置X轴的显示 Excel.Axis xAxis = (Excel.Axis)m_Book.ActiveChart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary); xAxis.MajorGridlines.Border.LineStyle = Excel.XlLineStyle.xlDot; xAxis.MajorGridlines.Border.ColorIndex = 1;/gridLine横向线条的颜色 xAxis.HasTitle = false; xAxis.MinimumScale = 1500; xAxis.MaximumScale = 6000; xAxis.TickLabels.Font.Name = 宋体; xAxis.TickLabels.Font.Size = 9; /设置Y轴的显示 Excel.Axis yAxis = (Excel.Axis)m_Book.ActiveChart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary); yAxis.TickLabelSpacing = 30; yAxis.TickLabels.NumberFormat = M月D日; yAxis.TickLabels.Orientation = Excel.XlTickLabelOrientation.xlTickLabelOrientationHorizontal;/Y轴显示的方向,是水平还是垂直等 yAxis.TickLabels.Font.Size = 8; yAxis.TickLabels.Font.Name = 宋体; /m_Book.ActiveChart.Floor.Interior.ColorIndex = 8; /*以下是设置标题* m_Book.ActiveChart.HasTitle=true; m_Book.ActiveChart.ChartTitle.Text = 净值指数; m_Book.ActiveChart.ChartTitle.Shadow = true; m_Book.ActiveChart.ChartTitle.Border.LineStyle = Excel.XlLineStyle.xlContinuous; */ oSeries = (Excel.Series)m_Book.ActiveChart.SeriesCollection(1); oSeries.Border.ColorIndex = 45; oSeries.Border.Weight = Excel.XlBorderWeight.xlThick; oSeries = (Excel.Series)m_Book.ActiveChart.SeriesCollection(2); oSeries.Border.ColorIndex = 9; oSeries.Border.Weight = Excel.XlBorderWeight.xlThick; 分享到: 下一篇:关于C#操作EXCEL,生成图表的全面应用之二(利用Microsoft.Office.Interop.OWC11)查看评论14楼 jusons 2012-02-17 16:24发表 回复 188310956 能发我一份源代码吗?13楼 wanghao_china 2011-11-21 15:34发表 回复 #region 处理DataSet ds= ScData.ListData(exec Vote_2008.dbo.P_VoteResult_Update + int.Parse(fdate);if (ds.Tables.Count= 0)MessageBox.Show(没有最新数据!);return;DataTableToSheet(titl

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论