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;

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值