zoukankan      html  css  js  c++  java
  • Java web项目JXl导出excel,(从eclipse上移动到tomact服务器上,之路径更改)

    我用的是jxl导出excel,比较简单,最开始我是固定路径不能选择,很局限,后来改了,而且固定路径当把项目放在服务器上时,路径不可行。

    在网上各位大神的帮助成功设置响应头,并且可选保存路径。

    1.前端

    //不是用的ajax,好像ajax不能成功
    //js $("#exportbaofei").click(function(){ window.location.href="${pageContext.request.contextPath}/ExportBaoFeiServlet"; }); //html
    <input type="button" value="导出废模" id="exportbaofei"/>

    2.servlet

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            log.info("导出报废模的servlet");
            MuJUService muJUService  = new MuJUService();
            //查询
            List<Map<String, String>>list =muJUService.exportBaoFei();
            //弹框选择路径
            WritableWorkbook book = null;
            response.reset();
            response.setCharacterEncoding("UTF-8");// 设置字符集
            // 创建工作流
            OutputStream os = null;
            try {
                // 设置弹出对话框
                response.setContentType("application/DOWLOAD");
                response.setCharacterEncoding("UTF-8");
                // 设置工作表的标题
                response.setHeader("Content-Disposition", "attachment; filename=feimu_month.xls");// 设置生成的文件名字、、不能用汉字,没有设置汉字解码
                os = response.getOutputStream();
                // 初始化工作表
                book = Workbook.createWorkbook(os);
            } catch (IOException e1) {
    
                log.error("导出excel出现IO异常", e1);
              //  throw new ServiceException("导出失败", e1);
                e1.printStackTrace();
            }
            
            //excel格式设置
            WritableFont font1= new WritableFont(WritableFont.createFont("楷体 _GB2312"), 12, WritableFont.NO_BOLD); //设置字体格式为excel支持的格式 WritableFont font3=new WritableFont(WritableFont.createFont("楷体 _GB2312"), 12, WritableFont.NO_BOLD);
            WritableCellFormat format1=new WritableCellFormat(font1);
            //把水平对齐方式指定为居中
            try {
                format1.setAlignment(jxl.format.Alignment.CENTRE);
                //把垂直对齐方式指定为居中
                format1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
                //设置自动换行
                format1.setWrap(true);
            } catch (WriteException e1) {
                // TODO Auto-generated catch block
                log.error(e1);
                e1.printStackTrace();
            }
            
            // WritableWorkbook book = null;
                try {
                    // 打开文件
                   // book = Workbook.createWorkbook(new File(filename+".xls"));
                    // 生成名为"学生"的工作表,参数0表示这是第一页
                    WritableSheet sheet = book.createSheet("当月报废单", 0);
                    
                    sheet.addCell(new Label(0,0,"序号",format1));
                    for(int i = 1;i<12;i++){
                        sheet.setColumnView(i,15);
                    }
                    sheet.addCell(new Label(1,0,"下单时间",format1));
                    sheet.addCell(new Label(2,0,"返回时间",format1));
                    sheet.addCell(new Label(3,0,"维修单号",format1));
                    sheet.addCell(new Label(4,0,"产品图号",format1));
                    sheet.addCell(new Label(5,0,"模具图号",format1));
                    sheet.addCell(new Label(6,0,"模具规格",format1));
                    sheet.addCell(new Label(7,0,"维修模号",format1));
                    sheet.addCell(new Label(8,0,"维修数量",format1));
                    sheet.addCell(new Label(9,0,"维修原因",format1));
                    sheet.addCell(new Label(10,0,"报废模号",format1));
                    sheet.addCell(new Label(11,0,"报废数量",format1));
                    if(list!=null && !list.isEmpty()){
                        int rows =1;//hang
                        int cols = 0;//lie
                        for (Map<String, String> map : list) {
                            sheet.addCell(new Number(0, rows, rows));
                            sheet.addCell(new Label(1,rows,map.get("wx_xdan_data"),format1));
                            sheet.addCell(new Label(2,rows,map.get("wx_back_data"),format1));
                            sheet.addCell(new Label(3,rows,map.get("wx_danhao"),format1));
                            sheet.addCell(new Label(4,rows,map.get("wx_product_num"),format1));
                            sheet.addCell(new Label(5,rows,map.get("wx_mj_num"),format1));
                            sheet.addCell(new Label(6,rows,map.get("wx_mj_guige"),format1));
                            sheet.addCell(new Label(7,rows,map.get("wx_muhao"),format1));
                            sheet.addCell(new Label(8,rows,map.get("wx_count"),format1));
                            sheet.addCell(new Label(9,rows,map.get("wx_reason"),format1));
                            sheet.addCell(new Label(10,rows,map.get("wx_baofei_muhao"),format1));
                            sheet.addCell(new Label(11,rows,map.get("wx_baofei_count"),format1));
                            rows++;
                        }
                    }
                    book.write();
                    
                } catch (Exception e) {
                    log.error(e);
                    System.out.println(e);
                }finally{
                    if(book!=null){
                        try {
                            book.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        } 
                    }
                }
            
        }
  • 相关阅读:
    Java应用开发与实践
    大话存储:存储系统底层架构原理极限剖析(终极版)
    Excel 2016公式与函数应用大全
    Excel高效办公应用技巧
    业务弯路池子
    一个现象,
    为什么有时候 进入这么多次,一次是 38次,一次是 114次,
    恶心 从判断开始,
    but,
    这两个的意思是不同的。。。
  • 原文地址:https://www.cnblogs.com/stepbystepwhx/p/7782816.html
Copyright © 2011-2022 走看看