excel的导出

  1. #region (使用模版)将DataTable的数据导出显示为报表
  2.     private DateTime beforeTime;            //Excel启动之前时间
  3.         private DateTime afterTime;             //Excel启动之后时间
  4.         /// <summary>
  5.         /// 将DataTable的数据导出显示为报表
  6.         /// </summary>
  7.         /// <param name="dt">要导出的数据</param>
  8.         /// <param name="FilePath">保存文件的路径</param>
  9.         /// <param name="SheetsNumber">导出报表的Sheet数</param>
  10.         /// <param name="RowIndex">导出报表的起始行</param>
  11.         /// <param name="ColIndex">导出报表的起始列</param>
  12.         /// <param name="TemplateFile">模版文件</param>
  13.         /// <returns></returns>
  14.         public string OutputExcel(Dictionary<string, System.Data.DataTable> dts, string FileName, string FilePath, int RowIndex, int ColIndex, string TemplateFile)
  15.         {
  16.             beforeTime = DateTime.Now;
  17.             Excel.Application excel;
  18.             Excel._Workbook xBk;
  19.             Excel._Worksheet xSt = null;
  20.             excel = new Excel.ApplicationClass();
  21.             xBk = excel.Workbooks._Open(@TemplateFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
  22.                 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  23.             int index = 0;
  24.             if (dts.Count > xBk.Sheets.Count)
  25.             {
  26.                 afterTime = DateTime.Now;
  27.                 return "TemplateError";
  28.             }
  29.             foreach (string key in dts.Keys)
  30.             {
  31.                 index++;
  32.                 int rowIndex = RowIndex;
  33.                 xSt = (Excel.Worksheet)xBk.Worksheets.get_Item(index);
  34.                 //取得表格中的数据
  35.                 System.Data.DataTable dt = dts[key];                
  36.                 foreach (DataRow row in dt.Rows)
  37.                 {
  38.                     int colIndex = ColIndex;
  39.                     rowIndex++;
  40.                     foreach (DataColumn col in dt.Columns)
  41.                     {
  42.                         colIndex++;
  43.                         if (col.DataType == System.Type.GetType("System.DateTime"))
  44.                         {
  45.                             string strDate = row[col.ColumnName].ToString();
  46.                             if (!"".Equals(strDate))
  47.                             {
  48.                                 xSt.Cells[rowIndex, colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
  49.                                 //xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
  50.                             }
  51.                         }
  52.                         else
  53.                             if (col.DataType == System.Type.GetType("System.String"))
  54.                             {
  55.                                 xSt.Cells[rowIndex, colIndex] = row[col.ColumnName];
  56.                                 //xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
  57.                             }
  58.                             else
  59.                             {
  60.                                 xSt.Cells[rowIndex, colIndex] = row[col.ColumnName];
  61.                             }
  62.                     }
  63.                 }
  64.                 afterTime = DateTime.Now;
  65.                 //显示效果
  66.                 //excel.Visible = true;                
  67.             }
  68.             //ClearFile(FilePath);
  69.             //string filename = DateTime.Now.ToString("yyyyMMddHHmmssff") + ".xls";
  70.             string filename = FileName + ".xls";
  71.             //判断临时文件是否存在,如不存在,创建
  72.             creatTempFolder(FilePath);
  73.             excel.ActiveWorkbook.SaveAs(FilePath + filename, Excel.XlFileFormat.xlExcel9795, nullnullfalsefalse, Excel.XlSaveAsAccessMode.xlNoChange, nullnullnullnullnull);
  74.             
  75.             #region  结束Excel进程
  76.             //需要对Excel的DCOM对象进行配置:dcomcnfg
  77.             
  78.             xBk.Close(nullnullnull);
  79.             excel.Workbooks.Close();
  80.             excel.Quit();
  81.             //注意:这里用到的所有Excel对象都要执行这个操作,否则结束不了Excel进程            
  82.             if (xSt != null)
  83.             {
  84.                 System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
  85.                 xSt = null;
  86.             }
  87.             if (xBk != null)
  88.             {
  89.                 System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
  90.                 xBk = null;
  91.             }
  92.             if (excel != null)
  93.             {
  94.                 System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
  95.                 excel = null;
  96.             }
  97.             GC.Collect();//垃圾回收
  98.             
  99.             #endregion
  100.                         
  101.             return filename;
  102.         }
  103.         #endregion
  104.         
  105.         #region Kill Excel进程
  106.         /// <summary>
  107.         /// 结束Excel进程
  108.         /// </summary>
  109.         public void KillExcelProcess()
  110.         {
  111.             Process[] myProcesses;
  112.             DateTime startTime;
  113.             myProcesses = Process.GetProcessesByName("Excel");
  114.             //得不到Excel进程ID,暂时只能判断进程启动时间
  115.             foreach (Process myProcess in myProcesses)
  116.             {
  117.                 startTime = myProcess.StartTime;
  118.                 if (startTime > beforeTime && startTime < afterTime)
  119.                 {
  120.                     myProcess.Kill();
  121.                 }
  122.             }
  123.         }
  124.         #endregion
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值