偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.
全部博文(1750)
分类: WINDOWS
2010-07-07 15:22:55
窗体顶端
原文地址, 需要翻 墙
Introduction: Exporting GridView to Excel is a very common task which is performed in most of the web applications. There are various techniques of exporting the GridView to excel and it highly depends on the application scenario. In this article I will demonstrate some techniques that you will find useful. GridView Export the Excel (Basic Code): Let's start with the basic export scenario. First, we need to populate the GridView with some data. I have created a custom table which has number of fields. You can check out the screen shot below. Now, the next task is to populate the GridView with the data from the database. Check out the following code which uses the DataSet to populate the GridView.
So, now the GridView is populated with the data. The next task is to export the GridView to excel. You can use the following code for the button click event.
You will also need to override the VerifyRenderingInServerForm method. Take a look at the code below:
When you click the Export to Excel (Default) button a dialog box will popup which will allow you to open or save the exported file. Select open the file and you will see the exported data in the excel spreadsheet. Take a look at the screen shot below which shows the exported GridView in excel spreadsheet. Exporting GridView to Excel With Style: Did you see the problem in the above exportation code? Yes, the leading zero's were truncated. It means that if your ID was 000345 it will show up as 345. You can fix this problem by adding a CSS script to the output stream. In order for the ID column to appear correctly you need to store it as text. The text format in excel is represented by "mso-number-format:"\@". Once, you know the format you can append the style in the output stream. Check out the code below.
As, you can see in the above code that I have used the string variable "style" to hold the style of the GridView column. And I used the Response.Write method to write the style to the output stream. The last thing that you need to do is to add the style to the ID column. This can be done in the RowDataBound event of the GridView control.
Now, when you export the GridView to excel your exported file will look something like this.
Exporting GridView With LinkButtons and Paging: When you try to export the GridView which contains LinkButtons and Paging enabled then you might see the following error. You can easily handle this error by going to the page source and turning the EnableEventValidation = "false".
But let's take a look at the exported file. As, you can see that the LinkButtons and DropDownLists are also exported with the GridView control. Although the DropDownList does display the correct user selections but it does not look good in the exported file. So, let's see how we can remove the DropDownList and only display the selected text. I have created a simple DisableControls method which iterates through the GridView control and replace all LinkButtons and DropDownLists with the Literal control.
The idea is very simple just replace all the LinkButton and DropDownList controls with a Literal control and assign their selection to the Literal control Text property. You need to call this function before the exportation takes place.
Now, when you export the GridView you will only see the selected text. Check out the screen shot below which shows the effect. Conclusion: In this article you learned different ways to export the GridView to excel. I have attached the complete source code to download. I hope you liked this article, happy coding! Similar Articles |
窗体底端