ASP.net Gridview导出到Excel .xlsx无法正常工作

时间:2013-11-13 10:17:16

标签: c# excel gridview

在.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;
    }
}

2 个答案:

答案 0 :(得分:2)

我认为您得到的错误是“您尝试打开的文件格式与文件扩展名指定的格式不同”。这是因为在传统的Export to Excel方法中,GridView首先转换为HTML字符串,然后将该HTML字符串导出到Excel,但Excel 2007/2010无法识别纯html格式。有一种方法可以在不使用HtmlTextWriter的情况下使用EPPlus。它允许您在服务器上创建Excel电子表格。查看这篇文章: Create Excel workbook in asp.net或此一个:Export Gridview data to Excel (.xlsx) without using HtmlTextWriter in Asp.NET

答案 1 :(得分:-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);