C#从DataView中生成Excel报表的方案.doc_第1页
C#从DataView中生成Excel报表的方案.doc_第2页
C#从DataView中生成Excel报表的方案.doc_第3页
C#从DataView中生成Excel报表的方案.doc_第4页
C#从DataView中生成Excel报表的方案.doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

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

文档简介

一、首先要引用一个Excel的组件,我一开始是在Office XP下尝试的,不 成功,后来把XP给干掉,装2k,就成功了,所以这里分享的是Office 2k下 引用相关组件来实现功能的,在工程中引用标签中的Microsoft Excel 9.0 Object Library,添加成功后,引用中会多出三个引用项: Excel、Office、VBIDE。 二、具体代码。 using System; using System.Data; using Excel; using System.IO; namespace Test.ExcelCom / / 将DataView中的数据导入Excel文件中 / 作者:Rexsp / 创建:2004-4-4 / public class OutputExcel #region 私有成员 / / 数据的DataView / private DataView dv=null; / / 表格标题 / private string title=null; / / 输出文件路径 / private string outFilePath=null; / / 输入文件名 / private string inputFilePath=null; #endregion #region 公共属性 / / 数据的DataView / public DataView DV setdv=value; / / 表格标题 / public string Title settitle=value; getreturn title; / / 输出文件路径 / public string OutFilePath setoutFilePath=value; getreturn outFilePath; / / 输入文件路径 / public string InputFilePath setinputFilePath=value; getreturn inputFilePath; #endregion #region 构造函数 public OutputExcel() public OutputExcel(DataView dv,string title) / / TODO: 在此处添加构造函数逻辑 / #endregion #region 公共方法 public void CreateExcel() int rowIndex=4;/行起始坐标 int colIndex=1;/列起始坐标 ApplicationClass myApp=null; Workbook myBook=null; Worksheet mySheet=null; /如果文件不存在,则将模板文件拷贝一份作为输出文件 /这里如果通过File.Create来创建文件是不行的,因为xls /的空文件也有固定的格式,跟文本不一样的,也许有其它 /通过程序直接生成excel的方法,大家可以尝试尝试的 if(!File.Exists(outFilePath) File.Copy(inputFilePath,outFilePath,true); myApp= new ApplicationClass(); myApp.Visible=false; object oMissiong=System.Reflection.Missing.Value; myApp.Workbooks.Open(outFilePath,oMissiong,oMissiong,oMissiong,oMissiong, oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong); myBook=myApp.Workbooks1; mySheet=(Worksheet)myBook.ActiveSheet; / /取得标题 / foreach(DataColumn col in dv.Table.Columns) colIndex+; mySheet.Cells4,colIndex = col.ColumnName; mySheet.get_Range(mySheet.Cells4,colIndex,mySheet.Cells4,colIndex).HorizontalAlignment = XlVAlign.xlVAlignCenter; /设置标题格式为居中对齐 / /取得表格中的数据 / foreach(DataRowView row in dv) rowIndex +; colIndex = 1; foreach(DataColumn col in dv.Table.Columns) colIndex +; if(col.DataType = System.Type.GetType(System.DateTime) mySheet.CellsrowIndex,colIndex = (Convert.ToDateTime(rowcol.ColumnName.ToString().ToString(yyyy-MM-dd); mySheet.get_Range(mySheet.CellsrowIndex,colIndex,mySheet.CellsrowIndex,colIndex).HorizontalAlignment = XlVAlign.xlVAlignCenter;/设置日期型的字段格式为居中对齐 else if(col.DataType = System.Type.GetType(System.String) mySheet.CellsrowIndex,colIndex = +rowcol.ColumnName.ToString(); mySheet.get_Range(mySheet.CellsrowIndex,colIndex,mySheet.CellsrowIndex,colIndex).HorizontalAlignment = XlVAlign.xlVAlignCenter;/设置字符型的字段格式为居中对齐 else mySheet.CellsrowIndex,colIndex = rowcol.ColumnName.ToString(); / /加载一个合计行 / int rowSum = rowIndex + 1; int colSum = 2; mySheet.CellsrowSum,2 = 合计; mySheet.get_Range(mySheet.CellsrowSum,2,mySheet.CellsrowSum,2).HorizontalAlignment = XlHAlign.xlHAlignCenter; / /设置选中的部分的颜色 / mySheet.get_Range(mySheet.CellsrowSum,colSum,mySheet.CellsrowSum,colIndex).Select(); mySheet.get_Range(mySheet.CellsrowSum,colSum,mySheet.CellsrowSum,colIndex).Interior.ColorIndex = 19;/设置为浅黄色,共计有56种 / /取得整个报表的标题 / mySheet.Cells2,2 = title; / /设置整个报表的标题格式 / mySheet.get_Range(mySheet.Cells2,2,mySheet.Cells2,2).Font.Bold = true; mySheet.get_Range(mySheet.Cells2,2,mySheet.Cells2,2).Font.Size = 22; / /设置报表表格为最适应宽度 / mySheet.get_Range(mySheet.Cells4,2,mySheet.CellsrowSum,colIndex).Select(); mySheet.get_Range(mySheet.Cells4,2,mySheet.CellsrowSum,colIndex).Columns.AutoFit(); / /设置整个报表的标题为跨列居中 / mySheet.get_Range(mySheet.Cells2,2,mySheet.Cells2,colIndex).Select(); mySheet.get_Range(mySheet.Cells2,2,mySheet.Cells2,colIndex).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection; / /绘制边框 / mySheet.get_Range(mySheet.Cells4,2,mySheet.CellsrowSum,colIndex).Borders.LineStyle = 1; mySheet.get_Range(mySheet.Cells4,2,mySheet.CellsrowSum,2).BordersXlBordersIndex.xlEdgeLeft.Weight = XlBorderWeight.xlThick;/设置左边线加粗 mySheet.get_Range(mySheet.Cells4,2,mySheet.Cells4,colIndex).BordersXlBordersIndex.xlEdgeTop.Weight = XlBorderWeight.xlThick;/设置上边线加粗 mySheet.get_Range(mySheet.Cells4,colIndex,mySheet.CellsrowSum,colIndex).BordersXlBordersIndex.xlEdgeRight.Weight = XlBorderWeight.xlThick;/设置右边线加粗 mySheet.get_Range(mySheet.CellsrowSum,2,mySheet.CellsrowSum,colIndex).BordersXlBordersIndex.xlEdgeBottom.Weight = XlBorderWeight.xlThick;/设置下边线加粗 myBook.Save(); myBook.Close( true,outFilePath,true); System.Runtime.InteropServices.Marshal.ReleaseComObject(mySheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook); System.Runtime.InteropServices.Marshal.ReleaseComObject(myApp); GC.Collect(); #endregion 一点说明:操作Excel的时候,可能会发生Excel进程被锁定,无法退 出,解决方法是在保存完并关闭myBook(工作簿)后,别关闭Excel进 程(/myApp.Quit();)。这样的结果是服务器上始终有一个Excel的 进程。可能会出现asp_net用户操作Excel的权限不够,配置Dcom。运 行Dcomcnfg.exe,找到Excel应用程序,配置其属性,身份验证级别 选无,身份标识选交互式用户,安全性页面,启动和访问均给 everyone。注意:查看当前进程中是否有Winword进程存在,如果有且 不能被结束,那么重启动计算机。再次运行你的代码即OK。这样以后 就不会出现权限不够的情况了。 三、调用 #region 测试Excel QuickItemCollection qic =new QuickItemCollection(); qic.GetAllInfo(); DataView dv= new DataView(); DataTable dt = new DataTable(Excel); dt.Columns.Add(ID,System.Type.GetType(System.String); dt.Columns.Add(ItemName,System.Type.GetType(System.String); int qicCount=qic.Count; for(int i=0;iqicCount;i+) Dat

温馨提示

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

最新文档

评论

0/150

提交评论