You may think that Code This is complicated because it is very effective for applications with high printing requirements. If you export data only, you can also use a simple format, such as using the following code:
Private sub page_load (byval sender as system. Object, byval e as system. eventargs) handles mybase. Load
Dim dT as datatable = ctype (application. Item ("mydatatable"), datatable)
Private function convertdttotdf (byval dT as datatable) as string
Dim Dr as datarow, Ary () as object, I as integer
Dim icol as integer
'Output column headers
For icol = 0 to DT. Columns. Count-1
Response. Write (Dt. Columns (icol). tostring & vbtab)
Next
Response. Write (vbcrlf)
'Output data
For each DR in DT. Rows
Ary = dr. itemarray
For I = 0 to ubound (ary)
Response. Write (ary (I). tostring & vbtab)
Next
Response. Write (vbcrlf)
Next
End Function
End Class
In the above Code, first set the browser output type to application/ms-excel, and set the exceloutput type to output in the browser. Then, call the custom process, the custom process outputs the data in a able in the form of a string stream. The data in each datatable is separated by tabs and then output to the browser. The output result is as follows:
The above method is simple, but it can also meet the basic requirements of data export. What should I do if I want to further modify it? Here we provide a method to bind the data to be exported to the DataGrid and then print the DataGrid. In this case, you can format the DataGrid to be printed and set the format and other attributes of the DataGrid. The Code is as follows:
Protected overrides sub render (byval writer as system. Web. UI. htmltextwriter)
Dim dT as datatable = ctype (application. Item ("mydatatable"), datatable)
Finally, let's take a look at how to call the client EXCEL to print. by clicking the "print" button, the customer can automatically open the client Excel and import the content to be printed. To achieve this effect, you must set the client's IE browser, in which "security-Local intranet-Custom Level ", set "Download unsigned activx" to start or prompt. The Code is as follows:
<Script language = "VBScript">
Sub exportbutton_onclick
Dim shtml, oexcel, obook
Shtml = Document. All. Item ("datagrid1"). outerhtml
Set oexcel = Createobject ("Excel. application ")
Set obook = oexcel. workbooks. Add
Obook. htmlproject. htmlprojectitems ("sheet1"). Text = shtml
Obook. htmlproject. refreshdocument
Oexcel. Visible = true
Oexcel. usercontrol = true
End sub
</SCRIPT>
In code-behind code, you only need to write it like this:
Dim dT as datatable = ctype (application. Item ("mydatatable"), datatable)
Datagrid1.datasource = dt
Datagrid1.databind ()
When running the program, you only need to click the export to excel button. In this case, the IE browser will prompt whether activx control interaction is allowed. Select "yes ", you can open the Excel file on the client to print the file.
The preceding methods are commonly used in ASP. NET to operate on Excel. They have their own advantages and disadvantages. I hope you can select them based on your actual situation.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service