C #. Net:excel npoi Import and export operation tutorial Read and write an Excel file to a database table, sample sharing

Source: Internet
Author: User

using (FileStream FileReader = File.openread (@ "C:\Users\Administrator\Desktop\112.xls"))
        {
Create a Workbook object to receive a file stream (Excel information)
Iworkbook workbook = new Hssfworkbook (filereader);
There are several tables in the workbook
int count = workbook. Numberofsheets;
Create a worksheet to read workbook table information
Isheet sheet= Workbook. Getsheet ("table name");
isheet sheet = workbook. Getsheetat (0);
String sql = @ "INSERT into T_excelin (Name,remarks) VALUES (@Name, @Remarks)";
int II = 0;
R = 1, excluding the table header row
for (int r = 1; r <= Sheet. Lastrownum; r++)
            {
Defining a parameter array para
sqlparameter[] para = new sqlparameter[] {
New SqlParameter ("Name", sqldbtype.nvarchar,50),
New SqlParameter ("Remarks", sqldbtype.nvarchar,50)
                };
Create a row to get sheet rows of data
IRow row = sheet. GetRow (r);
list<icell> Listcell = new list<icell> ();
int c = 1 that is, the ID column is not read automatically numbered
for (int c = 1; c < row. Lastcellnum; C + +)
                {
Adds the value of each cell in each row with the Listcell collection
Listcell. ADD (row. Getcell (c));
                }
Cyclic assignment to Para
for (int i = 0; i < Listcell. Count; i++)
                {
Para[i]. Value = Listcell[i]. ToString ();
                }
Execute ADD SQL statement
II + = sqlhelper.executenonquery (SQL, para);

}
if (ii > 0)
            {
Response.Write ("<script>alert (' Insert Excel data into database table ') </script>");
            }
Else
            {
Response.Write ("<script>alert (') ' Excel data inserted into the database table failed! ') </script> ");
            }

        }

———————————— parameter passing does not pass through the List<> collection ————————————

using (FileStream FileReader = File.openread (@ "C:\Users\Administrator\Desktop\112.xls"))
{
//Create Workbook object receive file stream (Excel information)
Iworkbook workbook = new Hssfworkbook (FileReader);
//workbooks have several tables
//int count = workbook. Numberofsheets;
//Create worksheet to read workbook table information
Isheet sheet= Workbook. Getsheet ("table name");
Isheet sheet = workbook. Getsheetat (0);
String sql = @ "INSERT into T_excelin (Name,remarks) VALUES (@Name, @Remarks)";
int II = 0;
//r = 1, excluding the table header row
for (int r = 1; r <= Sheet. Lastrownum; r++)
{
//define parameter array para
Sqlparameter[] para = new sqlparameter[] {
New SqlParameter ("Name", sqldbtype.nvarchar,50),
New SqlParameter ("Remarks", sqldbtype.nvarchar,50)
};
//Create a row to get sheet rows of data
IRow row = sheet. GetRow (R);
//list<icell> Listcell = new list<icell> ();
//int C = 1 that is, the ID column is not read automatically numbered
for (int c = 1; c < row. Lastcellnum; C + +)
{
//Add the value of each cell in each row with the Listcell collection
//listcell. ADD (row. Getcell (c));
//Add data for each cell of each row to para c-1 that is, start with 0 to record the parameters
PARA[C-1]. Value = row. Getcell (c). ToString ();
}
//cyclic assignment to para
//for (int i = 0; i < Listcell. Count; i++)
                //{
//para[i]. Value = Listcell[i]. ToString ();
                //}
//Execute ADD SQL statement
II = sqlhelper.executenonquery (SQL, para);
}
if (ii > 0)
{
Response.Write ("<script>alert (' Insert Excel data into database table ') </script>");
}
Else
{
Response.Write ("<script>alert (') ' Excel data inserted into the database table failed! ') </script> ");
}

}

C #. Net:excel npoi Import and export operation tutorial Read and write an Excel file to a database table, sample sharing

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.