1

导出到 .xls 中的 excel 正在工作,但在更改内容类型后导出到 .xlsx 不起作用 - 我的代码如下:

private void ExportToExcel()
{
    try
    {
        Response.Clear();
        Response.Buffer = true;

        //Response.AddHeader("content-disposition", "attachment;filename=LoanDataDeletion.xls");
        //Response.Charset = "";
       // Response.ContentType = "application/vnd.ms-excel";

        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.Charset = "";
        Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", "LoanDataDeletion.xlsx"));

        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        grdView.AllowPaging = false;
        grdView.DataBind();

        //Change the Header Row back to white color
        grdView.HeaderRow.Style.Add("background-color", "#FFFFFF");

        //Apply style to Individual Cells
        for (int i = 0; i < grdView.Columns.Count; i++)
        {
            grdView.HeaderRow.Cells[i].Style.Add("background-color", "green");
        }
        for (int i = 0; i < grdView.Rows.Count; i++)
        {
            GridViewRow row = grdView.Rows[i];

            //Change Color back to white
            row.BackColor = System.Drawing.Color.White;

            //Apply text style to each Row
            row.Attributes.Add("class", "textmode");

            //Apply style to Individual Cells of Alternating Row
            if (i % 2 != 0)
            {
                row.Cells[0].Style.Add("background-color", "#C2D69B");
                row.Cells[1].Style.Add("background-color", "#C2D69B");
                row.Cells[2].Style.Add("background-color", "#C2D69B");
                row.Cells[3].Style.Add("background-color", "#C2D69B");
            }
        }
        grdView.RenderControl(hw);

        //style to format numbers to string
        string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }
    catch (Exception)
    {
        throw;
    }
}
4

2 回答 2

2

我想你得到的错误是“你试图打开的文件与文件扩展名指定的格式不同”。发生这种情况是因为在传统的导出到 Excel 方法中,GridView 先转换为 HTML 字符串,然后将该 HTML 字符串导出到 Excel,但 Excel 2007/2010 无法识别纯 html 格式。有一种方法可以在不使用 HtmlTextWriter 的情况下使用EPPlus来做到这一点。它允许您在服务器上创建 Excel 电子表格。检查这篇文章: 在 asp.net 中创建 Excel 工作簿或这个:在 Asp.NET 中不使用 HtmlTextWriter 的情况下将 Gridview 数据导出到 Excel (.xlsx)

于 2014-03-14T11:31:06.803 回答
-1
            string path = System.IO.Path.GetTempPath() + System.DateTime.Now.Ticks + ".xls";
            GridView grdTemp = new GridView();
            grdTemp.DataSource = dt;
            grdTemp.Caption = "Products<br>  " + DateTime.Now;
            grdTemp.DataBind();

            using (StreamWriter sw = new StreamWriter(path))
            {
                using (HtmlTextWriter hw = new HtmlTextWriter(sw))
                {
                    grdTemp.RenderControl(hw);

                }

            }
            string save_path = path;
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", System.DateTime.Now.Ticks + ".xls"));
            Response.ContentType = "application/excel";
            Response.WriteFile(save_path);
            Response.End();
            System.IO.File.Delete(save_path);
于 2013-11-20T10:27:08.590 回答