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