标签:
添加引用
NPOI.dll
//需要引用的命名空间 using NPOI.HSSF.UserModel; string path1 = Server.MapPath(@"Muban/TCCheDuiZhang.xls"); //excel模板路径 string filename = Session["Uid"].ToString() + DateTime.Now.ToString("yyyyMMddhhmmssms") + ".xls"; //命名新的excel文件名 string path2 = Server.MapPath(@"Save/" + filename); //目标位置及文件名 // File.Copy(path1, path2); FileStream file = File.Open(path1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); //将模板读取到流中 FileStream file1 = new FileStream(path2, FileMode.Create); //创建一个新的流,用来保存 HSSFWorkbook hssfworkbook = new HSSFWorkbook(file); //excel工作簿 file.Close(); HSSFSheet hs = hssfworkbook.GetSheet("Sheet1") as HSSFSheet; //要操作的表,这里是默认的第一个表 try { var query = context.AddPersonInfo.Where(p => p.AI_Code == hfCode.Value); if (query.Count() > 0) { AddPersonInfo api = query.First(); hs.GetRow(1).GetCell(5).SetCellValue(((DateTime)api.AI_AddDate).ToString("yyyy年MM月dd日")); //向单元格填入数据 //…… hs.GetRow(6).GetCell(0).SetCellValue(api.AI_Aim); hs.GetRow(34).GetCell(0).SetCellValue(api.AI_Guidance); } } catch { } finally { //保存 hssfworkbook.Write(file1); file1.Close(); //关闭流 } Response.Redirect(@"~/Save/" + filename); //导出
标签:
原文地址:http://www.cnblogs.com/phantom-k/p/5503636.html