poi到excel

public InputStream getUserLoginCount(Map map) {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("信息发布网站用户访问统计报表");
Map cellWidths = new HashMap();
createHand(wb, sheet, "《信息发布网站用户访问统计报表》");
List<UserLoginCount> reVal = this.queryUserByLoginCunt(map);
short row = 3;
for (UserLoginCount userLoginCount : reVal) {
createRow(sheet, row, new short[] { 0, 1, 2, 3},
new String[] { userLoginCount.getUserName(), userLoginCount.getDeptName(), userLoginCount.getIpAddr(), userLoginCount.getCount()+" "},
new HSSFCellStyle[] { createCellHeandStyle(wb,
HSSFCellStyle.ALIGN_LEFT, HSSFFont.DEFAULT_CHARSET,
(short) 230) });
row++;
}

ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
wb.write(os);

return new ByteArrayInputStream(os.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
log.error("导出报表出错", e);
throw new RuntimeException(e);
}
}



private void createHand(HSSFWorkbook wb, HSSFSheet sheet, String title) {
Map cellWidths = new HashMap();
cellWidths.put((short) 0, (short) 5000);
cellWidths.put((short) 1, (short) 8000);
cellWidths.put((short) 2, (short) 5000);
cellWidths.put((short) 3, (short) 3000);
setCellWidth(sheet, cellWidths);
createRow(sheet, (short) 0, new short[] { 0 }, new String[] { title },
new HSSFCellStyle[] { createCellHeandStyle(wb,
HSSFCellStyle.ALIGN_CENTER, HSSFFont.BOLDWEIGHT_BOLD,
(short) 230) });

mergedRegion(sheet, 1, 1, 1, 4);

createRow(sheet, (short) 2, new short[] { 0, 1, 2, 3},
new String[] { "用户名", "部门", "ip地址", "登录次数"},
new HSSFCellStyle[] { createCellHeandStyle(wb,
HSSFCellStyle.ALIGN_LEFT, HSSFFont.BOLDWEIGHT_BOLD,
(short) 230) });
}

private void setCellWidth(HSSFSheet sheet, Map map) {
Set set = map.entrySet();
Iterator it = set.iterator();
while (it.hasNext()) {
Map.Entry entry = (Entry) it.next();
sheet.setColumnWidth((Short) entry.getKey(), (Short) entry
.getValue());
}
}

private HSSFRow createRow(HSSFSheet sheet, short rowNum, short[] cellNum,
String[] cellValue, HSSFCellStyle[] style) {
HSSFRow row = sheet.createRow(rowNum);
for (int i = 0; i < cellNum.length; i++) {
HSSFCell cell = row.createCell(cellNum[i]);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(new HSSFRichTextString(cellValue[i]));
if (style.length == cellNum.length) {
cell.setCellStyle(style[i]);
} else {
cell.setCellStyle(style[0]);
}

}
return row;
}

private HSSFCellStyle createCellHeandStyle(HSSFWorkbook wb, short align,
short font, short fontSize) {
HSSFFont cellFont = wb.createFont();
cellFont.setFontName("宋体");
cellFont.setBoldweight(font);
cellFont.setFontHeight(fontSize);

HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(align);
cellStyle.setFont(cellFont);
cellStyle.setWrapText(true);
return cellStyle;
}

private void mergedRegion(HSSFSheet sheet, int row, int cel, int toRow,
int toCel) {
sheet.addMergedRegion(new Region((short) (row - 1), (short) (cel - 1),
(short) (toRow - 1), (short) (toCel - 1)));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值