导出网格视图数据
将 gridview 导出到 Excel 电子表格的最佳方法是什么? 这看起来很简单
,只是我的 Gridview 没有导出属性。最快的方法是什么?
What is the best way to export a gridview into an Excel spreadsheet? This seems easy
except that my Gridview doesn't have an export attribute. What is the quickest way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
可能有一些解决方案,但如果您想自己做,您可以编写一些代码来遍历 GridView.Rows 集合,然后遍历其中的 GridViewRow.Cells 集合。
从那里构建 CSV 文件应该非常容易,并且 Excel 可以毫无问题地读取它。
CSV 文件只是文本文件,其值放在引号内,并以逗号分隔。像这样:
您可以打开记事本并手动构建一个来尝试一下。
There's probably something out there for this, but if you want to do it yourself you could just write some code that walks the GridView.Rows collection, and then the GridViewRow.Cells collection inside of that.
It should be pretty easy to build a CSV file from there, and Excel could read it no problem.
CSV files are just text files with values inside of quotes, seperated by commas. Like this:
You can just pop Notepad open and build one by hand to try it out.
我已经这样做过好几次了。 Excel 有 XML 版本。它以 .xml 扩展名结束,但您只需将文件的扩展名更改为 .xls,XML 格式的文件就可以在 Excel 中打开。
这种方法的最大障碍是 Excel XML 格式。我通常以我想要的近似格式在 Excel 中创建一个 Excel 文件。然后,我将 Excel 文件保存为 XML 格式,并在文本编辑器中将其打开。
我通常从此 Excel 示例页面创建一个模板文件。然后,当我在 Gridview 中导出信息时,我只需为包含我计划填充的单元格的部分创建 xml,我只需在模板文件中添加已有的文本即可。
一旦打开 xml 格式的 Excel 文件,您就会相对容易地找出所需的 XML。最难理解的部分是单元格引用格式选项的方式,这些选项位于 XML 文件的顶部。
祝你好运,如果您需要更多说明,请告诉我。
编辑:
您只需创建一次 Excel 模板文件,即可了解您需要生成的所需 xml。生成 xml 后,使用以下代码将其发送给用户:
I've done this several times. Excel has a XML version. It ends up with the .xml extension, but you can just change the file's extension to .xls and the XML formatted file will open in excel just fine.
The biggest hurdle of this approach is the excel XML format. I usually create an excel file in excel in the approximate format that I desire. I then save the Excel file to XML format, and open it up in a text editor.
I usually create a template file from this Excel sample page. Then when I am exporting the information in the Gridview, I only have to create the xml for the section that contains the cells I plan on populating, I just prepend, and append the text already in the template file.
Once you open up the xml formatted excel file, you'll figure out the needed XML relatively easily. The hardest part to understand is the way the cells reference the formatting options, which are at the top of the XML file.
Good luck, let me know if you need more clarification.
Edit:
You will only need to create the template excel file once, just to get a feel for the needed xml that you'll need to generate. Once you've generated the xml use the following code to send it to the user:
这个 .net 库非常适合我们的用例。
没有与 GridView 控件的内置集成,但编写通用适配器非常容易,并且可以在其他项目中重用。
This library for .net has worked very well for our use cases.
There's no built-in integration with a GridView control, but writing a generic adapter is easy enough and would be reusable in other projects.
此方法直接转换为 Excel 格式,无需在服务器上安装 XML 或使用 XML。
This method goes straight into Excel format without requiring XML be installed on the server or by using XML.
我使用了 CarlosAg.ExcelXmlWriter 链接。
我循环遍历所有
GridViews
HeaderCells
,然后遍历所有行。唯一的事情是,如果您允许分页并且您有多个页面,则必须将 PageSize 设置为较高的值(我设置为 10000000),然后将DataBind
的GridView
再次完成你的工作。然后设置旧的 PageSize 值。如果有人知道更好的解决方案,欢迎您。编辑: try/catch 之所以存在,是因为由于某种原因无法检查控件类型,然后转换为 Label 或
LinkButton ==> control.GetType()。
这是我的代码。
I used
CarlosAg.ExcelXmlWriter
link.I looped through all
GridViews
HeaderCells
and then through all rows. The only thing is that if you allow paging and you've more than one page you have to set the PageSize to a high value (I set to 10000000) thenDataBind
theGridView
again and do your work. Afterwards set the old PageSize value back. If someone knows a better solution, you're welcome.EDIT: The try/catch is there because for some reason it is not possible to check the control type and then cast to Label or
LinkButton ==> control.GetType()
.Here's my code.
您必须安装 Excel 并在项目中引用 Office 互操作库。
添加:
导入Microsoft.Office.Core,
导入 Microsoft.Office.Interop
上面的解决方案采用 gridview 并从中提取 html。然后写入excel。来自网格的 html 将包含样式属性,例如 padding 和 padding 等。颜色。它还将使可排序的列标题看起来像链接。当我使用它时,我编写了一个自定义解析器来去除所有不需要的样式,只为我提供原始数据。我将把这个任务留给你,因为它是特定于每个网格的。
包含对VerifyRenderingInServerForm 的覆盖非常重要,即使其中没有任何代码。
You must have excel installed and reference the Office interop libraries in your project.
Add:
Imports Microsoft.Office.Core,
Imports Microsoft.Office.Interop
The solution above takes the gridview and pulls the html out of it. Then writes it to excel. The html coming from the grid will include style attributes such as padding & color. It will also make sortable column headings look like links. When I used this I wrote a custom parser to strip all the unwanted styling out to give me just the raw data. I will leave that task up to you since it is specific to each grid.
It is very important that you include the override to VerifyRenderingInServerForm, even though there is not any code inside of it.