asp.net导入后台代码

  public void Upload(string information){

int Bank = 0;
for (int i = 0; i <Request.Files.Count; i++)
{
var httpfile = Request.Files[i];
string getFileName= httpfile.FileName;
string getfile= getFileName.Substring(getFileName.LastIndexOf("."));
IWorkbook workbook = null;
if (getfile.Equals(".xls"))
{
workbook = new HSSFWorkbook(httpfile.InputStream);
}
if (getfile.Equals(".xlsx"))
{
workbook = new XSSFWorkbook(httpfile.InputStream);
}
ISheet sheet= workbook.GetSheetAt(0);

for (int k = 1; k <7; k++)
{
IRow cells = sheet.GetRow(k);
string sql = string.Format("insert into " + information + "(StudentName,StudentAge) values(");
for (int j = 0; j < cells.LastCellNum; j++)
{
string value = cells.GetCell(j)+"";
sql += string.Format("'" + value.ToString() + "',");
}
sql = sql.Substring(0, sql.Length - 1) + ")";
Bank= DBHelper1.ExecuteNonQuery(sql);
}
}

}

猜你喜欢

转载自www.cnblogs.com/LYYXXXX/p/10386101.html