- #region (使用模版)将DataTable的数据导出显示为报表
- private DateTime beforeTime; //Excel启动之前时间
- private DateTime afterTime; //Excel启动之后时间
- /// <summary>
- /// 将DataTable的数据导出显示为报表
- /// </summary>
- /// <param name="dt">要导出的数据</param>
- /// <param name="FilePath">保存文件的路径</param>
- /// <param name="SheetsNumber">导出报表的Sheet数</param>
- /// <param name="RowIndex">导出报表的起始行</param>
- /// <param name="ColIndex">导出报表的起始列</param>
- /// <param name="TemplateFile">模版文件</param>
- /// <returns></returns>
- public string OutputExcel(Dictionary<string, System.Data.DataTable> dts, string FileName, string FilePath, int RowIndex, int ColIndex, string TemplateFile)
- {
- beforeTime = DateTime.Now;
- Excel.Application excel;
- Excel._Workbook xBk;
- Excel._Worksheet xSt = null;
- excel = new Excel.ApplicationClass();
- xBk = excel.Workbooks._Open(@TemplateFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
- Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
- int index = 0;
- if (dts.Count > xBk.Sheets.Count)
- {
- afterTime = DateTime.Now;
- return "TemplateError";
- }
- foreach (string key in dts.Keys)
- {
- index++;
- int rowIndex = RowIndex;
- xSt = (Excel.Worksheet)xBk.Worksheets.get_Item(index);
- //取得表格中的数据
- System.Data.DataTable dt = dts[key];
- foreach (DataRow row in dt.Rows)
- {
- int colIndex = ColIndex;
- rowIndex++;
- foreach (DataColumn col in dt.Columns)
- {
- colIndex++;
- if (col.DataType == System.Type.GetType("System.DateTime"))
- {
- string strDate = row[col.ColumnName].ToString();
- if (!"".Equals(strDate))
- {
- xSt.Cells[rowIndex, colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
- //xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
- }
- }
- else
- if (col.DataType == System.Type.GetType("System.String"))
- {
- xSt.Cells[rowIndex, colIndex] = row[col.ColumnName];
- //xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
- }
- else
- {
- xSt.Cells[rowIndex, colIndex] = row[col.ColumnName];
- }
- }
- }
- afterTime = DateTime.Now;
- //显示效果
- //excel.Visible = true;
- }
- //ClearFile(FilePath);
- //string filename = DateTime.Now.ToString("yyyyMMddHHmmssff") + ".xls";
- string filename = FileName + ".xls";
- //判断临时文件是否存在,如不存在,创建
- creatTempFolder(FilePath);
- excel.ActiveWorkbook.SaveAs(FilePath + filename, Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
- #region 结束Excel进程
- //需要对Excel的DCOM对象进行配置:dcomcnfg
- xBk.Close(null, null, null);
- excel.Workbooks.Close();
- excel.Quit();
- //注意:这里用到的所有Excel对象都要执行这个操作,否则结束不了Excel进程
- if (xSt != null)
- {
- System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
- xSt = null;
- }
- if (xBk != null)
- {
- System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
- xBk = null;
- }
- if (excel != null)
- {
- System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
- excel = null;
- }
- GC.Collect();//垃圾回收
- #endregion
- return filename;
- }
- #endregion
- #region Kill Excel进程
- /// <summary>
- /// 结束Excel进程
- /// </summary>
- public void KillExcelProcess()
- {
- Process[] myProcesses;
- DateTime startTime;
- myProcesses = Process.GetProcessesByName("Excel");
- //得不到Excel进程ID,暂时只能判断进程启动时间
- foreach (Process myProcess in myProcesses)
- {
- startTime = myProcess.StartTime;
- if (startTime > beforeTime && startTime < afterTime)
- {
- myProcess.Kill();
- }
- }
- }
- #endregion
excel的导出
最新推荐文章于 2023-04-27 01:00:00 发布