excel和word的读写

100000 行级别数据的 Excel 导入优化之路

excel

依赖

在这里插入图片描述

读入和导出

public class Excel {

    public static void main(String[] args) throws Exception {

        // 读取excel
        List<Map<String, String>> list = readExcel("E:/excelTest.xlsx");
        list.forEach(cell -> System.out.println(cell));

        // 导出excel
        String[] head = new String[]{"代偿编号", "姓名", "性别", "日期", "身份证号"};
        OutputStream out = new FileOutputStream("E:/excel.xlsx");
        writeExcel(list, head, out);
    }

    /**
     * 导出excel数据
     *
     * @param list 写出的表格内容
     * @param head 表头
     * @param out  输出流
     * @throws Exception
     */
    public static void writeExcel(List<Map<String, String>> list, String[] head, OutputStream out) throws Exception {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("学生信息");
        // 表头信息
        XSSFRow firstRow = sheet.createRow(0);
        for (int i = 0; i < head.length; i++) {
            XSSFCell cell = firstRow.createCell(i);
            cell.setCellValue(head[i]);
        }
        // 数据行
        for (int i = 0; i < list.size(); i++) {
            XSSFRow row = sheet.createRow(i + 1);
            for (int j = 0; j < head.length; j++) {
                XSSFCell cell = row.createCell(j);
                cell.setCellValue(list.get(i).get(head[j]));
            }
        }
        // 写出表格
        workbook.write(out);
    }


    /**
     * 读excel数据
     *
     * @param path
     * @return
     * @throws Exception
     */
    public static List<Map<String, String>> readExcel(String path) throws Exception {
        List<Map<String, String>> list = new ArrayList<>();

        InputStream inputStream = new FileInputStream(path);
        Workbook workbook = WorkbookFactory.create(inputStream);
        // 得到表
        Sheet sheet = workbook.getSheet("Sheet2");
        // 得到菜单行
        Row firstRow = sheet.getRow(0);
        // 得到总列数
        int physicalNumberOfCells = firstRow.getPhysicalNumberOfCells();
        // 得到总行数
        int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
//         遍历读取每个单元格的数据
        for (int i = 1; i < physicalNumberOfRows; i++) {
            Map<String, String> map = new HashMap<>();
            Row row = sheet.getRow(i);
            for (int j = 0; j < physicalNumberOfCells; j++) {
                Cell cell = row.getCell(j);
                int cellType = cell.getCellType();
                String val = "";
                if (cellType == Cell.CELL_TYPE_STRING) {// 判断cell数据类型是否是string
                    val = cell.getStringCellValue();
                } else if (cellType == Cell.CELL_TYPE_NUMERIC) {
                    if (DateUtil.isCellDateFormatted(cell)) {// 判断cell数据类型是否是date
                        Date dateCellValue = cell.getDateCellValue();
                        val = dateFormat(dateCellValue);
                    } else {
                        // 判断cell数据类型是否是double
                        double numericCellValue = cell.getNumericCellValue();
                        val = ((Integer)new Double(numericCellValue).intValue()).toString();
                    }
                }
                map.put(firstRow.getCell(j).getStringCellValue(), val);
            }
            list.add(map);
        }
        return list;
    }

    // 日期格式化
    private static String dateFormat(Date dateCell) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(dateCell);
    }
}

word导出(基于freamwork)

依赖

在这里插入图片描述

编辑word模板,另存为xml(表格数据需要手动修改xml文件)

在这里插入图片描述

测试代码

public class Word {
    public static void main(String[] args) throws Exception {
        Configuration configuration = new Configuration();
        // 加载并配置模板内容
        configuration.setDefaultEncoding("UTF-8");
        configuration.setClassForTemplateLoading(Word.class,"/com/javasm/word");
        Template template = configuration.getTemplate("购房合同.xml");
        Map<String,String> map = new HashMap<>();
        map.put("username","卡卡西");
        map.put("IDCard","610548199604051412");
        map.put("price","4836452.00");
        map.put("date","2020.8.11");

        // 输出流
        Writer out = new FileWriter("E:/购房合同.docx");
        template.process(map,out);

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值