java根据poi创建excel且实现列宽自适应及样式自定义功能

代码是伪代码,具体用什么数据类型来封装excel的数据可以根据实际情况选择。代码如下:

public class ExcelBuilder {
    /**
     根据参数,生成指定的HSSFWorkbook对象
     @param wbData excel数据,格式为:sheet集合<row集合<cell集合<单元格的值,String类型>>>
     @param sheetNames 每个sheet的名字
     @param titles 每列的表头,格式为:sheet的list:标题行字段名list
     @return
     */
    public static HSSFWorkbook createExcel(List<List<List<String>>> wbData, List<String> sheetNames, List<List<String>> titles){
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建样式对象
        HSSFCellStyle cellStyle = setStyleXlsx(wb);
        for (int i = 0; i < wbData.size(); i++) {
            //获取当前sheet数据
            List<List<String>> sheetData = wbData.get(i);
            //String sheetName = 根据参数获取当前需要创造的sheet名;
            String sheetName = sheetNames.get(i);
            //根据客户姓名创建sheet
            HSSFSheet sheet = wb.createSheet(sheetName);
            HSSFCell cell;
            HSSFRow firstRow = sheet.createRow(0);
            
            //创建标题行
            List<String> sheetTitles = titles.get(i);
            for (int j = 0; j < sheetTitles.size(); j++) {
                cell = firstRow.createCell(j);
                cell.setCellStyle(cellStyle);
                cell.setCellValue(sheetTitles.get(j));
            }
            
            //循环一次创建一行数据
            for (int k = 0; k < sheetData.size(); k++) {
                List<String> rowData = sheetData.get(k);
                HSSFRow row = sheet.createRow(k + 1);
                //循环一次创建一个单元格数据
                for (int a = 0; a < rowData.size(); a++){
                    cell = row.createCell(a);
                    //设置每个单元格的样式
                    cell.setCellStyle(cellStyle);
                    //写入每个单元格的数据
                    cell.setCellValue(rowData.get(a));
                }
            }
            //当前sheet包含的列数
            int columnNum = sheetTitles.size();
            //设置当前sheet的列宽自适应
            setSizeColum(sheet, columnNum);
        }
        return wb;
    }
    /**
     * 设置样式
     * @param book
     * @return
     */
    public  static HSSFCellStyle setStyleXlsx(HSSFWorkbook book){
        HSSFCellStyle cellStyle = book.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);// 居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直
        cellStyle.setBorderBottom(BorderStyle.THIN);//下边框
        cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
        cellStyle.setBorderTop(BorderStyle.THIN);//上边框
        cellStyle.setBorderRight(BorderStyle.THIN);//右边框
        return cellStyle;
    }
    /**
     * 设置列宽自适应
     * @param sheet 需要设置宽度自适应的sheet
     * @param size 列数
     */
    public static void setSizeColum(HSSFSheet sheet, int size){
        for(int columnNum = 0; columnNum < size; columnNum++){
            int columnWidth = sheet.getColumnWidth(columnNum) / 256;
            for(int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++){
                HSSFRow currentRow;
                //当前行未被使用过
                if(sheet.getRow(rowNum) == null){
                    currentRow = sheet.createRow(rowNum);
                }else {
                    currentRow = sheet.getRow(rowNum);
                }
                if (currentRow.getCell(columnNum) != null){
                    HSSFCell currentCell = currentRow.getCell(columnNum);
                    //判断该列的数据类型是否为String类型
                    if (currentCell.getCellTypeEnum() == CellType.STRING){
                        //这里值转换String类型的数据的列,不然currentCell.getStringCellValue()会报错
                        int length = currentCell.getStringCellValue().getBytes(StandardCharsets.UTF_8).length;
                        if (columnWidth < length){
                            columnWidth = length;
                        }
                    }else{
                        //如果有的列的数据类型不是String类型需在此处补充;如果是日期类型或者其他就得这样先转成String再设置
                        //此处示例日期类型
                        //                        int length = currentCell.getDateCellValue().toString().getBytes(StandardCharsets.UTF_8).length;
                        //                        if (columnWidth < length){
                        //                            columnWidth = length;
                        //                        }
                    }
                }
            }
            sheet.setColumnWidth(columnNum, columnWidth * 256);
        }
    }
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值