1.服务器通过生成文件流,进行返回。界面中隐藏一个超链接,指向这个服务器的方法。下载文件,触发这个隐藏链接的单击事件。缺点是,超链接url的长度有限制,不适合较多数据。
//导出function akreportfacade_export() { var searchString = $('#akreportfacade_searchstring').val(); var line = encodeURIComponent($('#akreportfacade_line').val()); var station = encodeURIComponent($('#akreportfacade_station').val()); var begin = encodeURIComponent($('#akreportfacade_begin').datetimebox('getValue')); var end = encodeURIComponent($('#akreportfacade_end').datetimebox('getValue')); var href = "/AkReportFacade/Export?searchString=" + searchString + "&line=" + line + "&station=" + station + "&begin=" + begin + "&end=" + end; $("#akreportfacade_export_href").attr("href", href); var a = document.getElementById("akreportfacade_export_href"); akfacade_invokeClick(a);}function akfacade_invokeClick(element) { if (element.click) element.click(); //判断是否支持click() 事件 else if (element.fireEvent) element.fireEvent('onclick'); //触发click() 事件 else if (document.createEvent) { var evt = document.createEvent("MouseEvents"); //创建click() 事件 evt.initEvent("click", true, true); //初始化click() 事件 element.dispatchEvent(evt); //分发click() 事件 }}
public void Export(){ string searchString = Request["searchString"]; string line = Request["line"]; string station = Request["station"]; string begin = Request["begin"]; string end = Request["end"]; Response.Clear(); Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", "Facade.xlsx")); NPOI.XSSF.UserModel.XSSFWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(); NPOI.SS.UserModel.ISheet sheet1 = workbook.CreateSheet("Facade"); //excel格式化 NPOI.SS.UserModel.ICellStyle dateStyle = workbook.CreateCellStyle(); dateStyle.DataFormat = workbook.CreateDataFormat().GetFormat("yyyy/m/d h:mm:ss"); NPOI.SS.UserModel.ICellStyle numberStyle = workbook.CreateCellStyle(); numberStyle.DataFormat = workbook.CreateDataFormat().GetFormat("0.00000"); NPOI.SS.UserModel.ICellStyle textStyle = workbook.CreateCellStyle(); textStyle.DataFormat = workbook.CreateDataFormat().GetFormat("@"); //给sheet1添加第一行的头部标题 NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0); row1.CreateCell(0).SetCellValue("工单号"); row1.CreateCell(1).SetCellValue("条码"); row1.CreateCell(2).SetCellValue("检测结果"); row1.CreateCell(3).SetCellValue("不良原因"); row1.CreateCell(4).SetCellValue("备注"); row1.CreateCell(5).SetCellValue("员工"); row1.CreateCell(6).SetCellValue("线别"); row1.CreateCell(7).SetCellValue("工位"); row1.CreateCell(8).SetCellValue("时间"); //将数据逐步写入sheet1各个行 string strSql = "where AkFacade.BarCode like '%@param%' and (AkFacade.DateTime between '@begin' and '@end') and AkFacade.LineTitle like '%@line%' and AkFacade.StationTitle like '%@station%'"; strSql = strSql.Replace("@param", searchString); strSql = strSql.Replace("@begin", begin); strSql = strSql.Replace("@end", end); strSql = strSql.Replace("@line", line); strSql = strSql.Replace("@station", station); ListpageResult = _akFacadeRepository.GetPageList(0, 100000, strSql, "order by AkFacade.Id desc"); for (int i = 0; i < pageResult.Count; i++) { //double.Parse(pageResult[i].MaterialSum.ToString()) NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1); rowtemp.CreateCell(0).SetCellValue(pageResult[i].OrderNumber); rowtemp.CreateCell(1).SetCellValue(pageResult[i].BarCode); rowtemp.CreateCell(2).SetCellValue(pageResult[i].Result); rowtemp.CreateCell(3).SetCellValue(pageResult[i].BadReason); rowtemp.CreateCell(4).SetCellValue(pageResult[i].Remark); rowtemp.CreateCell(5).SetCellValue(pageResult[i].Employee); rowtemp.CreateCell(6).SetCellValue(pageResult[i].LineTitle); rowtemp.CreateCell(7).SetCellValue(pageResult[i].StationTitle); rowtemp.CreateCell(8).SetCellValue(pageResult[i].DateTime); rowtemp.GetCell(0).CellStyle = textStyle; rowtemp.GetCell(1).CellStyle = textStyle; rowtemp.GetCell(2).CellStyle = textStyle; rowtemp.GetCell(3).CellStyle = textStyle; rowtemp.GetCell(4).CellStyle = textStyle; rowtemp.GetCell(5).CellStyle = textStyle; rowtemp.GetCell(6).CellStyle = textStyle; rowtemp.GetCell(7).CellStyle = textStyle; rowtemp.GetCell(8).CellStyle = dateStyle; } //写入到客户端 System.IO.MemoryStream ms = new System.IO.MemoryStream(); workbook.Write(ms); Response.BinaryWrite(ms.ToArray()); Response.Flush(); Response.End();}
2.在服务器上生成一个具体的文件,保存在服务器上。然后返回一个文件的URL链接,客户端载入这个文件的链接进行下载。好处是可以通过ajax提交数据进行下载,缺点是要控制好服务器上的文件。
//导出 function akpalletdetail_search_export() { var searchString = $('#akpalletdetail_search_searchstring').val(); var begin = $('#akpalletdetail_begin').datetimebox('getValue'); var end = $('#akpalletdetail_end').datetimebox('getValue'); var type = $('#akpalletdetail_batchsearch_type').val(); var dt = $('#akpalletdetail_batchsearch_data').val(); $.ajax({ type: "POST", url: "AkPalletDetail/Export", data: { "searchString": searchString, "begin": begin, "end": end, "type": type, "data": dt }, success: function (data) { var result = eval('(' + data + ')'); if (result.Success == "false") { $.show_warning("提示", result.Message); } else { window.open(result.Message); } } }); }
//导出public string Export(){ ResponseMessage responseMessage = new ResponseMessage(); try { string searchString = Request["searchString"]; string begin = Request["begin"]; string end = Request["end"]; string type = Request["type"]; string data = "'" + Request["data"].Replace("\n", "','") + "'"; NPOI.XSSF.UserModel.XSSFWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(); NPOI.SS.UserModel.ISheet sheet1 = workbook.CreateSheet("包装"); //excel格式化 NPOI.SS.UserModel.ICellStyle dateStyle = workbook.CreateCellStyle(); dateStyle.DataFormat = workbook.CreateDataFormat().GetFormat("yyyy/m/d h:mm:ss"); NPOI.SS.UserModel.ICellStyle numberStyle = workbook.CreateCellStyle(); numberStyle.DataFormat = workbook.CreateDataFormat().GetFormat("0.00000"); NPOI.SS.UserModel.ICellStyle textStyle = workbook.CreateCellStyle(); textStyle.DataFormat = workbook.CreateDataFormat().GetFormat("@"); //给sheet1添加第一行的头部标题 NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0); row1.CreateCell(0).SetCellValue("托盘号"); row1.CreateCell(1).SetCellValue("工单号"); row1.CreateCell(2).SetCellValue("包装备注"); row1.CreateCell(3).SetCellValue("包装员工"); row1.CreateCell(4).SetCellValue("包装时间"); row1.CreateCell(5).SetCellValue("条码"); row1.CreateCell(6).SetCellValue("档位名称"); row1.CreateCell(7).SetCellValue("规格"); row1.CreateCell(8).SetCellValue("产品等级"); row1.CreateCell(9).SetCellValue("电池片等级"); row1.CreateCell(10).SetCellValue("FQC不良"); row1.CreateCell(11).SetCellValue("判定结果"); row1.CreateCell(12).SetCellValue("FQC备注"); row1.CreateCell(13).SetCellValue("FQC员工"); row1.CreateCell(14).SetCellValue("FQC时间"); row1.CreateCell(15).SetCellValue("线别"); row1.CreateCell(16).SetCellValue("工位"); row1.CreateCell(17).SetCellValue("Pmax"); row1.CreateCell(18).SetCellValue("Ipm"); row1.CreateCell(19).SetCellValue("Eff"); row1.CreateCell(20).SetCellValue("Isc"); row1.CreateCell(21).SetCellValue("Voc"); row1.CreateCell(22).SetCellValue("Rs"); row1.CreateCell(23).SetCellValue("Rsh"); row1.CreateCell(24).SetCellValue("Vpm"); row1.CreateCell(25).SetCellValue("FF"); row1.CreateCell(26).SetCellValue("Sun"); row1.CreateCell(27).SetCellValue("Temp"); row1.CreateCell(28).SetCellValue("Class"); //将数据逐步写入sheet1各个行 string strSql = ""; if (!string.IsNullOrEmpty(searchString)) { strSql = "where AkPalletDetail.PalletNumber like '%@param%' or AkPalletDetail.BarCode like '%@param%'"; strSql = strSql.Replace("@param", searchString); } else { strSql = "where AkPallet.PackDate between '@begin' and '@end'"; strSql = strSql.Replace("@begin", begin); strSql = strSql.Replace("@end", end); } //如果有文件,上面的条件都不起作用 if (!string.IsNullOrEmpty(type) && !string.IsNullOrEmpty(data) && data.Length > 5) { if (type == "托盘号") { strSql = "where AkPalletDetail.PalletNumber in(" + data + ")"; } else { strSql = "where AkPalletDetail.BarCode in(" + data + ")"; } } ListpageResult = _akPalletDetailRepository.GetPageList(0, 10000, strSql, "order by AkPalletDetail.Id desc"); for (int i = 0; i < pageResult.Count; i++) { //double.Parse(pageResult[i].MaterialSum.ToString()) NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1); rowtemp.CreateCell(0).SetCellValue(pageResult[i].PalletNumber); rowtemp.CreateCell(1).SetCellValue(pageResult[i].OrderNumber); rowtemp.CreateCell(2).SetCellValue(pageResult[i].PackRemark); rowtemp.CreateCell(3).SetCellValue(pageResult[i].PackEmployee); rowtemp.CreateCell(4).SetCellValue(pageResult[i].PackDate); rowtemp.CreateCell(5).SetCellValue(pageResult[i].BarCode); rowtemp.CreateCell(6).SetCellValue(pageResult[i].Title); rowtemp.CreateCell(7).SetCellValue(pageResult[i].Spec); rowtemp.CreateCell(8).SetCellValue(pageResult[i].ProductLevel); rowtemp.CreateCell(9).SetCellValue(pageResult[i].BatteryLevel); rowtemp.CreateCell(10).SetCellValue(pageResult[i].BadReason); rowtemp.CreateCell(11).SetCellValue(pageResult[i].JudgeResult); rowtemp.CreateCell(12).SetCellValue(pageResult[i].Remark); rowtemp.CreateCell(13).SetCellValue(pageResult[i].Employee); rowtemp.CreateCell(14).SetCellValue(pageResult[i].FqcDate); rowtemp.CreateCell(15).SetCellValue(pageResult[i].LineTitle); rowtemp.CreateCell(16).SetCellValue(pageResult[i].StationTitle); rowtemp.CreateCell(17).SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].Pmax))); rowtemp.CreateCell(18).SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].Ipm))); rowtemp.CreateCell(19).SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].Eff))); rowtemp.CreateCell(20).SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].Isc))); rowtemp.CreateCell(21).SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].Voc))); rowtemp.CreateCell(22).SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].Rs))); rowtemp.CreateCell(23).SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].Rsh))); rowtemp.CreateCell(24).SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].Vpm))); rowtemp.CreateCell(25).SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].FF))); rowtemp.CreateCell(26).SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].Sun))); rowtemp.CreateCell(27).SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].Temp))); rowtemp.CreateCell(28).SetCellValue(pageResult[i].Class); rowtemp.GetCell(0).CellStyle = textStyle; rowtemp.GetCell(1).CellStyle = textStyle; rowtemp.GetCell(2).CellStyle = textStyle; rowtemp.GetCell(3).CellStyle = textStyle; rowtemp.GetCell(4).CellStyle = dateStyle; rowtemp.GetCell(5).CellStyle = textStyle; rowtemp.GetCell(6).CellStyle = textStyle; rowtemp.GetCell(7).CellStyle = textStyle; rowtemp.GetCell(8).CellStyle = textStyle; rowtemp.GetCell(9).CellStyle = textStyle; rowtemp.GetCell(10).CellStyle = textStyle; rowtemp.GetCell(11).CellStyle = textStyle; rowtemp.GetCell(12).CellStyle = textStyle; rowtemp.GetCell(13).CellStyle = textStyle; rowtemp.GetCell(14).CellStyle = dateStyle; rowtemp.GetCell(15).CellStyle = textStyle; rowtemp.GetCell(16).CellStyle = textStyle; rowtemp.GetCell(17).CellStyle = numberStyle; rowtemp.GetCell(18).CellStyle = numberStyle; rowtemp.GetCell(19).CellStyle = numberStyle; rowtemp.GetCell(20).CellStyle = numberStyle; rowtemp.GetCell(21).CellStyle = numberStyle; rowtemp.GetCell(22).CellStyle = numberStyle; rowtemp.GetCell(23).CellStyle = numberStyle; rowtemp.GetCell(24).CellStyle = numberStyle; rowtemp.GetCell(25).CellStyle = numberStyle; rowtemp.GetCell(26).CellStyle = numberStyle; rowtemp.GetCell(27).CellStyle = numberStyle; rowtemp.GetCell(28).CellStyle = textStyle; } string file = "包装_" + DateTime.Now.ToString("mmss") + ".xlsx"; string path = Server.MapPath("~") + @"Content\File\" + file; if (System.IO.File.Exists(path)) System.IO.File.Delete(path); using (FileStream fs = System.IO.File.OpenWrite(path)) { workbook.Write(fs);//向打开的这个xls文件中写入并保存。 } responseMessage.Message = @"/Content/File/" + file; responseMessage.Success = "true"; return JSON.Instance.ToJSON(responseMessage, JSONConfig.GetJSONParameters()); } catch (Exception exception) { responseMessage.Message = exception.ToString(); responseMessage.Success = "false"; return JSON.Instance.ToJSON(responseMessage, JSONConfig.GetJSONParameters()); }}