且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

excel导出效率慢,单元格样式复制方法分析

更新时间:2022-09-16 10:34:56

实现方法:

@SuppressWarnings("unchecked")

    public Row copyStyle(Sheet sheet, int srcRowNum, int destRowNum) {

     Row srcRow = sheet.getRow(srcRowNum);

     Row destRow = sheet.createRow(destRowNum);

     try{

        if(srcRow.getRowStyle()!=null){//row格式的复制

         destRow.setRowStyle(srcRow.getRowStyle());

        }

        int i=0;

        Iterator<Cell> srcCells = srcRow.cellIterator();

        while(srcCells.hasNext()) {

         Cell srcCell = srcCells.next();

         Cell destCell = destRow.createCell(i++);

         //cell格式的复制

            if(srcCell.getCellStyle()!=null)destCell.setCellStyle(srcCell.getCellStyle());


        }

     }catch(Exception e){

     LOG.error("copyStyle::"+e.getMessage());

     LOG.error(e);

     }

        return destRow;

}



调用方法:


Row row =null;

if(i==0){

row = sheet.createRow(i+6);

for (int j = 0; j < TITLES.length; j++) {

// 数据项单元格格式

Cell cell = row.createCell(j);

cell.setCellStyle(createCellStyleWithBorder(wookBook,CELL_STYPE_STRING, b));

}

}else{

row=copyStyle(sheet,i+5,i+6);

}



      本文转自tianjian_0913 51CTO博客,原文链接:http://blog.51cto.com/tianjian/1875844,如需转载请自行联系原作者