c# 将gridview保存为excel 2007

u010703853 2014-05-26 11:25:43
本人自己写的保存为2003版本的格式的,可是用2007的打开总是有乱码,而且弹出格式不对的对话框,如何保存为2007版本的呀?
我的代码如下:
 protected void Button1_Click(object sender, EventArgs e)
{
string XX = Label1.Text + Label3.Text;
Export("application/ms-excel", XX + "月份采购计划.xls");
}


public override void VerifyRenderingInServerForm(Control control)
{

}

private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
...全文
223 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 2 楼 kingredapple 的回复:
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 试试
试了 不行呀.....
exception92 2014-05-26
  • 打赏
  • 举报
回复
测试 这段代码

 GridView gv = new GridView();  
                     gv.DataSource = dtError;  
                      gv.DataBind();  
                    gv.Attributes.Add("style", "vnd.ms-excel.numberformat:@");  

                      HttpResponse hResponse = this.Response;  
                   string fileName1 = "新员工格式验证错误统计" + DateTime.Now.ToString("yyyyMMdd");  
  
                    hResponse.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName1, System.Text.Encoding.UTF8) + ".xls");  
                    hResponse.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");  
                    hResponse.ContentType = "application/ms-excel";  
                       this.EnableViewState = false;  

                  StringWriter tw = new StringWriter();  
                    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);  
                gv.RenderControl(hw);  

                    hResponse.Write(tw);  
                     hResponse.End();  

kingredapple 2014-05-26
  • 打赏
  • 举报
回复
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 试试
by_封爱 版主 2014-05-26
  • 打赏
  • 举报
回复

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

/// <summary>
        /// 将DataTable数据导出到Excel文件中(xlsx)
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="file"></param>
        public static void TableToExcelForXLSX(DataTable dt, string file)
        {
            XSSFWorkbook xssfworkbook = new XSSFWorkbook();
            ISheet sheet = xssfworkbook.CreateSheet("Test");

            //表头
            IRow row = sheet.CreateRow(0);
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                ICell cell = row.CreateCell(i);
                cell.SetCellValue(dt.Columns[i].ColumnName);
            }

            //数据
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                IRow row1 = sheet.CreateRow(i + 1);
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    ICell cell = row1.CreateCell(j);
                    cell.SetCellValue(dt.Rows[i][j].ToString());
                }
            }

            //转为字节数组
            MemoryStream stream = new MemoryStream();
            xssfworkbook.Write(stream);
            var buf = stream.ToArray();

            //保存为Excel文件
            using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write))
            {
                fs.Write(buf, 0, buf.Length);
                fs.Flush();
            }
        }
自己去下载NPOI.dll

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧