以编程方式在sharepoint 2010中导出到Excel时出错

时间:2014-09-08 10:56:15

标签: sharepoint sharepoint-2010 sharepoint-2007 sharepoint-2013

您正在尝试的文件格式与文件扩展名指定的格式不同。在打开文件之前,验证文件是否已损坏且来自受信任的来源。您要立即打开文件吗?

这个'导出到excel'死了页面上的事件。 这是我的代码

public void ExportToExcelitems(DataTable dt,string fileNameWithoutExt)         {

        if (dt.Rows.Count > 0)
        {
            string filename = fileNameWithoutExt + ".xls";
            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
            var dgGrid = new GridView();
            dgGrid.DataSource = dt;
            dgGrid.DataBind();
            dgGrid.HeaderRow.BackColor = System.Drawing.Color.White;

            dgGrid.HeaderRow.BackColor = System.Drawing.Color.White;

            foreach (GridViewRow row in dgGrid.Rows)
            {
                row.BackColor = System.Drawing.Color.White;

                foreach (TableCell cell in row.Cells)
                {

                    if (row.RowIndex % 2 == 0)
                    {
                        cell.BackColor = dgGrid.RowStyle.BackColor;


                    }
                    else
                    {
                        cell.BackColor = System.Drawing.Color.LightGray;

                    }

                }
            }

            dgGrid.AutoGenerateColumns = false;
            dgGrid.RenderControl(hw);
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
            this.EnableViewState = false;
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.Close();







            }
        }

1 个答案:

答案 0 :(得分:0)

我认为您应该考虑使用OpenXML(或更具体地说是ClosedXML)来构建具有xlsx扩展名的现代Excel文件。

打开XML SDK:https://docs.microsoft.com/en-us/office/open-xml/open-xml-sdk

封闭的XML(nuget):https://www.nuget.org/packages/ClosedXML/

代码示例:https://github.com/ClosedXML/ClosedXML/wiki/Deliver-an-Excel-file-in-ASP.NET