POI导出Excel

HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet hssfSheet = workbook.createSheet("sheet1");
HSSFRow hssfRow = hssfSheet.createRow(0);
hssfRow.setHeight((short)500);

HSSFCellStyle hssfCellStyle = workbook.createCellStyle();
hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
hssfCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 10);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
hssfCellStyle.setFont(font);
hssfCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
hssfCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
hssfCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
hssfCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
final String[] titles = { "protocol", "", "host", "port","resource"};
HSSFCell hssfCell = null;
for (int i = 0; i < titles.length; i++) {
    hssfCell = hssfRow.createCell(i);
    hssfCell.setCellValue(titles[i]);
    hssfCell.setCellStyle(hssfCellStyle);
}
for (int i = 0; i < content.size(); i++){
    Server lic = content.get(i);
    hssfRow = hssfSheet.createRow(i+1);
    hssfRow.setHeight((short)300);
    HSSFCellStyle contentRowStyle = workbook.createCellStyle();
    contentRowStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    contentRowStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
    font = workbook.createFont();
    contentRowStyle.setFont(font);
    hssfRow.setRowStyle(contentRowStyle);

    hssfRow.createCell(0).setCellValue(lic.getProtocol());
    hssfRow.createCell(1).setCellValue(lic.getHost());
    hssfRow.createCell(2).setCellValue(lic.getPort());
    hssfRow.createCell(3).setCellValue(lic.getResource());
}

final int defaultWidth = 5000;
for (int x = 13; x >= 0; x--){
    hssfSheet.setColumnWidth(x, defaultWidth);
}
hssfSheet.setColumnWidth(8, 10000);

try {
    workbook.write(outputStream);
    outputStream.flush();
    outputStream.close();
} catch (IOException e) {
      log.error("导出失败");
    e.printStackTrace();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值