/// <summary> /// 导出Excel,以旧列名-新列名词典为标头 /// </summary> /// <param name="dt"></param> /// <param name="dicCoumnNameMapping"></param> public static void DataTableToExcel(System.Data.DataTable dt, Dictionary<string, string> dicCoumnNameMapping, string fileName) { ExcelHandler eh = new ExcelHandler(); SheetExcelForm frm = new SheetExcelForm(); eh.ProcessHandler = frm.AddProcess; eh.ProcessErrorHandler = new Action<string>((msg) => { MessageBox.Show(msg); }); try { frm.Show(); Delay(20); var ds = new System.Data.DataSet(); ds.Tables.Add(dt); eh.DataSet2Excel(ds, dicCoumnNameMapping, fileName); ds.Tables.Remove(dt); ds.Dispose(); } catch (Exception ex) { MessageBox.Show("导出Excel错误:" + ex.Message); } finally { Delay(20); frm.Close(); } }
/// <summary> /// 将DataSet导出为excel,一个Table对应一个sheet /// </summary> /// <param name="ds"></param> public static void DataSetToExcel(System.Data.DataSet ds, string fileName) { ExcelHandler eh = new ExcelHandler(); SheetExcelForm frm = new SheetExcelForm(); eh.ProcessHandler = frm.AddProcess; eh.ProcessErrorHandler = new Action <string>((msg) => { MessageBox.Show(msg); }); try { frm.Show(); Delay(20); eh.DataSet2Excel(ds, null, fileName); ds.Dispose(); } catch (Exception ex) { MessageBox.Show("导出Excel错误:" + ex.Message); } finally { Delay(20); frm.Close(); } }
/// <summary> /// 将DataSet导出为excel,一个Table对应一个sheet /// </summary> /// <param name="ds"></param> public static void DataSetToExcel(System.Data.DataSet ds, string fileName) { ExcelHandler eh = new ExcelHandler(); SheetExcelForm frm = new SheetExcelForm(); eh.ProcessHandler = frm.AddProcess; eh.ProcessErrorHandler = new Action<string>((msg) => { MessageBox.Show(msg); }); try { frm.Show(); Delay(20); eh.DataSet2Excel(ds, null, fileName); ds.Dispose(); } catch (Exception ex) { MessageBox.Show("导出Excel错误:" + ex.Message); } finally { Delay(20); frm.Close(); } }
/// <summary> /// 导出Excel,以旧列名-新列名词典为标头 /// </summary> /// <param name="dt"></param> /// <param name="dicCoumnNameMapping"></param> public static void DataTableToExcel(System.Data.DataTable dt, Dictionary <string, string> dicCoumnNameMapping, string fileName) { ExcelHandler eh = new ExcelHandler(); SheetExcelForm frm = new SheetExcelForm(); eh.ProcessHandler = frm.AddProcess; eh.ProcessErrorHandler = new Action <string>((msg) => { MessageBox.Show(msg); }); try { frm.Show(); Delay(20); var ds = new System.Data.DataSet(); ds.Tables.Add(dt); eh.DataSet2Excel(ds, dicCoumnNameMapping, fileName); ds.Tables.Remove(dt); ds.Dispose(); } catch (Exception ex) { MessageBox.Show("导出Excel错误:" + ex.Message); } finally { Delay(20); frm.Close(); } }
/// <summary> /// 按照模板导出 /// </summary> /// <param name="dt">要导出的datatable</param> /// <param name="templateFile">要导出的excel模板文件</param> /// <param name="saveFile">要保存的文件名,可以为空</param> public void DataTableToExcelWithTemplate(System.Data.DataTable dt, string templateFile, string saveFile) { ExcelHandler eh = new ExcelHandler(); try { string exportDir = "~/ExcelFile/";//注意:该文件夹您须事先在服务器上建好才行 if (System.IO.Directory.Exists(GetPhysicalPath(exportDir)) == false) { System.IO.Directory.CreateDirectory(GetPhysicalPath(exportDir)); } if (string.IsNullOrEmpty(saveFile)) saveFile = DateTime.Now.ToString("yyyyMMddHHmmssfff_") + ".xls"; //设置文件在服务器上的路径 string physicalFileName = GetPhysicalPath(System.IO.Path.Combine(exportDir, saveFile)); eh.ProcessErrorHandler = this.ErrorMessageHandler; var ds = new System.Data.DataSet(); ds.Tables.Add(dt); string tmpFile = eh.ExportWithTemplate(dt, templateFile, physicalFileName, false); ds.Tables.Remove(dt); ds.Dispose(); //==============返回客户端 ResponeToClient(physicalFileName, saveFile); try { if (!string.IsNullOrEmpty(tmpFile)) { System.IO.FileInfo fi = new System.IO.FileInfo(tmpFile); if (fi.Exists) { fi.Attributes = System.IO.FileAttributes.Normal; System.IO.File.Delete(tmpFile); } } } catch (Exception ex) { AddMessage(ex.Message); } } catch (Exception ex) { if (this.ErrorMessageHandler != null) ErrorMessageHandler("导出过程中出错," + ex.Message); if (ErrorHandler != null) ErrorHandler(ex); GC.Collect(); } }
/// <summary> /// 按照模板导出 /// </summary> /// <param name="dt">要导出的datatable</param> /// <param name="templateFile">要导出的excel模板文件</param> /// <param name="saveFile">要保存的文件名,可以为空</param> public static void DataTableToExcelWithTemplate(System.Data.DataTable dt, string templateFile, string saveFile) { ExcelHandler eh = new ExcelHandler(); SheetExcelForm frm = new SheetExcelForm(); eh.ProcessHandler = frm.AddProcess; eh.ProcessErrorHandler = new Action <string>((msg) => { MessageBox.Show(msg); }); try { frm.Show(); Delay(20); string tmpFile = eh.ExportWithTemplate(dt, templateFile, saveFile); //MessageBox.Show("导出完成"); try { if (!string.IsNullOrEmpty(saveFile)) { System.Diagnostics.Process.Start(saveFile); } } catch { } try { if (!string.IsNullOrEmpty(tmpFile)) { System.IO.File.Delete(tmpFile); } } catch { } } catch (Exception ex) { MessageBox.Show("导出Excel错误:" + ex.Message); } finally { Delay(20); frm.Close(); } }
public void DataTableToExcle(System.Data.DataTable dt, Dictionary <string, string> dicCoumnNameMapping, string fileName) { try { string exportDir = "~/ExcelFile/";//注意:该文件夹您须事先在服务器上建好才行 if (System.IO.Directory.Exists(GetPhysicalPath(exportDir)) == false) { System.IO.Directory.CreateDirectory(GetPhysicalPath(exportDir)); } if (string.IsNullOrEmpty(fileName)) { fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls"; } //设置文件在服务器上的路径 string physicalFileName = GetPhysicalPath(System.IO.Path.Combine(exportDir, fileName)); ExcelHandler eh = new ExcelHandler(); eh.ProcessErrorHandler = this.ErrorMessageHandler; var ds = new System.Data.DataSet(); ds.Tables.Add(dt); eh.DataSet2Excel(ds, dicCoumnNameMapping, physicalFileName); ds.Tables.Remove(dt); ds.Dispose(); //==============返回客户端 ResponeToClient(physicalFileName, fileName); } catch (Exception ex) { if (this.ErrorMessageHandler != null) { ErrorMessageHandler("导出过程中出错," + ex.Message); } if (ErrorHandler != null) { ErrorHandler(ex); } GC.Collect(); } }
/// <summary> /// 按照模板导出 /// </summary> /// <param name="dt">要导出的datatable</param> /// <param name="templateFile">要导出的excel模板文件</param> /// <param name="saveFile">要保存的文件名,可以为空</param> public static void DataTableToExcelWithTemplate(System.Data.DataTable dt, string templateFile, string saveFile) { ExcelHandler eh = new ExcelHandler(); SheetExcelForm frm = new SheetExcelForm(); eh.ProcessHandler = frm.AddProcess; eh.ProcessErrorHandler = new Action<string>((msg) => { MessageBox.Show(msg); }); try { frm.Show(); Delay(20); string tmpFile = eh.ExportWithTemplate(dt, templateFile, saveFile); //MessageBox.Show("导出完成"); try { if (!string.IsNullOrEmpty(saveFile)) System.Diagnostics.Process.Start(saveFile); } catch { } try { if (!string.IsNullOrEmpty(tmpFile)) System.IO.File.Delete(tmpFile); } catch { } } catch (Exception ex) { MessageBox.Show("导出Excel错误:" + ex.Message); } finally { Delay(20); frm.Close(); } }
/// <summary> /// 按照模板导出 /// </summary> /// <param name="dt">要导出的datatable</param> /// <param name="templateFile">要导出的excel模板文件</param> /// <param name="saveFile">要保存的文件名,可以为空</param> public void DataTableToExcelWithTemplate(System.Data.DataTable dt, string templateFile, string saveFile) { ExcelHandler eh = new ExcelHandler(); try { string exportDir = "~/ExcelFile/";//注意:该文件夹您须事先在服务器上建好才行 if (System.IO.Directory.Exists(GetPhysicalPath(exportDir)) == false) { System.IO.Directory.CreateDirectory(GetPhysicalPath(exportDir)); } if (string.IsNullOrEmpty(saveFile)) { saveFile = DateTime.Now.ToString("yyyyMMddHHmmssfff_") + ".xls"; } //设置文件在服务器上的路径 string physicalFileName = GetPhysicalPath(System.IO.Path.Combine(exportDir, saveFile)); eh.ProcessErrorHandler = this.ErrorMessageHandler; var ds = new System.Data.DataSet(); ds.Tables.Add(dt); string tmpFile = eh.ExportWithTemplate(dt, templateFile, physicalFileName, false); ds.Tables.Remove(dt); ds.Dispose(); //==============返回客户端 ResponeToClient(physicalFileName, saveFile); try { if (!string.IsNullOrEmpty(tmpFile)) { System.IO.FileInfo fi = new System.IO.FileInfo(tmpFile); if (fi.Exists) { fi.Attributes = System.IO.FileAttributes.Normal; System.IO.File.Delete(tmpFile); } } } catch (Exception ex) { AddMessage(ex.Message); } } catch (Exception ex) { if (this.ErrorMessageHandler != null) { ErrorMessageHandler("导出过程中出错," + ex.Message); } if (ErrorHandler != null) { ErrorHandler(ex); } GC.Collect(); } }
public void DataTableToExcle(System.Data.DataTable dt, Dictionary<string, string> dicCoumnNameMapping, string fileName) { try { string exportDir = "~/ExcelFile/";//注意:该文件夹您须事先在服务器上建好才行 if (System.IO.Directory.Exists(GetPhysicalPath(exportDir)) == false) { System.IO.Directory.CreateDirectory(GetPhysicalPath(exportDir)); } if (string.IsNullOrEmpty(fileName)) fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls"; //设置文件在服务器上的路径 string physicalFileName = GetPhysicalPath(System.IO.Path.Combine(exportDir, fileName)); ExcelHandler eh = new ExcelHandler(); eh.ProcessErrorHandler = this.ErrorMessageHandler; var ds = new System.Data.DataSet(); ds.Tables.Add(dt); eh.DataSet2Excel(ds, dicCoumnNameMapping, physicalFileName); ds.Tables.Remove(dt); ds.Dispose(); //==============返回客户端 ResponeToClient(physicalFileName, fileName); } catch (Exception ex) { if (this.ErrorMessageHandler != null) ErrorMessageHandler("导出过程中出错," + ex.Message); if (ErrorHandler != null) ErrorHandler(ex); GC.Collect(); } }