zoukankan      html  css  js  c++  java
  • Asp.net导出Excel

     上篇博客,讲述初步导出Excel出现的问题。现在具体写一下导出Excel步骤。
       对于大批量的数据,导出Excel的办法如下。
       
     (1)使用DataTable导出Excel
         其中使用StringWriter类:将信息写入字符串。其命名空间为:System.IO

          具体的原理是:
          首先读取DataTable中的列名,然后循环读取DataTable行值。
          然后设置导出的编码,导出的类型。
          最后输出StringWriter对象即可。
          这种导出是将文件直接以文件流的形式输出到浏览器。

       

    [csharp] view plain copy
     
     print?
    1. private  StringWriter GetStringWriter(DataTable dt)  
    2.     {  
    3.         StringWriter sw = new StringWriter();  
    4.         //读列名  
    5.         foreach (DataColumn dc in dt.Columns)  
    6.             sw.Write(dc.ColumnName + " ");  
    7.         //读列值  
    8.         //重新的一行  
    9.         sw.Write(sw.NewLine);  
    10.               if (dt != null)  
    11.         {  
    12.             foreach (DataRow dr in dt.Rows)  
    13.             {  
    14.                 for (int i = 0; i < dt.Columns.Count; i++)  
    15.                 {  
    16.                     sw.Write(dr[i].ToString() + " ");  
    17.                 }  
    18.                 sw.Write(sw.NewLine);  
    19.             }  
    20.         }  
    21.         sw.Close();  
    22.               
    23.         return sw;  
    24.     }  
    [csharp] view plain copy
     
     print?
    1. protected void ExcelImport(DataTable dt, string ExportFileName)  
    2.     {  
    3.         StringWriter sw = GetStringWriter(dt);  
    4.         //当前编码  
    5.         HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");  
    6.         //把输出的文件名进行编码  
    7.         string fileName = HttpUtility.UrlEncode (ExportFileName, System.Text.Encoding.UTF8);  
    8.         //文件名  
    9.         string str = "attachment;filename=" + fileName + ".xls";  
    10.         //把文件头输出,此文件头激活文件下载框  
    11.         HttpContext.Current.Response.AppendHeader("Content-Disposition", str);//http报头文件  
    12.         HttpContext.Current.Response.ContentType = "application/ms-excel";  
    13.         this.Page.EnableViewState = false;  
    14.         Response.Write(sw);  
    15.         Response.End();  
    16.  }  
    [csharp] view plain copy
     
     print?
    1. protected void Button1_Click(object sender, EventArgs e)  
    2.    {  
    3.        DataTable dt = new SelectCourseManager().SelectByCollegeAndProperty(ddlCourseProperty.Text, ddlCollege.SelectedValue);  
    4.        if (dt.Rows.Count == 0)  
    5.            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascriopt' defer>alert('没有数据,不需要导出哈');</script>");  
    6.        else  
    7.        {   
    8.            //导出Excel  
    9.            ExcelImport(dt, "课程");  
    10.              
    11.         }  
    12.   
    13.    }  


          在导出Excel的时候,最关键的是编码问题,不同的形式导出Excel编码形式不同,否则,导出的Excel文件中文显示乱码。在DataTable导出Excel的时候,当前输出的编码样式是简体中文GB2312

          HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

          而其导出的文件名是:UTF8编码

            string fileName = HttpUtility.UrlEncode (ExportFileName, System.Text.Encoding.UTF8);   

  • 相关阅读:
    Three Algorithms for Fibonacci
    微软面试经历
    [TIP]命令行快速查看图片(Ubuntu)
    emacs as the c++ ide on the Ubuntu
    boost learn notes
    ReadingNotes@02122013
    ignoreunderline.org
    cnblogsminormode.org
    c++ 0x 新特性
    noip模拟赛 思考熊的马拉松
  • 原文地址:https://www.cnblogs.com/nzgbk/p/6112470.html
Copyright © 2011-2022 走看看