1    Excel

1.1  生成Excel

  • 生成excel
  @Override
    public void makExcel() throws DBusinessException, IOException {

        //0 设置前4列样式
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("wdd.xlsx");
        sheet.setColumnWidth(0,25*200);
        sheet.setColumnWidth(1,25*200);
        sheet.setColumnWidth(2,25*200);
        sheet.setColumnWidth(3,25*200);

        //1 第一行标题
        CellStyle createCellStyle = workbook.createCellStyle();
        createCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
        createCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

        HSSFRow row0 = sheet.createRow(0);
        row0.setHeight((short) (40*20));
        Cell cell0 = row0.createCell(0,Cell.CELL_TYPE_STRING);

        Font font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 18);
        createCellStyle.setFont(font);
        cell0.setCellStyle(createCellStyle);

        //合并单元格  起始行,截至行,起始列,截至列
        sheet.addMergedRegion(new CellRangeAddress(0,0,0,7));

        cell0.setCellValue("学生成绩表");

        //2 第二行标题
        HSSFCellStyle style = workbook.createCellStyle();
        HSSFCellStyle style1 = workbook.createCellStyle();
        HSSFDataFormat format = workbook.createDataFormat();
        style.setDataFormat(format.getFormat("@"));
        style.setDataFormat(format.getFormat("yyyy-MM-dd"));
        sheet.setDefaultColumnStyle(0,style);
        sheet.setDefaultColumnStyle(1,style);
        sheet.setDefaultColumnStyle(2,style1);
        sheet.setDefaultColumnStyle(3,style);

        HSSFRow row = sheet.createRow(1);
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("学生编号");
        HSSFCell cell1 = row.createCell(1);
        cell1.setCellValue("学生姓名");
        HSSFCell cell2 = row.createCell(2);
        cell2.setCellValue("入学日期");
        HSSFCell cell3 = row.createCell(3);
        cell3.setCellValue("总分");

        //3 第3行数据
        HSSFRow rowData = sheet.createRow(2);
        HSSFCell cellData = rowData.createCell(0);
        cellData.setCellValue("111");
        HSSFCell cellData1 = rowData.createCell(1);
        cellData1.setCellValue("魏子然");
        HSSFCell cellData2 = rowData.createCell(2);
        cellData2.setCellValue("2023-07-23");
        HSSFCell cellData3 = rowData.createCell(3);
        cellData3.setCellValue("100");

        //4 组装文件
        FileOutputStream outStream = new FileOutputStream("D:\\temp1.xlsx");
        try {
            workbook.write(outStream);
        } catch (IOException e) {
            log.error("CallServerServiceImpl->makExcel->throw1",e);
            throw new DBusinessException("1111111",e.getMessage(),"wddSyso");
        } finally {
            try {
                outStream.close();
            } catch (IOException e) {
                log.error("CallServerServiceImpl->makExcel->throw2",e);
                throw new DBusinessException("1111111",e.getMessage(),"wddSyso");
            }
        }
    }

 

  • 测试结果:

 

 

 

 

 

1.2  Excel上传判重

新建HashMap,使用唯一性字段(如果多个就用多个拼接)放到HashMap的key中,for循环用HashMap的containskey方法,判断重复

 

2    Word

posted on 2022-02-10 06:49  菜鸟乙  阅读(63)  评论(0)    收藏  举报