asp.net|excel| Printing | data | Database You may think the above code is more complex, because the above for the printing requirements of high application, is very effective. If you are simply exporting data, 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, you first set the output type of the browser to Application/ms-excel and set the output type of Excel to output in the browser, the default name is Test.xls, and then the custom procedure is invoked. The custom process outputs the data in a DataTable as a string stream, where the data in each DataTable is separated by a tab tab and then output to the browser, and the output is as follows:
The above method, the form of expression is relatively simple, but also can meet the basic requirements of data export. So how do you do that if you want to make a further modification? This provides a way to bind the data you want to export to the DataGrid before you print the DataGrid. You can then format the datagrid you want to print and set properties such as the DataGrid's format. 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, take a look at how to invoke the client's Excel to print, is to let customers click the "Print" button, you can automatically open the client Excel, will print the content to import. To achieve this effect, you must require the client's IE browser settings, in the security-local intranet-customization level, to set the 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's code, it's all you have to say:
Dim dt as DataTable = CType (Application.item ("mydatatable"), DataTable)
DataGrid1.DataSource = DT
Datagrid1.databind ()
When running the program, the user only need to click the Export to Excel button, when IE browser will be prompted to allow the Activx control interaction, select "Yes", you can open the client's Excel to print operations.
The above is in asp.net, commonly used several methods of Excel operation, each have advantages and disadvantages, I hope we can choose according to the 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.