当前位置:编程学习 > C#/ASP.NET >>

asp.net2.0导Excel问题.....高手请进

public  void  ExportToExcel(System.Data.DataTable  dt,  string  FileName)  
       {  
               DataGrid  excel  =  new  DataGrid();  
               System.Web.HttpContext  curContext  =  System.Web.HttpContext.Current;  
               System.Text.Encoding  encoding  =  System.Text.Encoding.GetEncoding("GB2312");  
               System.IO.StringWriter  strWriter  =  null;  
               System.Web.UI.HtmlTextWriter  htmlWriter  =  null;  
 
               if  (dt  !=  null)  
               {  
                       curContext.Response.ContentType  =  "application/vnd.ms-excel";  
                       curContext.Response.AppendHeader("Content-Disposition",  "attachment;filename="  +  FileName);  
                       curContext.Response.ContentEncoding  =  encoding;  
                       curContext.Response.Charset  =  "";  
 
                       strWriter  =  new  System.IO.StringWriter();  
                       htmlWriter  =  new  System.Web.UI.HtmlTextWriter(strWriter);  
 
                       excel.DataSource  =  dt.DefaultView;  
                       excel.AllowPaging  =  false;  
                       excel.DataBind();  
                       excel.RenderControl(htmlWriter);  
 
                       curContext.Response.Write(strWriter.ToString());  
                       strWriter.Close();  
                       curContext.Response.End();  
               }  
       }  
 
以上是一段导EXCEL的代码.运行完之后点开导出来的EXCEL打开看后数据是没问题,但是最下面一行就有这句话"正在中止线程."  

 偶尔会出现乱码情况.

求解... 

例:

编号 字段1   字段2 字段3
3 kkk              rrr   aaa
2 kkkk            ccc   sss
1 hhhh            ggg ddd
正在中止线程。

--------------------编程问答--------------------

没人吗....郁闷了```

--------------------编程问答-------------------- Response.Clear();
    Response.Buffer = true;
    Response.Charset = "GB2312";
    Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
    // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
    Response.ContentEncoding = System.Text.Encoding.UTF7;
    Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
    System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
    this.GridView1.RenderControl(oHtmlTextWriter);
    Response.Output.Write(oStringWriter.ToString());
    Response.Flush();
    Response.End();


这样试试 --------------------编程问答--------------------
回 lbaeolus() 

按照你那方法试了.发现问题还是存在的.没变化

--------------------编程问答-------------------- Public Sub GridViewToExcel(ByVal ctl As Control, ByVal FileName As String)
        HttpContext.Current.Response.Clear()
        HttpContext.Current.Response.Buffer = True
        HttpContext.Current.Response.Charset = "GB2312"
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default
        'Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>")
        HttpContext.Current.Response.ContentType = "application/ms-excel"
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" & Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) & ".xls")
        ctl.Page.EnableViewState = False
        Dim oStringWriter As System.IO.StringWriter = New System.IO.StringWriter
        Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(oStringWriter)
        ctl.RenderControl(oHtmlTextWriter)
        HttpContext.Current.Response.Write(oStringWriter.ToString())
        HttpContext.Current.Response.Flush()
        HttpContext.Current.Response.End()
    End Sub
调用时GridViewToExcel(控件id,表名) --------------------编程问答--------------------  Response.Clear();
        HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
        HttpContext.Current.Response.Charset = "UTF-7";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
        grdsendbox.Page.EnableViewState = false;
        System.Globalization.CultureInfo Cul = new System.Globalization.CultureInfo("ZH-CN", true);
        System.IO.StringWriter sw = new System.IO.StringWriter(Cul);
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
        grdsendbox.RenderControl(hw);
        HttpContext.Current.Response.Write(sw.ToString());
        HttpContext.Current.Response.End(); --------------------编程问答-------------------- 楼上的是正解 --------------------编程问答-------------------- 按钮代码如下:
                        string sql="";
GetDataLay data=new GetDataLay(Page);
string dt=System.DateTime.Today.ToString("yyyyMMdd",System.Globalization.DateTimeFormatInfo.InvariantInfo);



sql="select id,ename,linker,tel,fax,email,addr from enterprise where iflag=0";
连接数据库并读取数据
if (dr!=null)
{
if (dr.HasRows)
{
Response.Clear(); 
Response.Buffer= true; 
Response.Charset="GB2312";    
Response.AppendHeader("Content-Disposition","attachment;filename=QYDJ"+dt+".xls"); 
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
this.EnableViewState = false; 
Response.Write("\t\t\t企业注册登记汇总表\t\n");
Response.Write("\t\t\t\t\t"+System.DateTime.Now.ToString("D")+"\t\n");
Response.Write("帐号\t企业名称\t联系人\t联系电话\t传真\t邮箱\t地址\t\n");
sql="";
while (dr.Read())
{
sql="";
for (int i=0;i<dr.FieldCount;i++)
{
sql += "\t"+dr[i].ToString();
}
Response.Write(sql.Substring(1)+"\n");
}
sql=null;
dr.Close();

Response.End();
}
else
{
dr.Close();

sql="没有您选择的汇总表!";
}
}
else
{
dr.Close();

sql="形成汇总表出错,请稍后重试。";
}
if (sql!=null)
{
Page.Response.Write(sql);
}

我的程序就是这么到处的,没有问题
http://www.xpeng.com.cn
http://www.lyyywx.com.cn
http://www.bdgxqsq.com.cn --------------------编程问答-------------------- 顶一下 --------------------编程问答-------------------- 顶7楼的,一般没有问题
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,