1

我想将 GridView 导出到 excel,这很容易。但在网格上方,在 Excel 中,我想要一些其他信息用于识别。我可以以某种方式导出除网格视图以外的东西,然后放入下面的网格视图吗?

编辑: 由于某种原因,当 GridView1 可见并且我尝试导出时,整个页面都会导出,而不仅仅是 gridview。不知道为什么!

Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
    'Create a StringWriter and HtmlTextWriter
    Dim sw As System.IO.StringWriter = New System.IO.StringWriter()
    Dim htw As New System.Web.UI.HtmlTextWriter(sw)

    'Clear the Response object's content and specify the header for the HTML response and type of application file to create
    Response.ClearContent()
    Response.AddHeader("content-disposition", "attachment; filename=SaveFile.xls")
    Response.ContentType = "application/vnd.ms-excel"
    Response.Charset = ""
    EnableViewState = False

    htw.WriteLine("Test, test, test")

    Try
        'Check for the number of GridView rows
        If GridView1.Rows.Count < 65535 Then
            'Turn sorting and paging off and rebind the GridView control
            GridView1.AllowSorting = False
            GridView1.AllowPaging = False
            GridView1.PageSize = GridView1.Rows.Count
            GridView1.AutoGenerateSelectButton() = False
            GridView1.DataBind()


            'Render the GridView1 as HTML - this will cause an error that will fire the VerifyRenderingInServerForm event -- this event is trapped by the Overriding sub procedure given at the end of the program listing
            GridView1.RenderControl(htw)

            'Write the response
            Response.Write(sw.ToString())
            Response.End()

            'Turn sorting and paging on and rebind the GridView control
            GridView1.AllowSorting = True
            GridView1.AllowPaging = True
            '.GridView1.PageSize = 10
            GridView1.AutoGenerateSelectButton() = True
            GridView1.DataBind()
        End If
    Catch ex As Exception

    End Try

End Sub
4

5 回答 5

3

是的你可以。

做这样的事情:

HttpContext.Current.Response.Write("some string value")

在你通过你的gridview之前。

于 2009-03-30T19:04:50.220 回答
1

如果您想将内容导出到 ExcelML,请查看Telerik的 RadGrid

您还可以将标题信息插入网格等

于 2009-03-30T20:00:42.973 回答
1

这是我做同样事情的代码

protected void ExportExcel_OnClick(object sender, EventArgs e) {
    响应。清除();
    Response.AddHeader("content-disposition", "attachment;filename=brugere.xls");
    Response.Charset = "windows-1252";
    Response.ContentType = "应用程序/vnd.xls";
    使用 (StringWriter stringWrite = new StringWriter())
    使用 (HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite)) {
        GridView1.AllowPaging = false;
        GridView1.DataBind();
        GridView1.RenderControl(htmlWrite);
        字符串 html = stringWrite.ToString();
        字符串结果 = Replacer.Replace(html, "");
        Response.Write(结果);
    }
    响应。结束();
}

请注意,我使用正则表达式修剪生成的 html 以避免格式化、图像、div 和诸如此类的东西。

静态只读 Regex Replacer = new Regex("(<input[^<>]*>)|"+
  "(类=\"[^\"]*\")|(样式=\"[^\"]*\")|"+
  "(<a[^]*>)|(</a>)|(<div>)|(</div>)|" +
  "(cellspacing=\"[^\"]*\")|(cellpadding=\"[^\"]*\")|" +
  "(id=\"[^\"]*\")|(border=\"[^\"]*\")", RegexOptions.IgnoreCase);

请记住覆盖以下内容以确保网格将呈现在页面之外

公共覆盖无效VerifyRenderingInServerForm(控制控制){
    返回;
}
于 2009-06-29T10:38:09.600 回答
1

在 Excel 中打开此文件将生成一条警告消息。我会使用像 NPOI 这样的开源导出库之一。 http://npoi.codeplex.com/

如果您仍然喜欢使用 HTML 输出,您可以考虑从以下链接下载 Microsoft 关于 Office HTML 格式的文档:http: //msdn.microsoft.com/en-us/library/aa155477%28office.10%29.aspx

您只需要此存档中的 CHM 文件(打包在 EXE 中)。

祝你好运。

于 2012-02-24T09:56:58.217 回答
0

如果您的 GridView 使用来自 DataTable、DataSet 或 List<> 的数据填充,则以下库将允许您通过调用一个“ CreateExcelDocument ”函数将其导出到 Excel 2007 (.xlsx) 文件。

// Step 1: Create a DataSet, and put some sample data in it
DataSet ds = CreateSampleData();

// Step 2: Create the Excel .xlsx file
try
{
    string excelFilename = "C:\\Sample.xlsx";
    CreateExcelFile.CreateExcelDocument(ds, excelFilename);
}
catch (Exception ex)
{ 
    MessageBox.Show("Couldn't create Excel file.\r\nException: " + ex.Message);
    return;
}

提供了完整的源代码,因此您可以对其进行调整,以在一个或多个工作表的顶部添加额外的数据行。

这个库使用Open XML库,所以它是完全免费的。 http://www.mikesknowledgebase.com/pages/CSharp/ExportToExcel.htm

于 2011-11-23T12:12:28.103 回答