Java 利用【poi.xssf】生成excel文件、并设置样式。

利用【poi.xssf】生成excel文件、并设置样式。
api官网:http://poi.apache.org/apidocs/index.html

代码实例:

String xlsPath = "E:测试.xlsx";
        
        // excel文档对象
        XSSFWorkbook wk = new XSSFWorkbook();
        // sheet对象
        XSSFSheet sheet = wk.createSheet("测试");
        
        // 字体样式
        XSSFFont xssfFont = wk.createFont();
        // 加粗
        xssfFont.setBold(true);
        // 字体名称
        xssfFont.setFontName("楷体");
        // 字体大小
        xssfFont.setFontHeight(12);
        
        // 表头样式
        XSSFCellStyle headStyle = wk.createCellStyle();
        // 设置字体css
        headStyle.setFont(xssfFont);
        // 竖向居中
        headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 横向居中
        headStyle.setAlignment(HorizontalAlignment.CENTER);
        // 边框
        headStyle.setBorderBottom(BorderStyle.THIN);
        headStyle.setBorderLeft(BorderStyle.THIN);
        headStyle.setBorderRight(BorderStyle.THIN);
        headStyle.setBorderTop(BorderStyle.THIN);
        
        // 内容字体样式
        XSSFFont contFont = wk.createFont();
        // 加粗
        contFont.setBold(false);
        // 字体名称
        contFont.setFontName("楷体");
        // 字体大小
        contFont.setFontHeight(11);
        // 内容样式
        XSSFCellStyle contentStyle = wk.createCellStyle();
        // 设置字体css
        contentStyle.setFont(contFont);
        // 竖向居中
        contentStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 横向居中
        //contentStyle.setAlignment(HorizontalAlignment.CENTER);
        // 边框
        contentStyle.setBorderBottom(BorderStyle.THIN);
        contentStyle.setBorderLeft(BorderStyle.THIN);
        contentStyle.setBorderRight(BorderStyle.THIN);
        contentStyle.setBorderTop(BorderStyle.THIN);
        
        // 自动换行
        contentStyle.setWrapText(true);
        
        // 数字样式
        XSSFCellStyle numStyle = wk.createCellStyle();
        // 设置字体css
        numStyle.setFont(contFont);
        // 竖向居中
        numStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 横向居中
        numStyle.setAlignment(HorizontalAlignment.CENTER);
        // 边框
        numStyle.setBorderBottom(BorderStyle.THIN);
        numStyle.setBorderLeft(BorderStyle.THIN);
        numStyle.setBorderRight(BorderStyle.THIN);
        numStyle.setBorderTop(BorderStyle.THIN);
        
        // 标题字体样式
        XSSFFont titleFont = wk.createFont();
        // 加粗
        titleFont.setBold(false);
        // 字体名称
        titleFont.setFontName("宋体");
        // 字体大小
        titleFont.setFontHeight(16);
        
        // 标题样式
        XSSFCellStyle titleStyle = wk.createCellStyle();
        titleStyle.setFont(titleFont);
        // 竖向居中
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 横向居中
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        // 边框
        titleStyle.setBorderBottom(BorderStyle.THIN);
        titleStyle.setBorderLeft(BorderStyle.THIN);
        titleStyle.setBorderRight(BorderStyle.THIN);
        titleStyle.setBorderTop(BorderStyle.THIN);
        
        // 合并单元格(第一行、标题)
        CellRangeAddress cAddress = new CellRangeAddress(0, 0, 0, 3);
        sheet.addMergedRegion(cAddress);
        
        // 合并单元格(第一个分类)
        CellRangeAddress cAddress2 = new CellRangeAddress(2, 3, 0, 0);
        sheet.addMergedRegion(cAddress2);

        // 创建第一行
        XSSFRow row1 = sheet.createRow(0);
        // 创建第一行第一列
        XSSFCell row1Cell1 = row1.createCell(0);
        row1Cell1.setCellValue("title");
        row1Cell1.setCellStyle(titleStyle);
        XSSFCell row1Cell2 = row1.createCell(1);
       // 为了保证合并的单元格能有效追加外框、被合并的单元格、内容要设置为空
        row1Cell2.setCellValue("");
        row1Cell2.setCellStyle(titleStyle);
        XSSFCell row1Cell3 = row1.createCell(2);
        row1Cell3.setCellValue("");
        row1Cell3.setCellStyle(titleStyle);
        XSSFCell row1Cell4 = row1.createCell(3);
        row1Cell4.setCellValue("");
        row1Cell4.setCellStyle(titleStyle);
        
        // 创建第二行
        XSSFRow row2 = sheet.createRow(1);
        // 创建第二行第一列
        XSSFCell row2Cell1 = row2.createCell(0);
        row2Cell1.setCellValue("分类");
        row2Cell1.setCellStyle(headStyle);
        // 列宽
        sheet.setColumnWidth(row2Cell1.getColumnIndex(), 60 * 50);
        // 创建第二行第二列
        XSSFCell row2Cell2 = row2.createCell(1);
        row2Cell2.setCellValue("内容");
        row2Cell2.setCellStyle(headStyle);
        // 列宽
        sheet.setColumnWidth(row2Cell2.getColumnIndex(), 356 * 50);
        // 创建第二行第三列
        XSSFCell row2Cell3 = row2.createCell(2);
        row2Cell3.setCellValue("标准");
        row2Cell3.setCellStyle(headStyle);
        // 列宽
        sheet.setColumnWidth(row2Cell3.getColumnIndex(), 70 * 50);
        // 创建第二行第四列
        XSSFCell row2Cell4 = row2.createCell(3);
        row2Cell4.setCellValue("备注");
        row2Cell4.setCellStyle(headStyle);
        // 列宽
        sheet.setColumnWidth(row2Cell4.getColumnIndex(), 70 * 50);
        
        // 创建第三行
        XSSFRow row3 = sheet.createRow(2);
        // 创建第三行第一列
        XSSFCell row3Cell1 = row3.createCell(0);
        row3Cell1.setCellValue("分类1");
        row3Cell1.setCellStyle(contentStyle);
        // 创建第三行第二列
        XSSFCell row3Cell2 = row3.createCell(1);
        row3Cell2.setCellValue("AAAAAAAAAAAAAAAAAAAAAA");
        row3Cell2.setCellStyle(contentStyle);
        // 创建第三行第三列
        XSSFCell row3Cell3 = row3.createCell(2);
        row3Cell3.setCellValue(10);
        row3Cell3.setCellStyle(numStyle);
        // 创建第三行第四列
        XSSFCell row3Cell4 = row3.createCell(3);
        row3Cell4.setCellValue(6);
        row3Cell4.setCellStyle(numStyle);
        
        // 创建第四行
        XSSFRow row4 = sheet.createRow(3);
        // 创建第四行第一列
        XSSFCell row4Cell1 = row4.createCell(0);
        row4Cell1.setCellValue("");
        row4Cell1.setCellStyle(contentStyle);
        // 创建第四行第二列
        XSSFCell row4Cell2 = row4.createCell(1);
        row4Cell2.setCellValue("BBBBBBBBBBBBBBBBBBBBBBBBBBBB");
        row4Cell2.setCellStyle(contentStyle);
      
        // 创建第四行第三列
        XSSFCell row4Cell3 = row4.createCell(2);
        row4Cell3.setCellValue(10);
        row4Cell3.setCellStyle(numStyle);
        // 创建第四行第四列
        XSSFCell row4Cell4 = row4.createCell(3);
        row4Cell4.setCellValue(6);
        row4Cell4.setCellStyle(numStyle);
        
        // 创建第五行
        XSSFRow row5 = sheet.createRow(4);
        // 创建第五行第一列
        XSSFCell row5Cell1 = row5.createCell(0);
        row5Cell1.setCellValue("分类2");
        row5Cell1.setCellStyle(contentStyle);
        // 创建第五行第二列
        XSSFCell row5Cell2 = row5.createCell(1);
        row5Cell2.setCellValue("CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC");
        row5Cell2.setCellStyle(contentStyle);
        // 创建第五行第三列
        XSSFCell row5Cell3 = row5.createCell(2);
        row5Cell3.setCellValue(10);
        row5Cell3.setCellStyle(numStyle);
        // 创建第五行第四列
        XSSFCell row5Cell4 = row5.createCell(3);
        row5Cell4.setCellValue(6);
        row5Cell4.setCellStyle(numStyle);
        
        FileOutputStream outputStream = new FileOutputStream(xlsPath);
        wk.write(outputStream);
        outputStream.flush();

生成的文件:

excel文件

猜你喜欢

转载自blog.csdn.net/qingdatiankong/article/details/81566787