nuget 添加Install-Package ExcelDataReader
包
//FilePath为上传的文件路径
public void Leading_in(string FilePath)
{
var file = new FileStream(FilePath, FileMode.Open, FileAccess.Read);//FilePath为文件的路径带名字,FileMode.Open为打开文件,FileAccess.Read为以只读的方式打开
var excel = Excel.ExcelReaderFactory.CreateOpenXmlReader(file);
var dataset = excel.AsDataSet();//将表格导出为dataset格式
//循环插入数据到数据库
Model.production model = new Model.production();
BLL.production bll = new BLL.production();
int i = 0;
var lujing = HttpContext.Current.Session["lujing"].ToString();
foreach (DataRow dr in dataset.Tables[0].Rows)
{
if (i != 0 && lujing!="")
{
DataTable dt = new DataTable();
dt = new BLL.production().GetList_pro(" production_].ToString();
if (bll.Update(model_1))
{
}
}
}
}
i++;
}
}
红色部分为引入包后的初始化