Database data imported in ASP.NET and prints

xiaoxiao2021-03-06  38

If you only export data on a single single, you can 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) Response.ContentType = "application / ms-Excel "Response.AddHeader (" Content-Disposition "," inline; filename = test.xls ") Response.Write (ConvertDtToTDF (dt)) End SubPrivate Function ConvertDtToTDF (ByVal dt As DataTable) As String Dim dr As DataRow, ary () As Object, I as Integer DIM ICOL AS INTEGER 'Output Column Title 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.ItemRay for i = 0 To Ubound (Ary) Response.write (Ary (i) .tostring & vbtab) Next Response.write (vbcrlf) Nextend Functionend Class

In the above code, first set the browser's output type to Application / MS-Excel, and set the output type of Excel to output in the browser, the default name is Test.xls, then the custom process will be called This custom process outputs the data in a DataTable in a string stream, where the data in each DataTable is separated by a Tab tab, and finally outputs to the browser, the output is as follows:

The above method is simple in the form of a manifestation, but it can also meet the basic requirements of data export. That if you want to make it further, how do you do it? A method is provided here that the data to be exported first is bound to the DataGrid, then print the DataGrid. At this time, you can format the DataGrid to be printed, set the data of the DataGrid's Format. code show as below:

Protected Overrides Sub Render (ByVal writer As System.Web.UI.HtmlTextWriter) Dim dt As DataTable = CType (Application.Item ( "MyDataTable"), DataTable) Response.ContentType = "application / ms-Excel" Response.AddHeader ( " Content-disposition "," inline; filename = test.xls ") DataGrid1.datasource = DT DATAGRID1.DATABIND () DataGrid1.RenderControl (Writer) End Sub

The effect of printing is as follows: If you want to go to Word printed, you can also use the above method, just change the code to:

Response.contentType = "Application / MS-Word" response.addhead ("content-disposition", "inline; filename = test.doc")

Finally, let's take a look, how to call the client's Excel to print, just let the customer click the "Print" button to automatically open the client's Excel, and import the content to print. To achieve such an effect, you must require the client's IE browser settings in "Safe-Local Intranet-Custom Level" to set "Download Unqualified Activx" to start or prompt. code show as below: