package com.xxx.statistic.file;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import com.hundsun.statistic.util.FileUtils;
@SuppressWarnings("deprecation")
public class ExcelWriter {
public static void write(String floder ,String fileName,String title,String[] fields,String[] fieldNames,List<Map<String,Object>> records) throws Exception{
if (records.size()>0){
FileOutputStream fileOut = null;
String filePath = "c:/statistic/excel/"+floder+"/"+fileName+"_"
+new SimpleDateFormat("yyMMdd_HHmmss").format(new Date()) +".xls";
try {
FileUtils.createFoldersByPath(filePath.substring(0,filePath.lastIndexOf("/")));
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(title);
//报表标题行 0行
HSSFRow rowHead = sheet.createRow(0);
rowHead.setHeightInPoints((short) 36);// 设置宽度
HSSFFont f = sheet.getWorkbook().createFont();
CellStyle style = sheet.getWorkbook().createCellStyle();
f.setFontHeightInPoints((short) 18);// 字号
f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
f.setFontName("华文楷体");
style.setFont(f);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, fields.length-1));// 合并第一行单元格
HSSFCell cellHead = rowHead.createCell(0);
cellHead.setCellValue(title);
cellHead.setCellStyle(style);
//表头 1行
HSSFRow rowTableHead = sheet.createRow(1);
for(int i=0;i<fields.length;i++){
sheet.setColumnWidth(i, 5000);
HSSFCell cell = rowTableHead.createCell(i);
cell.setCellValue(fieldNames[i]);
CellStyle styleTableHead = sheet.getWorkbook().createCellStyle();
styleTableHead.setFillForegroundColor(IndexedColors.AQUA.getIndex());
styleTableHead.setFillPattern(CellStyle.SOLID_FOREGROUND);
styleTableHead.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
styleTableHead.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
styleTableHead.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
styleTableHead.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
styleTableHead.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
styleTableHead.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
cell.setCellStyle(styleTableHead);
}
//表体 2行开始
for(int i=0;i<records.size();i++){
HSSFRow rowTableBody = sheet.createRow(i+2);
for(int j=0;j<fields.length;j++){
HSSFCell cell = rowTableBody.createCell(j);
cell.setCellValue(records.get(i).get("oracle".equals(floder)?fields[j].toUpperCase():fields[j]).toString());
}
}
fileOut = new FileOutputStream(filePath);
wb.write(fileOut);
} catch (Exception e) {
throw new Exception(e);
}finally{
if (fileOut != null)
try {fileOut.close();} catch (IOException e) {}
}
}
}
}
Excel-生成工具类
最新推荐文章于 2021-05-11 16:08:07 发布