关于POI文件导出 核心 初始化代码

/**
* 初始化函数
* @param title 表格标题,传“空值”,表示无标题
* @param headerList 表头列表
*/
private void initialize(String title, List headerList,List upList) {
this.wb = new SXSSFWorkbook(500);
this.sheet = wb.createSheet("Export");
this.styles = createStyles(wb);
// Create title
if (StringUtils.isNotBlank(title)){
Row titleRow = sheet.createRow(rownum++);  //rownum int类型默认是0   row 下表从1开始  这里获取标题 
titleRow.setHeightInPoints(30);
Cell titleCell = titleRow.createCell(0);//获取第一个单元
titleCell.setCellStyle(styles.get("title"));//设置样式(应该是合并单元格 styles。get(title))获取标题的样式
titleCell.setCellValue(title);//写入标题
sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(),
titleRow.getRowNum(), titleRow.getRowNum(), headerList.size()-1));

}
//int climnwidth=sheet.getColumnWidth(1)*headerList.size();
Row towRow = sheet.createRow(rownum++);  //第二行
Row tRow = sheet.createRow(rownum++);// 第三行 
Row fRow = null;
int rowNum=0;
int rwNum=0;
if(upList.size()==4){


//第二行第一个单元格
Cell cell1 = towRow.createCell(0);
cell1.setCellValue(upList.get(0));      
       //在sheet里增加合并单元格           
       sheet.addMergedRegion(new CellRangeAddress(1,1, 0, 1));
       
       Cell cellLast = towRow.createCell(headerList.size()-2);
cellLast.setCellValue(upList.get(1));
sheet.addMergedRegion(new CellRangeAddress(1,
       1, headerList.size()-2, headerList.size()-1));
//第三行第一个单元格
Cell cellt = tRow.createCell(0);
cellt.setCellValue(upList.get(2));
sheet.addMergedRegion(new CellRangeAddress(2,
       2, 0, 1));

Cell tCellLast = tRow.createCell(headerList.size()-2); 
tCellLast.setCellValue(upList.get(3));
sheet.addMergedRegion(new CellRangeAddress(2,
       2, headerList.size()-2, headerList.size()-1));
}else{
for(int i=0;i<=upList.size();i++){
if(i==1){
//获取到第二行的数据
Cell cell1 = towRow.createCell(0); //第一个单元格
cell1.setCellValue(upList.get(i-1));      
       //在sheet里增加合并单元格           
       sheet.addMergedRegion(new CellRangeAddress(1,1, 0, 1));
}
if(i==2){
Cell cellLast = towRow.createCell(headerList.size()-2); //获取到了第二行最后一列的单元格
//然后设置样式赋值
cellLast.setCellValue(upList.get(i-1));
sheet.addMergedRegion(new CellRangeAddress(1,
        1, headerList.size()-2, headerList.size()-1));
}
if(i>2){
if(rowNum Cell cell1 = tRow.createCell(rowNum);
cell1.setCellValue(upList.get(i-1));
rowNum=rowNum+2;
sheet.addMergedRegion(new CellRangeAddress(2,
        2, rowNum-2,rowNum-1));
// sheet.setColumnWidth(0, climnwidth/upList.size()-2);
}else{
if(rwNum==0){
fRow = sheet.createRow(rownum++);
}
Cell cell1 = fRow.createCell(rwNum); //第一个单元格
cell1.setCellValue(upList.get(i-1));
rwNum=rwNum+2;
sheet.addMergedRegion(new CellRangeAddress(3,
        3, rwNum-2,rwNum-1));
}
}
}
}


// Create header 
if (headerList == null){
throw new RuntimeException("headerList not null!");
}
//如果标题list 不为空 则循给第二行添加标题
Row headerRow = sheet.createRow(rownum++);//1++ = 2 第二行 
headerRow.setHeightInPoints(16);
for (int i = 0; i < headerList.size(); i++) {
Cell cell = headerRow.createCell(i);
cell.setCellStyle(styles.get("header"));
String[] ss = StringUtils.split(headerList.get(i), "**", 2);
if (ss.length==2){
cell.setCellValue(ss[0]);
Comment comment = this.sheet.createDrawingPatriarch().createCellComment(
new XSSFClientAnchor(0, 0, 0, 0, (short) 3, 3, (short) 5, 6));
comment.setString(new XSSFRichTextString(ss[1]));
cell.setCellComment(comment);
}else{
cell.setCellValue(headerList.get(i));
}
sheet.autoSizeColumn(i);
}
for (int i = 0; i < headerList.size(); i++) {  
int colWidth = sheet.getColumnWidth(i)*2;
       sheet.setColumnWidth(i, colWidth < 3000 ? 3000 : colWidth);  
}
log.debug("Initialize success.");
}

你可能感兴趣的:(自己写的笔记,导出)