Export the information in the DataGrid of ASP. NET webform to Microsoft Excel

Source: Internet
Author: User
Tags strong password
Export the information in the DataGrid of ASP. NET webform to Microsoft Excel. This article will gradually introduce DataGridThe Web server controls the slave, and then DataGridThe content is exported to Microsoft Excel.

This article will explain the twoDataGridTechnical advantages of materials:

  • Excel MIME type (or content type)

    You can use the server-side programDataGridLink to the data, and then open the data in the Excel file of the computer. If you want to perform this operationContenttypeSetApplication/vnd. MS-Excel. After receiving the new data stream with the terminal, the data will be displayed in Excel, as if opening the content in the Web browser's new website.

  • Use Excel to automate

    You can use the producer programDataGridThen, click "auto-dynamic excel" to display HTML in a new live album. "Excel auto-scaling" always displays information outside the browser window of the Excel application. One of the highlights of "auto-Automation" is that if you want to modify a live notebook after the information is posted, you can use a program to control the Excel file. However, the Excel command labels are marked as insecure, therefore, you must apply the security settings that allow "auto-scaling" on the web browser.

Description
  1. Invoke Visual Studio. NET. In[Case Study]In the menu, point[New], Click[Case Study].
  2. Click[Case-type]In the pane[Visual basic Program case]. Click[Release this]Under[ASP. NET web application]. Name the applicationExcelexportAnd then click[Confirm].

    "Design" website displays webform1.

  3. In "solution manager ",[Webform1.aspx]Top and right mouse clicks, and then click[Rename]. Change the case nameBottom. aspx.
  4. In[Zookeeper]Click[HTML original response], Set the followingDataGridAdded to the <form> and </form> labels:
    <asp:datagrid id="DataGrid1" runat="server" GridLines="Vertical" CellPadding="3" BackColor="White"BorderColor="#999999" BorderWidth="1px" BorderStyle="None" Width="100%" Height="100%" Font-Size="X-Small"Font-Names="Verdana"><SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle><AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle><ItemStyle BorderWidth="2px" ForeColor="Black" BorderStyle="Solid" BorderColor="Black" BackColor="#EEEEEE"></ItemStyle><HeaderStyle Font-Bold="True" HorizontalAlign="Center" BorderWidth="2px" ForeColor="White" BorderStyle="Solid"BorderColor="Black" BackColor="#000084"></HeaderStyle><FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle><PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999" Mode="NumericPages"></PagerStyle></asp:datagrid>
  5. In[Zookeeper]Click[Design]You can return the design parameters.

    DataGridIt appears on webform.

  6. In[Zookeeper]Click[Program history]In this way, you can view the program signature after the web form. Add the following program hostsPage_loadEvent handling formula:

    Note:You must change the user ID <username> and Password = <strong password> to a correct value to upload this program. The user must have a correct permission to perform this operation on the resource.

    ' Create a connection and open it.Dim objConn As New System.Data.SqlClient.SqlConnection("User ID=<username>;Password=<strong password>;Initial Catalog=Northwind;Data Source=SQLServer;")objConn.Open()Dim strSQL As StringDim 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 = oViewDataGrid1.DataBind()' Verify if the page is to be displayed in Excel.If Request.QueryString("bExcel") = "1" Then    ' Set the content type to Excel.    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:: Replace sqlserver in the program environment with your SQL server name. If you cannot access the SQL server where the northwind dataset is located, modify the response string to use the Microsoft Access 2002 example northwind Dataset:

    Provider = Microsoft. Jet. oledb.4.0; Data Source = C:/program files/Microsoft Office/office10/samples/northwind. MDB

    If you select this method, modify the aforementioned program role to useOledbclientNamespace (not usedSqlclientName ).

  7. In[Case Study]Click[Add an HTML website]. Name the websiteTop.htmAnd then click[Enable Logging].

    Top.htm is displayed in the "design" operation.

  8. In[Zookeeper]Click[HTML original response]. Replace the content of the original HTML response window with the following program:
  9. In[Case Study]Click[Add an HTML website]. Name the websiteFrameset.htmAnd then click[Enable Logging].

    The parameter frameset.htm will be opened in the parameter settings.

  10. In[Zookeeper]Click[HTML original response]. Replace the content of the original HTML response window with the following program:
  11. In "solution manager ",Using frameset.htm]Press the mouse on the right and then click[Set to start sequence].
  12. In[Build]Click[Build solution].
Please refer to it!
  1. In[Zookeeper]Click[Dynamic, but not dynamic].

    After the Web tracking tool opens the frameworkDataGridDisplays the information of the northwind resource.

  2. Click in the drop-down list[Automation], And then press[Go].

    DataGridContent displays the external content of the browser in the Microsoft Excel application window.

  3. Click in the drop-down list[MIME type (in a new window)], And then press[Go].

    DataGridThe content is displayed in the Excel file of the new web browser window.

    Note:: When you are prompted to open or store an Excel file, please click[Enable Logging].

  4. Click in the drop-down list[MIME type (in the frame)], And then press[Go].

    DataGridContent is displayed in the framework of the Web browser Excel framework.

    Note:: When you are prompted to open or store an Excel file, please click[Enable Logging].

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.