1. 服务端输出方式 1.1 html 输出方式最常用方式,我来为大家科普一下关于如何在excel导出文件?下面希望有你要的答案,我们一起来看看吧!

如何在excel导出文件(导出Excel的四种方式)

如何在excel导出文件

1. 服务端输出方式

1.1 html 输出方式最常用方式

优点:不需要任何插件

缺点:不是真正excel文件,做不了太复杂的格式,偶尔会出现打不开的情况

代码示例

Response.Clear(); Response.Buffer = true; Response.Charset = "GB2312"; //设置了类型为中文防止乱码的出现 string danwei = Session["danWeiMingCheng"].ToString(); //2011.07.14 luochengxin Response.AddHeader("Content-Disposition", "inline;filename=手术登记查询.xls", System.Text.Encoding.UTF8)); Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); //设置输出流为简体中文 Response.ContentType = "application/ms-excel"; //设置输出文件类型为excel文件。 System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true); System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); Response.Write(Sbr.ToString()); Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>"); Response.Write(oStringWriter.ToString()); Response.End();

1.2 office com组件 输出方式

优点:生成真正的Excel文件,强类型编码

缺点:服务器需要安装office且与版本绑定太深(不推荐使用),常用cs类型程序开发

代码示例:

protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { ApplicationClass app = new ApplicationClass(); Workbook workBook = app.Workbooks.Open(@"e:\tt.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); Worksheet sheet = (Worksheet)workBook.Worksheets[1]; Response.Write(sheet.Name "<br/>"); Range range = sheet.get_Range("E7:J112", Type.Missing); Range item = null; for (int row = 0; row < range.Rows.Count; row ) { for (int col = 0; col < range.Columns.Count; col ) { item = (Range)range.get_Item(row, col); Response.Write(item.Value2); Response.Write(" | "); } Response.Write("<br/>"); } //关闭 app.Workbooks.Close(); app.Quit(); //释放 System.Runtime.InteropServices.Marshal.ReleaseComObject(item); System.Runtime.InteropServices.Marshal.ReleaseComObject(range); System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook); System.Runtime.InteropServices.Marshal.ReleaseComObject(app); item = null; app = null; workBook = null; range = null; GC.Collect(); //杀进程 try{ KillExcelProcess(); }catch{} } } public void KillExcelProcess() { Process[] myProcesses; myProcesses = Process.GetProcessesByName("Excel"); foreach (Process myProcess in myProcesses) { myProcess.Kill(); } }

1.3 NPOI 输出方式

优点:生成真正的Excel文件,可做特殊复杂操作,且为独立第三方DLL,不需要依然office组件解决了1.2的问题,

缺点:生成时占用内存资源大,CPU耗时大,如果生成一万条以上excel极其容易出现内存溢出

代码示例:

private void button1_Click(object sender, EventArgs e) { //创建工作薄 HSSFWorkbook wk = new HSSFWorkbook(); //创建一个名称为mySheet的表 ISheet tb = wk.CreateSheet("mySheet"); //创建一行,此行为第二行 IRow row = tb.CreateRow(1); for (int i = 0; i < 20; i ) { ICell cell = row.CreateCell(i); //在第二行中创建单元格 cell.SetCellValue(i);//循环往第二行的单元格中添加数据 } //打开一个xls文件,如果没有则自行创建,如果存在myxls.xls文件则在创建是不要打开该文件! using (FileStream fs = File.OpenWrite(@"c:/myxls.xls")) { wk.Write(fs); //向打开的这个xls文件中写入mySheet表并保存。 MessageBox.Show("提示:创建成功!"); } }

2. 客户端输出方式

2.1 OCX组件导出

优点:生成真正的Excel文件

缺点:仅IE下可用,需要开启activeX权限

代码示例:

function AllAreaExcel(content) { var oXL = new ActiveXObject("Excel.Application"); var oWB = oXL.Workbooks.Add(); var oSheet = oWB.ActiveSheet; var div_all = content; var sel = document.body.createTextRange(); sel.moveToElementText(div_all); sel.select(); sel.execCommand("Copy"); oSheet.Paste(); oXL.Visible = true; }

2.2 第三方JS插件

优点

缺点:仅标准H5可用,IE兼容模式下不可能用

可参考:https://www.cnblogs.com/liuxianan/p/js-excel.html

,