Step 1: Create an Excel file.
It is easy to create an Excel file. The key step is to select the content you want to display on the page and choose "insert"> "name"> "definition" from the menu ", enter the name of the selected region. Here, I name it myRange1, which will be used later. After the Excel file is edited, save it to the hard disk. I saved it to the root directory of the Web server. Of course you can save it anywhere, but you should make some changes in the code.
Step 2: encoding.
Create an ASP. NET application. In the default WebForm1.aspx, drag and drop a DataGrid control to display the content in Excel. Open the background encoding file and reference the namespace System. Data. OleDb, because this namespace will be used in subsequent code. Insert the following code in the Page_Load () event:
// Create a string variable. "Data Source" is the location of the. Xls file
String strConnection = "Provider = Microsoft. Jet. OLEDB.4.0;" +
"Data Source =" + Server. MapPath ("../ExcelData.xls") + ";" +
"Extended Properties = Excel 8.0 ;";
// Create a connection object
OleDbConnection objConnection = new OleDbConnection (strConnection );
// Create an OleDbCommand object. MyRange1 is the region name defined in the Excel file
OleDbCommand objCmdSelect = new OleDbCommand ("SELECT * FROM myRange1", objConnection );
OleDbDataAdapter objAdapter = new OleDbDataAdapter ();
DataSet dsExcel = new DataSet ();
// Open the database connection
ObjConnection. Open ();
ObjAdapter. SelectCommand = objCmdSelect;
ObjAdapter. Fill (dsExcel );
// Bind data to the DataGrid control
DataGrid1.DataSource = dsExcel. Tables [0]. DefaultView;
DataGrid1.DataBind ();
// Close the database connection
ObjConnection. Close ();
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