将数据导出到excel与日期格式的设置

//将数据导出到excel

private HSSFWorkbook createExcel(List<Logs> list){
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Audit Logs");

//head style
HSSFCellStyle headingCellStyle = wb.createCellStyle();
headingCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

//head font
HSSFFont headingCellFont = wb.createFont();
headingCellFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headingCellStyle.setFont(headingCellFont);

// head
HSSFRow row;
row = sheet.createRow((short) 0);
String names[] = { "User Type", "User Name", "IP Address", "Action", "Details", "Date" };

//set list head
for (int i = 0; i < 6; i++) {
HSSFCell cell = row.createCell((short) (i));
cell.setCellValue(names[i]);
cell.setCellStyle(headingCellStyle);
}
//设置日期格式
HSSFCellStyle dateCellStyle = wb.createCellStyle();
HSSFDataFormat format = wb.createDataFormat();
short fmt = format.getFormat("yyyy/MM/dd HH:mm:ss");
dateCellStyle.setDataFormat(fmt);

short rowCount = 1;
for (int j = 0; j < list.size(); j++) {
row = sheet.createRow(rowCount);
Logs logs = list.get(j);
int column = 0;
HSSFCell cell = row.createCell((short) (column++));
cell.setCellValue(logs.getUsertype());

cell = row.createCell((short) (column++));
cell.setCellValue(logs.getUsername());

cell = row.createCell((short) (column++));
cell.setCellValue(this.getIp(logs));

cell = row.createCell((short) (column++));
cell.setCellValue(logs.getAction());

cell = row.createCell((short) (column++));
cell.setCellValue(getDetails(logs));

cell = row.createCell((short) (column));
cell.setCellStyle(dateCellStyle);
cell.setCellValue(DateUtil.format(logs.getDate(), "yyyy/MM/dd HH:mm:ss"));
rowCount++;
}
FileOutputStream fos;
StringBuffer filePath = new StringBuffer().append(Glob.tempdir());
if(filePath.charAt(filePath.length()-1) != File.separatorChar)
filePath.append(File.separator);
filePath.append("export");

File file = new File(filePath.toString());
if (!file.exists()) {
file.mkdirs();
}
filePath.append(File.separator);
filePath.append(System.currentTimeMillis() + "_auditLogs.xls");
try {
fos = new FileOutputStream(filePath.toString());
wb.write(fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
return wb;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值