Asp.net to obtain the excel worksheet data and connection method (vb.net, c #)

Source: Internet
Author: User

Asp tutorial. net how to obtain the excel worksheet data and Connection Method

C # how to read an excel Data Table
<% @ Page Language = "C #" %>
<% @ Import Namespace = "System. Data" %>
<% @ Import Namespace = "System. Data. OleDb" %>
<Script runat = "server">

Void Page_Load (object sender, EventArgs e ){

String ConnectionString = @ "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = C: Yourspreadsheet.xls; Extended Properties =" "Excel 8.0; HDR = Yes "";";
String CommandText = "select * from [Book $]";

OleDbConnection myConnection = new OleDbConnection (ConnectionString );
OleDbCommand myCommand = new OleDbCommand (CommandText, myConnection );

MyConnection. Open ();

DataGrid1.DataSource = myCommand. ExecuteReader (CommandBehavior. CloseConnection );
DataGrid1.DataBind ();

MyConnection. Close ();
}

</Script>
<Html>
<Head>
</Head>
<Body style = "FONT-FAMILY: arial">
<H2> Simple Data Report
</H2>
<Hr size = "1"/>
<Form runat = "server">
<Asp: datagrid id = "DataGrid1" runat = "server" EnableViewState = "False" ForeColor = "Black" BackColor = "White" CellPadding = "3" GridLines = "None" CellSpacing = "1 ">
<HeaderStyle font-bold = "True" forecolor = "white" backcolor = "# 4A3C8C"> </HeaderStyle>
<ItemStyle backcolor = "# DEDFDE"> </ItemStyle>
</Asp: datagrid>
</Form>
</Body>
</Html>

How to read excel Data Using vb.net

<% @ Import namespace = "System. Data" %>
<% @ Import namespace = "System. Data. OleDb" %>

<Html>
<Head>
<Title> Reading from an Excel Workbook </title>
</Head>

<Body>
<H3> Reading from an Excel Workbook </H3>
<Asp: DataGrid id = "dgInventory" runat = "server"/>
</Body>
</Html>

<Script language = "VB" runat = "server">
Sub Page_Load (Source As Object, E As EventArgs)
Dim strConnection As String = "Provider = Microsoft. Jet. OleDb.4.0 ;"&_
"Data source = C: yourExcellFileName.xls ;"&_
"Extended Properties = Excel 8.0 ;"
Dim objConnection As New OleDbConnection (strConnection)

Dim strSQL As String = "SELECT * FROM Items WHERE Source = 'Dell '"
Dim objCommand As New OleDbCommand (strSQL, objConnection)

ObjConnection. Open ()
DgInventory. DataSource = objCommand. ExecuteReader ()
DgInventory. DataBind ()
ObjConnection. Close ()
End Sub
</Script>

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.