How to import data rules from Excel tables to databases under ASP. NET

Source: Internet
Author: User

Today, I received a new requirement to display the data in the Excel sheet on the page.
In my personal analysis, I first need to save the data in Excel to the database, and then display it on the page. I am a newbie, and I have never read Excel data before, after studying (mainly looking at other people's documents) and writing the implementation process, I want to write a few articles about Excel, first of all, rule-based Excel Data Import, there is also irregular Excel data import, and Excel is generated based on the data.

Start: import the rule Excel to the database.
First, let's take a look at the Excel structure,
This is a simple and regular Excel format. Import it to the database.
Copy codeThe Code is as follows:
View Code
Protected void btnImport_Click (object sender, EventArgs e)
If (FileUpload1.HasFile = false) // HasFile is used to check whether a specified file exists in FileUpload.
Response. Write ("<script> alert ('select an Excel file') </script> ");
Return; // if no file exists, return
String IsXls = System. IO. Path. GetExtension (FileUpload1.FileName). ToString (). ToLower (); // System. IO. Path. GetExtension get the file extension
If (IsXls! = ". Xls ")
If (IsXls! = ". Xlsx ")
Response. Write ("<script> alert ('only Excel files' can be selected) </script> ");
Return; // If the selected file is not an Excel file
String filename = FileUpload1.FileName; // obtain the DateTime date function of the Execle file name.
String savePath = Server. MapPath ("upfiles \") + filename); // Server. MapPath obtains the relative path of the virtual Server.
FileUpload1.SaveAs (savePath); // SaveAs saves the uploaded file content on the server
DataSet ds = ExcelSqlConnection (savePath, filename, IsXls); // call a custom Method
DataRow [] dr = ds. Tables [0]. Select (); // defines a DataRow Array
Int rowsnum = ds. Tables [0]. Rows. Count;
If (rowsnum = 0)
Response. Write ("<script> alert ('excel table is empty, no data! ') </Script> "); // when the Excel table is empty, prompt the user
For (int I = 0; I <dr. Length; I ++)
// In addition to setting up an "upfiles" folder, you don't need to worry about it. You only need to obtain the Excel value in the following way, then insert these values into the database in your way.
String title = dr [I] ["title"]. ToString ();
String linkurl = dr [I] ["link address"]. ToString ();
String categoryname = dr [I] ["category"]. ToString ();
// Response. Write ("<script> alert ('imported content:" + ex. Message + "') </script> ");
Response. Write ("<script> alert ('excle table imported successfully! '); </Script> ");
# Region connect to Excel to read Excel Data and return the DataSet data set
/// <Summary>
/// Connect to Excel to read Excel Data and return the DataSet data set
/// </Summary>
/// <Param name = "filepath"> Excel server path </param>
/// <Param name = "tableName"> Excel table name </param>
/// <Returns> </returns>
Public static System. Data. DataSet ExcelSqlConnection (string filepath, string tableName, string IsXls)
String strCon = "";
If (IsXls = ". xls ")
StrCon = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + filepath + "; Extended Properties = 'excel 8.0; HDR = YES; IMEX = 1 '";
StrCon = "Provider = Microsoft. ACE. OLEDB.12.0; Data Source =" + filepath + "; Extended Properties = 'excel 12.0; HDR = YES; IMEX = 1 '";
OleDbConnection ExcelConn = new OleDbConnection (strCon );
String strCom = string. Format ("SELECT * FROM [Sheet1 $]");
ExcelConn. Open ();
OleDbDataAdapter myCommand = new OleDbDataAdapter (strCom, ExcelConn );
DataSet ds = new DataSet ();
MyCommand. Fill (ds, "[" + tableName + "$]");
ExcelConn. Close ();
Return ds;
ExcelConn. Close ();
Return null;
# Endregio

This code is modified on the basis of others' code. Previously, only the xls format can be imported, and the xlsx format is not supported. The main difference between the two formats is that
Xls Format:
Copy codeThe Code is as follows:
StrConn = "Provider = Microsoft. ACE. OLEDB.12.0; Data Source = '" + serverFilename + "'; Extended Properties = 'excel 12.0; HDR = YES '";

Xlsx format:
Copy codeThe Code is as follows:
StrConn = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = '" + serverFilename + "'; Extended Properties = 'excel 8.0; HDR = YES ;'";

Of course, you also need to connect to the database to import the database and create a table with the same structure.

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: 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.