POI导出工具类
2018-12-12 本文已影响30人
周六不算加班
1、导入相关的类
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
2、工具类
public class ExcelUtils {
static Logger logger = LogManager.getLogger(ExcelUtils.class);
// excel默认宽度;
private static int width = 256 * 14;
// 默认字体
private static String excelfont = "微软雅黑";
/**
*
* @param excelName
* 导出的EXCEL名字
* @param sheetName
* 导出的SHEET名字 当前sheet数目只为1
* @param headers
* 导出的表格的表头
* @param ds_titles
* 导出的数据 map.get(key) 对应的 key
* @param ds_format
* 导出数据的样式 1:String left; 2:String center 3:String right 4 int right
* 5:float ###,###.## right 6:number: #.00% 百分比 right
* @param widths
* 表格的列宽度 默认为 256*14
* @param data
* 数据集 List<Map>
* @param response
* @throws IOException
*/
public static void export(String excelName, String sheetName, List<String> headers, List<String> ds_titles,
int[] ds_format, int[] widths, List<Map<String, Object>> data, HttpServletRequest request,
HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
// session.setAttribute("state", null);
logger.info("export->");
if (widths == null) {
widths = new int[ds_titles.size()];
for (int i = 0; i < ds_titles.size(); i++) {
widths[i] = width;
}
}
if (ds_format == null) {
ds_format = new int[ds_titles.size()];
for (int i = 0; i < ds_titles.size(); i++) {
ds_format[i] = 1;
}
}
// 设置文件名
String fileName = "";
if (StringUtils.isNotEmpty(excelName)) {
fileName = excelName;
}
// 创建一个工作薄
@SuppressWarnings("resource")
HSSFWorkbook wb = new HSSFWorkbook();
// 创建一个sheet
HSSFSheet sheet = wb.createSheet(StringUtils.isNotEmpty(sheetName) ? sheetName : "excel");
// 创建表头,如果没有跳过
int headerrow = 0;
if (headers != null) {
HSSFRow row = sheet.createRow(headerrow);
// 表头样式
HSSFCellStyle style = wb.createCellStyle();
HSSFFont font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontName(excelfont);
font.setFontHeightInPoints((short) 11);
style.setFont(font);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
for (int i = 0; i < headers.size(); i++) {
sheet.setColumnWidth((short) i, (short) widths[i]);
HSSFCell cell = row.createCell(i);
cell.setCellValue(headers.get(i));
cell.setCellStyle(style);
}
headerrow++;
}
// 表格主体 解析list
if (data != null) {
List<HSSFCellStyle> styleList = new ArrayList<>();
for (int i = 0; i < ds_titles.size(); i++) { // 列数
HSSFCellStyle style = wb.createCellStyle();
HSSFFont font = wb.createFont();
font.setFontName(excelfont);
font.setFontHeightInPoints((short) 10);
style.setFont(font);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
if (ds_format[i] == 1) {
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
} else if (ds_format[i] == 2) {
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
} else if (ds_format[i] == 3) {
style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
// int类型
} else if (ds_format[i] == 4) {
style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
// int类型
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("0"));
} else if (ds_format[i] == 5) {
// float类型
style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.00"));
} else if (ds_format[i] == 6) {
// 百分比类型
style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%"));
}
styleList.add(style);
}
for (int i = 0; i < data.size(); i++) { // 行数
HSSFRow row = sheet.createRow(headerrow);
Map<String, Object> map = data.get(i);
for (int j = 0; j < ds_titles.size(); j++) { // 列数
HSSFCell cell = row.createCell(j);
Object o = map.get(ds_titles.get(j));
//增加对对象的处理
if(o!=null&&!(o instanceof String)&&!(o instanceof Integer)&&!(o instanceof byte[])){
JSONObject obj= JSONObject.parseObject(o.toString());
if(obj.size()>1){
o=obj.get("value");
}
}
if (o == null || "".equals(o)) {
cell.setCellValue("");
} else if (ds_format[j] == 4) {
// int
cell.setCellValue((Long.valueOf(o + "")).longValue());
} else if (ds_format[j] == 5 || ds_format[j] == 6) {
// float
cell.setCellValue((Double.valueOf(o + "")).doubleValue());
} else {
cell.setCellValue(o + "");
}
cell.setCellStyle((HSSFCellStyle) styleList.get(j));
}
headerrow++;
}
}
fileName = fileName + ".xls";
response.setHeader("Content-disposition", fileName);
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes(),"ISO-8859-1"));
response.setHeader("Pragma", "No-cache");
response.setCharacterEncoding("UTF-8");
OutputStream ouputStream = response.getOutputStream();
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();
}
}