Remit to Microsoft Excel

xiaoxiao2021-03-06  46

Remit to Microsoft Excel

This article will gradually lead you to the DataGrid Web Server control item on ASP.NET Webform, and then turn the DataGrid content to Microsoft Excel. technology

This article will explain two exchanges

DataGrid information technology:

Using the Excel MIME type (or content type) You can use the servo-end program code to link the DataGrid to the information, then open the information in the Excel of the client computer. If you want to perform this, set contentType to Application / VND.ms-Excel. After the user receives the new information flow, the information will be displayed in Excel, just open content in a web browser's new web page. Using Excel Automation You can use the user's program code, capture from the DataGrid into html, then "Automatically perform Excel" to display HTML with a new homebook. "Excel Automation" will always display data outside the browser of the Excel application window. "Automation" is the advantage that if you want to modify the loafa in the remapping, you can control Excel. However, since Excel's instruction code is not secure, the client must be set to "Automation" security settings on the web browser.

Step by step

Start Visual Studio .NET. In the [File] menu, point to [Add], then press [Explan]. Press [Visual Basic Project] in the [Project Type] pane. Press [ASP.NET Web Application] under [Model]. Name your application Excelexport, then click [OK]. WebForm1 will appear in "Design" inspection. Press the right mouse button on the [WebForm1.aspx] of the "Solution General", then press [Rename]. Change the file name to Bottom.aspx. In the [View] function table, press [HTML Original], add the following DataGrid to the

and tags:

BorderColor = "# 999999" Borderwidth = "1px" borderwerstyle = "none" width = "100%" height = "100%" font-size = "x-small"

Font-names = "verdana">

Bordercolor = "Black" backcolor = "# 000084">

In the [View] function table, press [Design] to return to design inspection. DataGrid appears on WebForm. In the [View] function table, press [Code], so that the program code behind the web form can be displayed. Add the following code code to the Page_Load event handle: Note You must change user ID and password = to the correct value to execute this code code. The user account must have correct permissions to perform this job on the database. 'CREATE A Connection and Open IT.

Dim objconn as new system.data.sqlclient.sqlconnection ("User ID = ; password = ; initial catalog = northwind; data source = sqlserver;")

Objconn.open ()

DIM STRSQL AS STRING

Dim objDataSet as new dataset ()

Dim objadapter as new system.data.sqlclient.sqldataadapter ()

'Get all the customers from the usa.

strsql = "SELECT * from Customers where country = 'usa'"

Objadapter.selectCommand = new system.data.sqlclient.sqlcommand (strsql, objconn)

'Fill the dataset.

Objadapter.Fill (ObjDataSet)

'Create a new view.

Dim Oview As New DataView (Objdataset.tables (0))

'Set up the data grid and bind the data.datagrid1.datasource = Oview

DataGrid1.databind ()

'Verify if the page is to be displayed in Excel.

If Request.QueryString ("bexcel") = "1" THEN

'Set the content type.

Response.contentType = "Application / VND.MS-Excel"

'Remove The Charset from the Content-Type Header.

Response.Charset = "" "

'Turn Off The View State.

ME.enableViewState = false

Dim Tw As new system.io.stringwriter ()

DIM HW as new system.web.ui.htmlTextWriter (TW)

'Get the html for the control.

DataGrid1.RenderControl (HW)

'Write the html back to the browser.

Response.write (Tw.toString ())

'End the response.

Response.end ()

END IF

Note: Please replace SQLServer in the program code for your SQL Server name. If you are unable to access the SQL Server in the NorthWind sample repository, modify the connection string to use Microsoft Access 2002 sample Northwind Database: provike = microsoft.jet.Oledb.4.0; data source = C: / Program Files / Microsoft Office / Office10 / Samples / NorthWind.mdb If you choose this method, modify the AFOREMENTIONED code to use the OLEDBCLIENT namespace (without using SqlClient namespace). On the [Project] function table, press [Join HTML]. Name the web page to top.htm, then click [On]. Top.htm will appear in "Design" inspection. On the [View] function table, press [HTML Original]. Replace the content of the HTML original window to the following code: