Import Excel file data to the database and support multiple worksheets

Source: Internet
Author: User

I read this article about importing batch Excel Data Into Oracle data. I cannot help but share a test example I wrote last year. This example is used in the background of a Web project I created last year. In fact, it is very simple to upload the Execl file and then read the data and write it into the database. At that time, I also searched for some information on the Internet, but there was a lot of ready-made code, but the problem occurred when I copied it and used it, the Excel file uploaded in the background can only read the content of the first worksheet, but cannot be read after the second worksheet, that is, the database cannot be imported after the second worksheet, when it was very depressing, I finally saw a solution from a netizen in a Foreign Forum. In fact, the code is very simple. I also simply encapsulated it. I don't need to talk about it anymore. paste the Code directly and I am done ..

Code /*************************************** *****************************
Filename: UpLoadExcelToSql. cs
Created: 2009/04/16
Author: liaoyun
Purpose: import data to the database based on the uploaded Excel File
**************************************** *****************************/
Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Collections. Generic;
Using System. Data. OleDb;
Using System. Text;
Using System. Data. SqlClient;

/// <Summary>
/// Summary of UpLoadExcelToSql
/// </Summary>
Public class UpLoadExcelToSql
{
Private string conStringExcel; // excel connection string
Private string excelFilePath; // Excel file path

// The connection string should be obtained from the configuration file
Private string connectionString = "Data Source = a3421387dce54fa \ SQL2005; Initial Catalog = Northwind; Integrated Security = True ";

/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "excelFileName"> Excel file name </param>
Public UpLoadExcelToSql (string excelFilePath)
{
This. excelFilePath = excelFilePath;
This. conStringExcel = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" +
ExcelFilePath +"
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.