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兼容模式下不可能用
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)