将自定义标题添加到 Excel 文件
在 ASP.NET 中将 DataSet
导出到 Excel 时,是否可以向 Excel 添加自定义标头? 我有一个这样的要求。我可以成功将 DataSet
导出到 Excel。但我无法添加自定义标头。如果有人有解决方案,请帮助我。提前致谢。
Is it possible to add a custom header to the Excel while exporting a DataSet
to Excel in ASP.NET?
I have one requirement like this. I can export the DataSet
to the Excel successfully. But I can't add the custom header. Please help me if anybody have the solution. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我给你准备了一点小礼物。这是我在大量研究 Excel Interop 之后编写的一个完整的 Excel 处理程序。查看“dataGridView1 = YOUR_DATATABLE_HERE;”行我知道它说的是 dataGridView,它是 DataTable,仅供参考。喂它,你就是金子。当然,您需要将数据集转换为 DataTable,但这是另一个问题。简而言之,您可以复制并粘贴此代码,您所要做的就是更改 YOUR_DATATABLE_HERE 变量和实际的 DataTable,剩下的事情将由它完成。有很多注释掉的部分。根据需要取消注释。它们应该是不言自明的。哦,仅供参考...如果您的 PageSetup 无法正常工作,那就令人头痛了。它可以是任何东西,从您需要添加打印机到一些非常奇特的东西,但是它依赖于主机,而不依赖于代码。如果这最终使您崩溃,请注释掉该部分。
请注意代码的“#region Column Headers”部分。您可以在此处更改标题。在我的代码中,我只是从表中提取它们,但您可以自定义它们。如果您需要这部分的帮助,请告诉我,但同样,它应该是不言自明的。我知道这是一大块代码,但好处是,它实际上已经准备好按原样运行。您所要做的就是将其放入您的项目中,为其提供一个数据表,也许添加一些资源(即任何带红色下划线的内容,只需右键单击它并选择解析),然后您就应该设置为使用 Excel 任何内容。祝你好运!
I got a little treat for you. This is a full blown Excel handler I wrote after lots of studying of Excel Interop. Look at the line "dataGridView1 = YOUR_DATATABLE_HERE;" I know it says dataGridView, it's DataTable, just FYI. Feed it that and you are golden. Of course you would need to convert a dataset to DataTable, but that's another question. Simply put, you can copy and paste this code, and all you have to change is the YOUR_DATATABLE_HERE variable with an actual DataTable and that thing will do the rest. There are lots of commented out sections. Uncomment them as needed. They should be self explanatory. Oh FYI... if your PageSetup does not work properly, that's a headache. It could be anything from you need to add a printer to some really fancy stuff, however it's host computer dependant, not code dependant. If that ends up crashing you, please comment out that section.
Notice the "#region Column Headers" part of the code. Here you can change the headers. In my code I simply pull them from the table but you can customize them. Let me know if you need help with that portion, but again, it should be self explanatory. I know this is a huge chunk of code, but the nice thing is, it's practically ready to go as-is. All you have to do is throw it in your project, feed it a DataTable, maybe add some resources (i.e. anything that's underlined red, just right click it and choose resolve), and you should be set to go for Excel anything. Good luck to you!
当您使用数据集时,如果您使用数据集第一行中的值作为列名称,则可以更改数据集第一行中的值,然后将数据集导出到 Excel:
As you are using dataset, you can change the value in the first row of the dataset if you are using that to be your column names and then export the dataset to excel:
如果您使用 Response.Write 导出 Excel。
您可以轻松使用以下代码,并且可以根据需要自定义
Header
,就像 HTML 标头一样。麦克,你也在寻找同样的东西吗?
IF you are using
Response.Write
to Export the Excel.You can use the following code with minimal effort and the
Header
can be customized as you want, just like HTML headers.Mac, Are you looking for the same?