博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
生成文件下载
阅读量:6294 次
发布时间:2019-06-22

本文共 14264 字,大约阅读时间需要 47 分钟。

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);    List
pageResult = _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 + ")";            }        }        List
pageResult = _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()); }}

 

转载地址:http://wmdta.baihongyu.com/

你可能感兴趣的文章
前端Vue:函数式组件
查看>>
程鑫峰:1.26特朗.普力挺美元力挽狂澜,伦敦金行情分析
查看>>
safari下video标签无法播放视频的问题
查看>>
01 iOS中UISearchBar 如何更改背景颜色,如何去掉两条黑线
查看>>
对象的继承及对象相关内容探究
查看>>
Spring: IOC容器的实现
查看>>
Serverless五大优势,成本和规模不是最重要的,这点才是
查看>>
Nginx 极简入门教程!
查看>>
iOS BLE 开发小记[4] 如何实现 CoreBluetooth 后台运行模式
查看>>
Item 23 不要在代码中使用新的原生态类型(raw type)
查看>>
为网页添加留言功能
查看>>
JavaScript—数组(17)
查看>>
Android 密钥保护和 C/S 网络传输安全理论指南
查看>>
以太坊ERC20代币合约优化版
查看>>
Why I Began
查看>>
同一台电脑上Windows 7和Ubuntu 14.04的CPU温度和GPU温度对比
查看>>
js数组的操作
查看>>
springmvc Could not write content: No serializer
查看>>
Python系语言发展综述
查看>>
新手 开博
查看>>