C#、javaScript 导出Excel的几种方法!

来源:互联网 发布:云服务器php环境搭建 编辑:程序博客网 时间:2024/06/01 20:03
第一种:将页面的Table导出Excel(javaScript)

    //导出Excel    function excelPrint() {        var date = new Date();        var filename = document.title + '-' + date.toLocaleDateString().toString() + '.xls';        var tempStr = document.getElementById('strtable').outerHTML;        var newWin = window.open();        newWin.document.write(tempStr);        newWin.document.close();        newWin.document.execCommand('saveas', true, filename);        newWin.window.close();    }

 

第二种:将DataGrid或GridView导出Excel

        //导出Excel        public void ToExcel(DataGrid dg)        {            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + this.Title + "-" + DateTime.Now.ToShortDateString() + ".xls");  //文件名            HttpContext.Current.Response.Charset = "UTF-8";            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;            HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword             dg.Page.EnableViewState = false;  //是否导出全部            System.IO.StringWriter tw = new System.IO.StringWriter();            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);            dg.RenderControl(hw);            HttpContext.Current.Response.Write(tw.ToString());            HttpContext.Current.Response.End();        }

 

第三种:拼语句,对某个工作表一行一行地写进去

 

        //导出Excel        public void ExportExcle()        {            Aspose.Excel.Excel xlDoc = new Aspose.Excel.Excel();  //需要引用Aspose.Excel.dll,在我的资源有得下载

string filename = "业务数据报表" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";//文件名 + DateTime.Now.ToString("yyyyMMddHHmmss") string filepath = HttpContext.Current.Server.MapPath(Request.ApplicationPath) + @"\DownloadFile\" + filename;//放置文件地 try { string fileTem = HttpContext.Current.Server.MapPath(Request.ApplicationPath) + @"\ExcelTemplate\流程业务数据模板.xls"; xlDoc.Open(fileTem); //打开模板 Aspose.Excel.Worksheet xSt; System.Data.DataTable dt = null; string AppId = ""; for (int i = 1; i <= 10; i++) { switch (i) { case 1: AppId = "202"; break; case 2: AppId = "203"; break; case 3: AppId = "204"; break; case 4: AppId = "206"; break; case 5: AppId = "209"; break; case 6: AppId = "210"; break; case 7: AppId = "211"; break; case 8: AppId = "212"; break; case 9: AppId = "213"; break; case 10: AppId = "214"; break; } xSt = xlDoc.Worksheets[i - 1]; //去读取数据放入xSt dt = FlowDP.GetBusinessData_WorkOrderQuery(AppId); //根据不同的条件返回DataTable if (dt != null) { int iColumnCnt = dt.Columns.Count; int iRowCnt = dt.Rows.Count; for (int m = 0; m < iRowCnt; m++) { for (int n = 0; n < iColumnCnt; n++) { xSt.Cells[m + 1, Convert.ToByte(n)].PutValue(dt.Rows[m][n].ToString()); //把数据写到Excel } } } else { xSt = null; } } //保存文件 xlDoc.Save(filepath, Aspose.Excel.FileFormatType.Default); //下载文件 System.IO.FileInfo file = new System.IO.FileInfo(filepath); Response.Clear(); Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF8; // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); // 添加头信息,指定文件大小,让浏览器能够显示下载进度 Response.AddHeader("Content-Length", file.Length.ToString()); // 指定返回的是一个不能被客户端读取的流,必须被下载 Response.ContentType = "application/ms-excel"; // 把文件流发送到客户端 Response.WriteFile(file.FullName); Response.Flush();//这个语句必须有,否则就不回弹出保存的对话框 Response.Close(); // 停止页面的执行 Response.End(); } catch (Exception e) { Page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "script", "alert('" + e.Message + "');", true); } finally { if (File.Exists(filepath)) { File.Delete(filepath); //删除文件 } } }

 Aspose.Excel.dll下载地址

 

原创粉丝点击