单个生成:
string pathexecl=WebConfigurationManager.AppSettings["ContractExport"] + "\Driver\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xlsx";//生成后保存目录
string Official = WebConfigurationManager.AppSettings["Official"];//模版
IWorkbook wb = new XSSFWorkbook(Official); //使用模板
ISheet sheet1 = wb.GetSheet("Sheet1"); //选择第一页
DataTable dp = IntentionBLL.SerchDataExecl(name); //获取填入数据
IRow row = sheet1.CreateRow(1);
row.CreateCell(0).SetCellValue(dp.Rows[i]["Name"].ToString()); //表示在第二行第一列填入数据(1代表第二行 0代表第一列)
FileStream file = new FileStream(pathexecl, FileMode.Create); //创建已填充数据的execl表
wb.Write(file); //保存
file.Close();
多个打包成zip
string desktopPath = @"D:\DealerQRcode"; //选择文件夹跟zip保存路径
string returntext = DateTime.Now.ToString("yyyyMMddHHmmss") //生成文件唯一名称
string newStoragePath = desktopPath + @"\" + returntext; //生成文件夹保存路径
if (!Directory.Exists(newStoragePath ))//判断上传路径是否存在
{
Directory.CreateDirectory(newStoragePath );//不存在则进行创建
}
List<CarAndBindModel> carAndBindModels = CarBindBLL.GetCarDataB();//数据源
foreach (var item in carAndBindModels) //遍历
{
IWorkbook wb = new XSSFWorkbook(); //如果要使用模板,方法参照单个生成
string imgStoragePath = newStoragePath + @"\" + item.CarNum + ".xlsx";
FileStream file = new FileStream(imgStoragePath,FileMode.Create);//创建文件夹
wb.Write(file); //把生成的execl放入创建的文件夹中
file.Close(); //关闭流
}
ZipHelper.CreateZip(newStoragePath, newStoragePath + ".zip"); //把创建的文件夹打包成zip