将GridView导出到Excel

时间:2010-03-16 06:15:18

标签: c# .net asp.net

使用Matt的util代码

(对Unicode文本进行了一点编辑)

    public class GridViewExportUtil
{
    /// <param name="fileName"></param>
    /// <param name="gv"></param>
    public static void Export(string fileName, GridView gv)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        HttpContext.Current.Response.Charset = System.Text.Encoding.Unicode.EncodingName;
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
        HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
        HttpContext.Current.Response.AddHeader(
             "content-disposition", string.Format(//"content-disposition",
                "attachment; filename=Report.xml"));//, fileName)); // Need .XLS file


        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  Create a form to contain the grid
                Table table = new Table();

                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);
                }

                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    GridViewExportUtil.PrepareControlForExport(row);
                    table.Rows.Add(row);
                }

                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }

                //  render the table into the htmlwriter
                table.RenderControl(htw);

                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }

    /// <summary>
    /// Replace any of the contained controls with literals
    /// </summary>
    /// <param name="control"></param>
    private static void PrepareControlForExport(Control control)
    {
        for (int i = 0; i < control.Controls.Count; i++)
        {
            Control current = control.Controls[i];
            if (current is LinkButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
            }
            else if (current is ImageButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
            }
            else if (current is HyperLink)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
            }
            else if (current is DropDownList)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
            }
            else if (current is CheckBox)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
            }

            if (current.HasControls())
            {
                GridViewExportUtil.PrepareControlForExport(current);
            }
        }
    }

问题:如何使下载的文件可编辑(非只读)

并且... XLS不会以Unicode格式打开。当我将格式更改为UTF8时,我看不到俄语单词:S

第二个问题:如何为.xls制作Unicode

第三个问题:如何保存表格行?

谢谢。

4 - 如何在NPOI表中创建表

            int i =0;
            if (gv.HeaderRow != null)
            {
                GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                sheet1.CreateRow(1).CreateCell(i).SetCellValue(gv.HeaderRow.ToString());
                //table.Rows.Add(gv.HeaderRow);
                i++;
            }
是吗?

1 个答案:

答案 0 :(得分:1)

HTML与XML不同。

Excel 2002/2003使用的SpreadsheetML语言是一种完全不同的语言。如果你甚至让Excel打开文件,那只是因为无论文件扩展名如何,它都会识别HTML文件。

至于Unicode问题,我认为问题是在调用ToString()时你的StringWriter的编码设置。

我建议查看NPOI项目,其中包含从.NET创建二进制文件/ BIFF(Excel 97/2000),SpreadsheetML(Excel 2002/2003)和OOXML(Excel 2007)文件所需的一切:

http://npoi.codeplex.com/