Пример #1
0
 @Override
 public Workbook writeWorkbook(
     Workbook workbook, String sheetTitle, List<List<String>> tableDataList, int headerNum) {
   if (workbook == null) workbook = new XSSFWorkbook();
   int exportRecordNum = tableDataList.size(); // 导出的总记录数
   int tableStartRowNum = headerNum > 0 ? headerNum : 0; // 表体数据开始行数
   int sheetNum =
       exportRecordNum / (CommonConst.EXCEL_MAX_EXPORT_NUM - headerNum)
           + 1; // 工作表的页数
   List<XSSFSheet> sheetList = new ArrayList<XSSFSheet>();
   if (StringUtils.isEmpty(sheetTitle)) sheetTitle = "sheet";
   for (int i = 0; i < sheetNum; i++) {
     // 在Excel工作簿中建一工作表
     String sTitle = sheetTitle + (i + 1);
     XSSFSheet sheet = (XSSFSheet) workbook.createSheet(sTitle);
     sheet.setSelected(true); // 设置工作薄为选中
     sheet.setAutobreaks(true);
     sheet.setPrintGridlines(true);
     sheetList.add(sheet);
   }
   /** ***********************输出表头********************************* */
   if (headerNum > 0) {
     for (XSSFSheet sheet : sheetList) {
       XSSFRow headRow = sheet.createRow(0);
       headRow.setHeightInPoints(20);
       for (int i = 0; i < headerNum; i++) {
         List<String> headerRowDataList = tableDataList.get(i);
         for (int j = 0; j < headerRowDataList.size(); j++) {
           XSSFCellStyle cellStyle = (XSSFCellStyle) createDefHeaderCellStyle(workbook); // 默认表头样式
           createCell(headRow, j, headerRowDataList.get(j), cellStyle);
         }
       }
       // 固定表头
       sheet.createFreezePane(0, 1);
     }
   }
   /** *********************输出表体内容************************* */
   // 设置列样式
   XSSFCellStyle columnStyle = (XSSFCellStyle) workbook.createCellStyle();
   columnStyle.setFillBackgroundColor(HSSFColor.GREEN.index);
   columnStyle.setWrapText(true);
   if (tableDataList.size() > (tableStartRowNum + 1)) {
     for (int i = tableStartRowNum; i < exportRecordNum; i++) {
       List<String> rowDataList = tableDataList.get(i);
       XSSFRow row = null;
       int currentSheet = i / CommonConst.EXCEL_MAX_EXPORT_NUM; // 当前工作表的页数
       XSSFSheet sheet = sheetList.get(currentSheet);
       int rowIndex = i - CommonConst.EXCEL_MAX_EXPORT_NUM * currentSheet;
       row = sheet.createRow(rowIndex);
       for (int colIndex = 0, colLength = rowDataList.size(); colIndex < colLength; colIndex++) {
         createCell(row, colIndex, rowDataList.get(colIndex));
       }
     }
   }
   // 调整列的宽度(取第一列为基准)
   for (XSSFSheet sheet : sheetList) {
     for (int i = 0; i < tableDataList.get(0).size(); i++) {
       sheet.autoSizeColumn((short) i);
       sheet.setColumnWidth((short) i, (short) (sheet.getColumnWidth((short) i) + 1000));
     }
   }
   return workbook;
 }
Пример #2
0
 /**
  * Sets a flag indicating whether this sheet is selected.
  *
  * <p>Note: multiple sheets can be selected, but only one sheet can be active at one time.
  *
  * @param value <code>true</code> if this sheet is selected
  * @see Workbook#setActiveSheet(int)
  */
 public void setSelected(boolean value) {
   _sh.setSelected(value);
 }