public String export(DataTable dataTable ,List tableColumns, List values) throws Exception {
String simpleName = Calendar.getInstance().get(Calendar.SECOND)+".xls";
String fileName = SysParameter.EXPORT_TEMP + simpleName;
HSSFWorkbook workbook;//工作本
HSSFSheet sheet;// 当前表Sheet
HSSFRow row;//当前行Row
HSSFCell cell;//当前单元格Cell
HSSFCellStyle titleStyle , cellStyle;
HSSFFont font;
int rowIndex = 0;
int cellIndex = 0;
Iterator iterator,valueIterator;
TableColumn column;
DataTableSample data;
FileOutputStream outputStream = null;
try{
workbook = new HSSFWorkbook();
sheet = workbook.createSheet(dataTable.getTableCname());
valueIterator = values.iterator();
cellStyle = workbook.createCellStyle();
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
titleStyle = workbook.createCellStyle();
titleStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
titleStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
font = workbook.createFont();
font.setColor(HSSFColor.WHITE.index);
titleStyle.setFont(font);
row = sheet.createRow(rowIndex++);
cellIndex = 0;
iterator = tableColumns.iterator();
while(iterator.hasNext()){
column = (TableColumn)iterator.next();
cell = row.createCell(cellIndex++);
cell.setCellStyle(titleStyle);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(column.getColumnCname());
}
while(valueIterator.hasNext()){
row = sheet.createRow(rowIndex++);
cellIndex = 0;
iterator = tableColumns.iterator();
data = (DataTableSample)valueIterator.next();
while(iterator.hasNext()){
column = (TableColumn)iterator.next();
cell = row.createCell(cellIndex++);
cell.setCellStyle(cellStyle);
if(column.getColumnName().toLowerCase().startsWith("ex")){
if( data.getEx(Integer.parseInt(column.getColumnName().substring(2)))!=null){
cell.setCellValue( data.getEx( Integer.parseInt(column.getColumnName().substring(2))) );
}
}else if(column.getColumnName().toLowerCase().startsWith("ix")){
if(data.getIx(Integer.parseInt(column.getColumnName().substring(2)))!=null){
cell.setCellValue( data.getIx(Integer.parseInt(column.getColumnName().substring(2))));
}
}else if(column.getColumnName().toLowerCase().startsWith("dx")){
if(data.getDx(Integer.parseInt(column.getColumnName().substring(2)))!=null){
cell.setCellValue( data.getDx(Integer.parseInt(column.getColumnName().substring(2))));
}
}
}
}
<span style="color:#ff0000;">outputStream = new FileOutputStream(fileName);
workbook.write(outputStream);</span>
}finally{
if(outputStream!=null){
outputStream.close();
}
}
return simpleName;
}
项目所用技术回顾之excel导出(基于poi)
最新推荐文章于 2022-01-25 14:45:18 发布