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

第一种:将页面的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下载地址

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值