asp.net - 导出表

发布于 2024-12-03 08:39:18 字数 464 浏览 1 评论 0原文

将表格导出为 Excel 文件格式时遇到一个大问题。

首先,我创建了在服务器上运行的代码,并允许我将数据导出到 Excel。由于我的表是从数据库动态创建的,因此该阶段表中没有任何内容,因此没有导出任何数据。

我的第二种方法是使用 javascript 或一个名为“DataTables”(www.datatables.net)的非常好的 jQuery 插件来定位客户端的最终编译表。两次尝试都失败了。 Javascript 对我来说似乎太复杂了,而且它在 Firefox 中运行有困难,另一方面插件需要一个非常具体的表结构,我担心我无法提供。

因此,我的一个新想法是:在服务器上编译和构建页面之后、将其发送到浏览器之前抓取页面。使用服务器上的函数定位表并获取其数据。最后将数据导出到Excel,并将页面发送到浏览器。现在。是否可以?如果是的话,那又如何呢?

我是编程世界的初学者,因此任何建设性的建议和批评都将受到高度赞赏。我不介意任何硬代码示例;)

I have a big problem with exporting my table to Excel file format.

Firstly I created code which runs on server and allows me to export data to Excel. Due to the fact that my table is created dynamically from the database there is nothing WITHIN the table at that stage, so no data were exported.

My second approach was targeting the final compiled table on the client side using either javascript or a very nice jQuery plugin called "DataTables" (www.datatables.net). Both of the attempts failed. Javascript seems to be to complex for me, plus it has difficulties running in Firefox, plugin on the other hand requires a very specific table structure which I am afraid I cannot provide.

So, a new idea of mine is: grab the page just after compiling and building it on the server, but before sending it to the browser. Target THE table and source its data using function on server. Finally export data to Excel, and send the page to the browser. Now. Is it possible? And if yes, then how?

I am beginner in programming world so any constructive suggestions and criticism would be highly appreciated. I would not mind any hard code examples ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

五里雾 2024-12-10 08:39:18

你可以尝试做这样的事情:

protected void btnExport_Click(object sender, EventArgs e)
{       
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "application/vnd.ms-excel";
    Response.Charset = "";

    System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

    //if you're exporting a table put the table in a placeholder and render 
    //the placeholder to the text writer here
    grdJobs.RenderControl(oHtmlTextWriter);

    Response.Write(oStringWriter.ToString());
    Response.End();
}

You can try doing something like this:

protected void btnExport_Click(object sender, EventArgs e)
{       
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "application/vnd.ms-excel";
    Response.Charset = "";

    System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

    //if you're exporting a table put the table in a placeholder and render 
    //the placeholder to the text writer here
    grdJobs.RenderControl(oHtmlTextWriter);

    Response.Write(oStringWriter.ToString());
    Response.End();
}
巷子口的你 2024-12-10 08:39:18

您需要做的是将查询结果导出到 .CSV 文件中。 CSV 文件可以在 Excel 中打开,完全没有问题。 http://wiki.asp.net/page.aspx/ 401/export-to-csv-file/ 这向您展示了如何导出为 .CSV 格式。

What you need to do is export your query results in a .CSV file. CSV files can be opened in Excel no problem at all. http://wiki.asp.net/page.aspx/401/export-to-csv-file/ This shows you how to export into a .CSV format.

机场等船 2024-12-10 08:39:18

您将在这里得到很多建议,而不是答案。我的建议是尝试 jQuery 插件: table2csv 以创建更通用的文件格式。但有一些方法可以针对实际的 Excel 格式,例如项目。

You're going to get a lot of suggestions instead of answers on this here. My recommendation would be to try the jQuery plugin: table2csv in order to create a more universal file format. But there are ways to target an actual Excel format, like this project.

执妄 2024-12-10 08:39:18

如果您想导出到实际的 XLS 或 XLSX 而不仅仅是 CSV 或仅在 Excel 中“打开”的文件,有第三方工具可以帮助您完成此操作。这里有一个例子:

http://www.officewriter.com

If you want to export to actual XLS or XLSX instead of just CSV or something that just "opens" in Excel, there are third party tools that can help you with this. One example here:

http://www.officewriter.com

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文