ASP. NET Excel Data Import Database

Source: Internet
Author: User
Tags import database

Today, I have made such a project, and I need to import the Excel data to the database. Although I have written it before, I forgot it. So today I found a better one to collect it.

 

Below isCode:

 

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
}
String Isxls = System. Io. Path. getextension (fileupload1.filename). tostring (). tolower (); // System. Io. Path. getextension get the file extension
If (Isxls ! =   " . Xls " )
{
Response. Write ( " <SCRIPT> alert ('only Excel file' 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
Fileupload1.saveas (savepath ); // Saveas saves the uploaded file content on the server
Dataset DS = Excelsqlconnection (savepath, filename ); // Call a custom Method
Datarow [] Dr = DS. Tables [ 0 ]. Select (); // Define a datarow Array
Int Rowsnum = DS. Tables [ 0 ]. Rows. count;
If (Rowsnum =   0 )
{
Response. Write ( " <SCRIPT> alert ('excel table is empty, no data! ') </SCRIPT> " ); // Prompt the user when the Excel table is empty
}
Else
{
For ( Int I =   0 ; I < Dr. length; I ++ )
{
// Except for creating a folder named "upfiles", you don't need to worry about it. You just 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 ();
String Customername = Dr [I] [ " Content Provider " ]. 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 Strcon =   " Provider = Microsoft. Jet. oledb.4.0; Data Source = "   + Filepath +   " ; Extended properties = 'excel 8.0; HDR = yes; IMEX = 1' " ;
Oledbconnection excelconn =   New Oledbconnection (strcon );
Try
{
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;
}
Catch
{
Excelconn. Close ();
Return   Null ;
}
}
# Endregion

 

 

The Excel format is as follows:

 

Title Link address Category Content Provider
Spoof Japanese Women 1 Http://fegnhuang.com Photo Phoenix video
Spoof Japanese women 2 Http://fegnhuang.com Photo Phoenix video
Spoof Japanese Women 3 Http://fegnhuang.com Funny Phoenix video
Spoof Japanese women 4 Http://fegnhuang.com Funny Phoenix video
Spoof Japanese women 5 Http://fegnhuang.com Funny Mango TV
Spoof Japanese women 6 Http://fegnhuang.com Beauty Mango TV
Spoof Japanese women 7 Http://fegnhuang.com Beauty Mango TV
Spoof Japanese Women 8 Http://fegnhuang.com Beauty Mango TV
Spoof Japanese women 9 Http://fegnhuang.com Fashion Sina video
Spoof Japanese women 10 Http://fegnhuang.com Fashion Sina video

 

 

Other implementation methods reference: http://blog.csdn.net/lee576/archive/2011/03/02/6217811.aspx

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.