string path1 = Server.MapPath(@"Muban/LKJSearch.xls"); //获取模板 string filename = Session["Uid"].ToString() + DateTime.Now.ToString("yyyyMMddhhmmssms") + ".xls"; //命名临时文件名 string path2 = Server.MapPath(@"Save/" + filename); //临时文件路径 File.Copy(path1, path2); //复制模板创建临时文件 MOIExcel.Application ea = new MOIExcel.ApplicationClass(); ea.Visible = false; //设置为不可见 MOIExcel.Workbooks wbks = ea.Workbooks; try { MOIExcel._Workbook wbk = wbks.Open(path2, false); MOIExcel._Worksheet ws = (MOIExcel._Worksheet)wbk.Sheets.get_Item(1); //获取第一个工作表,准备操作 List<LKJSearch> listlkj = SearchLKJ(); int m = 5; for (int i = 0; i < listlkj.Count; i++) { List<Questions> lkjq = context.Questions.Where(p => p.Q_CheckNumber == listlkj[i].LKJ_Code).ToList(); if (lkjq.Count > 0) { for (int j = 0; j < lkjq.Count; j++) { ws.Cells[m, 1] = i + 1; //向指定单元格填入数据 ws.Cells[m, 2] = ((DateTime)listlkj[i].LKJ_CheckTime).ToString("MM.dd"); //…… //边框 var rowall = ws.get_Range(ws.Cells[m, 1], ws.Cells[m, 13]); rowall.Borders.LineStyle = MOIExcel.XlLineStyle.xlContinuous; } } } wbk.Save(); //保存 Response.Redirect(@"~/Save/" + filename); //导出 } catch { } finally { wbks.Close(); ea.Quit(); ea = null; }
需要添加office的引用,并且引用命名空间
部署网站的时候,有些权限需要修改,这点比较麻烦,后来换用NPOI
时间: 2024-10-11 01:34:59