从DataGrid输出文件到Excel、Word和Text文件使用这样输出还是很高效的。

1、输出到Excel:
            Response.Clear();
            Response.AddHeader(
"content-disposition""attachment;filename=FileName.xls");
            Response.Charset 
= "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType 
= "application/vnd.xls";
            System.IO.StringWriter stringWrite 
= new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite 
= new HtmlTextWriter(stringWrite);
            myDataGrid.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();

2、输出到Word文档
            Response.Clear();
            Response.AddHeader(
"content-disposition""attachment;filename=FileName.doc");
            Response.Charset 
= "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType 
= "application/vnd.word";
            System.IO.StringWriter stringWrite 
= new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite 
= new HtmlTextWriter(stringWrite);
            myDataGrid.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();

3、输出到文本文件
            Database db = DatabaseFactory.CreateDatabase(); 
            DBCommandWrapper selectCommandWrapper 
= db.GetStoredProcCommandWrapper("sp_GetLatestArticles"); 
            
            DataSet ds 
= db.ExecuteDataSet(selectCommandWrapper); 

            StringBuilder str 
= new StringBuilder(); 

            
for(int i=0;i<=ds.Tables[0].Rows.Count - 1; i++
            {
                
for(int j=0;j<=ds.Tables[0].Columns.Count - 1; j++
                {
                    str.Append(ds.Tables[
0].Rows[i][j].ToString()); 
                }
                str.Append(
"<BR>");
            }

            Response.Clear();
            Response.AddHeader(
"content-disposition""attachment;filename=FileName.txt");
            Response.Charset 
= "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType 
= "application/vnd.text";
            System.IO.StringWriter stringWrite 
= new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite 
= new HtmlTextWriter(stringWrite);
            Response.Write(str.ToString());
            Response.End();

相关文章:

  • 2021-08-28
  • 2021-12-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-23
猜你喜欢
  • 2021-12-06
  • 2022-12-23
  • 2022-12-23
  • 2021-07-01
  • 2021-12-14
  • 2022-12-23
相关资源
相似解决方案