java excel 多个sheet_java poi 通过excel模板导出并且生成多个sheet

@SuppressWarnings("deprecation")

private static void copyRows(HSSFWorkbook workbook, HSSFSheet

fromsheet, HSSFSheet newsheet, int firstrow, int lastrow)

{

if ((firstrow == -1) || (lastrow == -1) || lastrow <

firstrow)

{

return;

}

// 拷贝合并的单元格

Region region = null;

for (int i = 0; i < fromsheet.getNumMergedRegions();

i++)

{

region = fromsheet.getMergedRegionAt(i);

if ((region.getRowFrom() >= firstrow) &&

(region.getRowTo() <= lastrow))

{

newsheet.addMergedRegion(region);

}

}

HSSFRow fromRow = null;

HSSFRow newRow = null;

HSSFCell newCell = null;

HSSFCell fromCell = null;

// 设置列宽

for (int i = firstrow; i <= lastrow; i++)

{

fromRow = fromsheet.getRow(i);

if (fromRow != null)

{

for (int j = fromRow.getLastCellNum(); j >=

fromRow.getFirstCellNum(); j--)

{

int colnum = fromsheet.getColumnWidth((short) j);

if (colnum > 100)

{

newsheet.setColumnWidth((short) j, (short) colnum);

}

if (colnum == 0)

{

newsheet.setColumnHidden((short) j, true);

}

else

{

newsheet.setColumnHidden((short) j, false);

}

}

break;

}

}

// 拷贝行并填充数据

for (int i = 0; i <= lastrow; i++)

{

fromRow = fromsheet.getRow(i);

if (fromRow == null)

{

continue;

}

newRow = newsheet.createRow(i - firstrow);

newRow.setHeight(fromRow.getHeight());

for (int j = fromRow.getFirstCellNum(); j <

fromRow.getPhysicalNumberOfCells(); j++)

{

fromCell = fromRow.getCell((short) j);

if (fromCell == null)

{

continue;

}

newCell = newRow.createCell((short) j);

newCell.setCellStyle(fromCell.getCellStyle());

int cType = fromCell.getCellType();

newCell.setCellType(cType);

switch (cType)

{

case HSSFCell.CELL_TYPE_STRING:

newCell.setCellValue(fromCell.getRichStringCellValue());

break;

case HSSFCell.CELL_TYPE_NUMERIC:

newCell.setCellValue(fromCell.getNumericCellValue());

break;

case HSSFCell.CELL_TYPE_FORMULA:

newCell.setCellFormula(fromCell.getCellFormula());

break;

case HSSFCell.CELL_TYPE_BOOLEAN:

newCell.setCellValue(fromCell.getBooleanCellValue());

break;

case HSSFCell.CELL_TYPE_ERROR:

newCell.setCellValue(fromCell.getErrorCellValue());

break;

default:

newCell.setCellValue(fromCell.getRichStringCellValue());

break;

}

}

}

}

你可能感兴趣的:(java,excel,多个sheet)