• jxl 导入导出Excel(有模板)


    1、导入

        @Override
        public String importBusinessScope(File file, String unit_id) throws Exception {
            Workbook wb = Workbook.getWorkbook(file);// 从文件流中取得Excel工作区对象
            Sheet sheet = wb.getSheet(0);
    
            Range[] rangeCell = sheet.getMergedCells();// 合并单元格
    
            StringBuilder validateMessage = new StringBuilder();
            BusinessScopeItem businessScopeItem = null;
            BusinessScopeSubItem businessScopeSubItem = null;
            List<BusinessScopeSubItem> businessScopeSubItemList = new ArrayList<>();
            for (int i = 4; i < sheet.getRows(); i++) {
                businessScopeItem = new BusinessScopeItem();
                businessScopeSubItem = new BusinessScopeSubItem();
                // 事项
                String item_id = "";
                String item = sheet.getCell(1, i).getContents();
                for (Range r : rangeCell) {
                    if (i > r.getTopLeft().getRow() && i <= r.getBottomRight().getRow() && 1 >= r.getTopLeft().getColumn()
                            && 1 <= r.getBottomRight().getColumn()) {
                        item = sheet.getCell(r.getTopLeft().getColumn(), r.getTopLeft().getRow()).getContents();
                    }
                }
                businessScopeItem.setItem(item);
                businessScopeItem.setIsdel("0");
                List<BusinessScopeItem> businessScopeItemList = businessScopeItemDao.selectAllItemList(businessScopeItem);
                if (businessScopeItemList != null && businessScopeItemList.size() > 0) {
                    item_id = businessScopeItemList.get(0).getItem_id();
                } else {
                    businessScopeItem.setUnit_id(unit_id);
                    businessScopeItem.setCreatedate(DateTime.toLocaleStringYYYYMMDD());
                    item_id = businessScopeItemDao.insertItem(businessScopeItem);
                }
    
                // 子事项
                String subitem = sheet.getCell(2, i).getContents();
                for (Range r : rangeCell) {
                    if (i > r.getTopLeft().getRow() && i <= r.getBottomRight().getRow() && 2 >= r.getTopLeft().getColumn()
                            && 2 <= r.getBottomRight().getColumn()) {
                        subitem = sheet.getCell(r.getTopLeft().getColumn(), r.getTopLeft().getRow()).getContents();
                    }
                }
                businessScopeSubItem.setItem_id(item_id);
                businessScopeSubItem.setSubitem(subitem);
                businessScopeSubItem.setIsdel("0");
                int count = businessScopeSubItemDao.selectCountSubItemByItemAndSubItem(businessScopeSubItem);
                if (count > 0) {
                    validateMessage.append("第").append(i + 1).append("行,事项(").append(item).append(")和子事项(").append(subitem)
                            .append(")已存在;");
                } else {
                    businessScopeSubItem.setItem(item);
                }
    
                // 主要内容
                String content = sheet.getCell(3, i).getContents();
                for (Range r : rangeCell) {
                    if (i > r.getTopLeft().getRow() && i <= r.getBottomRight().getRow() && 3 >= r.getTopLeft().getColumn()
                            && 3 <= r.getBottomRight().getColumn()) {
                        content = sheet.getCell(r.getTopLeft().getColumn(), r.getTopLeft().getRow()).getContents();
                    }
                }
                businessScopeSubItem.setContent(content);
    
                // 实施依据
                String rules = sheet.getCell(4, i).getContents();
                for (Range r : rangeCell) {
                    if (i > r.getTopLeft().getRow() && i <= r.getBottomRight().getRow() && 4 >= r.getTopLeft().getColumn()
                            && 4 <= r.getBottomRight().getColumn()) {
                        rules = sheet.getCell(r.getTopLeft().getColumn(), r.getTopLeft().getRow()).getContents();
                    }
                }
                businessScopeSubItem.setRules(rules);
    
                // 实施期限
                String limitdate = sheet.getCell(5, i).getContents();
                for (Range r : rangeCell) {
                    if (i > r.getTopLeft().getRow() && i <= r.getBottomRight().getRow() && 5 >= r.getTopLeft().getColumn()
                            && 5 <= r.getBottomRight().getColumn()) {
                        limitdate = sheet.getCell(r.getTopLeft().getColumn(), r.getTopLeft().getRow()).getContents();
                    }
                }
                businessScopeSubItem.setLimitdate(limitdate);
    
                // 创建日期
                businessScopeSubItem.setCreatedate(DateTime.toLocaleStringYYYYMMDD());
                businessScopeSubItemList.add(businessScopeSubItem);
            }
            wb.close();
    
            if (validateMessage.length() > 0) {
                return validateMessage.toString();
            } else {
                for (BusinessScopeSubItem subItem : businessScopeSubItemList) {
                    businessScopeSubItemDao.insertSubItem(subItem);
                }
            }
    
            return "0";
        }
    View Code

    2、导出

        @Override
        public String exportBusinessScope(BusinessScopeItem businessScopeItem, HttpServletResponse response)
                throws Exception {
            // 获取导出数据
            List<BusinessScopeItem> businessScopeItemList = businessScopeItemDao.selectAllBusinessScopeList(businessScopeItem);
            if (businessScopeItemList != null && businessScopeItemList.size() > 0) {
                // 模板入径
                String templePath = getClass().getResource("/").getPath();
                int index = templePath.indexOf("WebContent");
                templePath = templePath.substring(0, index + 10) + "/website/enroll/businessscope/businessScopeModel.xls";
    
                // 读取模板文件
                Workbook wb = Workbook.getWorkbook(new File(templePath));
                // 取得输出流
                OutputStream os = response.getOutputStream();
                // 清空输出流
                response.reset();
                // 下面是对中文文件名的处理,设置相应内容的编码格式
                response.setCharacterEncoding("UTF-8");
                // 设置文件的默认文件名
                response.setHeader("Content-Disposition", "attachment;filename=businessScopeModel.xls");
                // 定义输出类型
                response.setContentType("application/msexcel");
                // 建立Excel文件
                WritableWorkbook book = Workbook.createWorkbook(os, wb);
                // 通过索引,获取模板文件中的sheet页第一页
                WritableSheet sheet = book.getSheet(0);
                // 实例化表格格式
                // 设置单元格纵横居中
                WritableCellFormat wcfCenter = new WritableCellFormat();
                wcfCenter.setAlignment(Alignment.CENTRE);
                wcfCenter.setVerticalAlignment(VerticalAlignment.CENTRE);
                // 是否自动换行
                wcfCenter.setWrap(true); 
                // 设置表格线为细线,黑色
                wcfCenter.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
                
                // 设置单元格水平靠左,上下居中
                WritableCellFormat wcfLeft = new WritableCellFormat();
                wcfLeft.setAlignment(Alignment.LEFT);
                wcfLeft.setVerticalAlignment(VerticalAlignment.CENTRE);
                // 是否自动换行
                wcfLeft.setWrap(true); 
                // 设置表格线为细线,黑色
                wcfLeft.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
    
                // 写入Excel
                int i = 4;
                int seq = 0;
                for (BusinessScopeItem item : businessScopeItemList) {
                    List<BusinessScopeSubItem> businessScopeSubItemList = item.getBusinessScopeSubItemList();
                    int subItemCount = 0;
                    if (businessScopeSubItemList != null && businessScopeSubItemList.size() > 0) {
                        subItemCount = businessScopeSubItemList.size() -1;
                    }
                    // 是否需要合并单元格
                    if (subItemCount > 0) {
                        // 设置第1列、第4行到第1列、第subItemCount行合并
                        sheet.mergeCells(0, i, 0, i + subItemCount);
                        sheet.mergeCells(1, i, 1, i + subItemCount);
                    }
    
                    // 序号
                    seq++;
                    // 实例化单元格内容,并应用样式对单元格进行处理
                    Label label_Seq = new Label(0, i, String.valueOf(seq), wcfCenter);
                    // 将内容添加入单元格中
                    sheet.addCell(label_Seq);
    
                    // 事项
                    Label label_item = new Label(1, i, item.getItem(), wcfCenter);
                    sheet.addCell(label_item);
    
                    // 子事项相关信息
                    if (businessScopeSubItemList != null) {
                        int j = i;
                        for (BusinessScopeSubItem subItem : businessScopeSubItemList) {
                            // 子事项
                            Label label_subitem = new Label(2, j, subItem.getSubitem(), wcfCenter);
                            sheet.addCell(label_subitem);
    
                            // 主要内容
                            Label label_content = new Label(3, j, subItem.getContent(), wcfLeft);
                            sheet.addCell(label_content);
    
                            // 实施依据
                            Label label_rules = new Label(4, j, subItem.getRules(), wcfLeft);
                            sheet.addCell(label_rules);
    
                            // 实施期限
                            Label label_limitdate = new Label(5, j, subItem.getLimitdate(), wcfCenter);
                            sheet.addCell(label_limitdate);
    
                            // 重定向行
                            j++;
                        }
                    }
                    
                    // 重定向行
                    i += subItemCount + 1;
                }
                
                // 尾部
                // 设置行高
                sheet.setRowView(i, 1500, false);
                // 合并单元格
                sheet.mergeCells(0, i, 1, i);
                sheet.mergeCells(2, i, 5, i);
                // 填充数据
                Label label_bottom1 = new Label(0, i, "举办单位审核意见", wcfCenter);
                sheet.addCell(label_bottom1);
                
                // 设置单元格水平靠右,垂直靠下
                WritableCellFormat wcfBottom = new WritableCellFormat();
                wcfBottom.setAlignment(Alignment.RIGHT);
                wcfBottom.setVerticalAlignment(VerticalAlignment.BOTTOM);
                // 是否自动换行
                wcfBottom.setWrap(true);
                // 设置表格线为细线,黑色
                wcfBottom.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
                
                Calendar calendar = Calendar.getInstance();
                String year = String.valueOf(calendar.get(Calendar.YEAR));
                Label label_bottom2 = new Label(2, i, "(公章)                          
    " + year + "年    月    日                    
    ", wcfBottom);
                sheet.addCell(label_bottom2);
    
                book.write();// 写入表格
                book.close();// 结束表格编写
                os.close();// 关闭数据流
            } else {
                return "没有数据!";
            }
            return "0";
        }
    View Code
  • 相关阅读:
    hibernate和mybatis区别
    Spring事务的传播行为和隔离级别
    数组拷贝
    spring mvc 一次请求 两次查询
    手游性能之渲染分析3
    Android pm 命令详解
    Android am命令使用
    Android dumpsys命令详细使用
    java处理高并发高负载类网站的优化方法
    关于ArrayList的5道面试题
  • 原文地址:https://www.cnblogs.com/BobXie85/p/9474268.html
Copyright © 2020-2023  润新知