将数据导出到excel与日期格式的设置
2009-09-01 11:54
537 查看
//将数据导出到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;
}
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;
}
相关文章推荐
- Java导出Excel设置大数据不被科学计数法显示、自定义日期格式
- GridView导出数据到Excel时格式设置
- 【导出EXCEL汇编】asp.net 将数据导出到excel中,并设置其格式
- 支持将数据导出到Excel文档的时候设置单元格格式的.NET控件Spire.DataExport
- 导出EXCEL汇编】asp.net 将数据导出到excel中,并设置其格式
- java 导出EXCEL 日期格式问题 设置日期格式和本机不一样
- 【导出EXCEL汇编】asp.net 将数据导出到excel中,并设置其格式
- 【导出EXCEL汇编】asp.net 将数据导出到excel中,并设置其格式
- 【导出EXCEL汇编】asp.net 将数据导出到excel中,并设置其格式
- excel 设置单元格格式为日期格式,数据未发生转换,双击才转换
- PHPExcel导入和导出的实现,导出数据过多失败,设置导出格式。
- C#将dataGridView数据导出成EXCEl、WORD格式
- Java导出Excel 单元格格式设置
- Asp.Net 导出数据格式为Excel 数据的9种方案
- phpexcle 导出数据 日期格式的问题
- POI设置EXCEL单元格格式为文本、小数、百分比、货币、日期、科学计数法和中文大写
- .net中将数据以excel格式导出
- 在Java中导出word、excel格式文件时JSP页面头的设置
- POI导出EXCEL带水印,以及单元格格式设置
- C# 导出excel并设置格式