项目上这种功能很多,写了一个工具类,代码有点垃圾,大神勿喷
导入导出poi组件Excel
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
package com.tc.util;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Footer;
import org.apache.poi.ss.usermodel.Header;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class ExcelUtil {
Logger logger = LoggerFactory.getLogger(ExcelUtil.class);
public void exportMultiToDownFile(HttpServletResponse response, List<?> list, String filename, String[] title) {
Logger logger = LoggerFactory.getLogger(ExcelUtil.class);
filename += new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis());
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = genSheet(workbook, filename);
Map<String, XSSFCellStyle> tableStyle = createStyle(workbook);
Map<String, XSSFCellStyle> titleStyle = createStyle(workbook);
Map<String, XSSFCellStyle> contextStyle = createStyle(workbook);
genExcel(filename, list, title, sheet, tableStyle.get("cellStyle2"), titleStyle.get("cellStyle"), contextStyle.get("cellStyle3"));
String suffix = ".xls";
filename += suffix;
OutputStream out = null;
try {
out = response.getOutputStream();
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
workbook.write(out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
logger.info(e.getMessage());
} finally {
try {
if (out != null) {
out.close();
}
} catch (Exception e) {
e.printStackTrace();
logger.info(e.getMessage());
}
}
}
public void exportToDownFile(HttpServletResponse response, List<?> list, String filename, String[] title) {
filename += new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis());
String suffix = ".xls";
filename += suffix;
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(filename);
sheet.setDefaultColumnWidth(20);
Map<String, HSSFCellStyle> style = createStyle(workbook);
HSSFRow row = sheet.createRow(0);
row.setHeightInPoints(20);
HSSFCell cell = row.createCell(0);
cell.setCellValue(filename);
cell.setCellStyle(style.get("cellStyle"));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, (title.length - 1)));
HSSFRow rowTitle = sheet.createRow(1);
rowTitle.setHeightInPoints(20);
HSSFCell hc;
for (int i = 0; i < title.length; i++) {
hc = rowTitle.createCell(i);
hc.setCellValue(title[i]);
hc.setCellStyle(style.get("cellStyle2"));
}
Field[] fields;
int i = 2;
int index = 0;
for (Object obj : list) {
fields = obj.getClass().getDeclaredFields();
HSSFRow rowBody = sheet.createRow(i);
rowBody.setHeightInPoints(20);
int j = 0;
for (Field f : fields) {
f.setAccessible(true);
Object va = null;
try {
va = f.get(obj);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (null == va) {
va = "---";
}
hc = rowBody.createCell(j);
hc.setCellValue(va.toString());
hc.setCellStyle(style.get("cellStyle3"));
j++;
}
i++;
}
OutputStream out = null;
try {
out = response.getOutputStream();
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
workbook.write(out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
logger.info(e.getMessage());
} finally {
try {
if (out != null) {
out.close();
}
} catch (Exception e) {
e.printStackTrace();
logger.info(e.getMessage());
}
}
}
public static void genExcel(String filename, List<?> list, String[] title, XSSFSheet sheet, XSSFCellStyle tableStyle, XSSFCellStyle titleStyle, XSSFCellStyle contextStyle) {
sheet.setDefaultColumnWidth(20);
XSSFRow row = sheet.createRow(0);
row.setHeightInPoints(20);
XSSFCell cell = row.createCell(0);
cell.setCellValue(filename);
cell.setCellStyle(titleStyle);
sheet.addMergedRegion(new CellRangeAddress(
0,
0,
0,
(title.length - 1)
));
XSSFRow rowTitle = sheet.createRow(1);
rowTitle.setHeightInPoints(20);
XSSFCell xc;
for (int i = 0; i < title.length; i++) {
xc = rowTitle.createCell(i);
xc.setCellValue(title[i]);
xc.setCellStyle(tableStyle);
}
Field[] fields;
int i = 2;
for (Object obj : list) {
fields = obj.getClass().getDeclaredFields();
XSSFRow rowBody = sheet.createRow(i);
rowBody.setHeightInPoints(20);
int j = 0;
for (Field f : fields) {
f.setAccessible(true);
Object va = null;
try {
va = f.get(obj);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (null == va) {
va = "---";
}
xc = rowBody.createCell(j);
xc.setCellValue(va.toString());
xc.setCellStyle(contextStyle);
j++;
}
i++;
}
}
public static XSSFSheet genSheet(XSSFWorkbook workbook, String sheetName) {
XSSFSheet sheet = workbook.createSheet(sheetName);
sheet.setHorizontallyCenter(true);
sheet.setFitToPage(false);
Footer footer = sheet.getFooter();
footer.setRight("Page " + HeaderFooter.numPages() + " Of " + HeaderFooter.page());
Header header = sheet.getHeader();
header.setRight("Create Date " + HeaderFooter.date() + " " + HeaderFooter.time());
XSSFPrintSetup ps = sheet.getPrintSetup();
ps.setLandscape(true);
ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
return sheet;
}
public Map<String, HSSFCellStyle> createStyle(HSSFWorkbook workbook) {
Map<String, HSSFCellStyle> map = new HashMap<>();
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
HSSFFont fontStyle = workbook.createFont();
fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellStyle.setFont(fontStyle);
HSSFCellStyle cellStyle2 = workbook.createCellStyle();
cellStyle2.setFont(fontStyle);
cellStyle2.setBorderBottom(XSSFCellStyle.BORDER_THIN);
cellStyle2.setBorderLeft(XSSFCellStyle.BORDER_THIN);
cellStyle2.setBorderTop(XSSFCellStyle.BORDER_THIN);
cellStyle2.setBorderRight(XSSFCellStyle.BORDER_THIN);
HSSFCellStyle cellStyle3 = workbook.createCellStyle();
cellStyle3.setBorderBottom(XSSFCellStyle.BORDER_THIN);
cellStyle3.setBorderLeft(XSSFCellStyle.BORDER_THIN);
cellStyle3.setBorderTop(XSSFCellStyle.BORDER_THIN);
cellStyle3.setBorderRight(XSSFCellStyle.BORDER_THIN);
map.put("cellStyle", cellStyle);
map.put("cellStyle2", cellStyle2);
map.put("cellStyle3", cellStyle3);
return map;
}
public Map<String, XSSFCellStyle> createStyle(XSSFWorkbook workbook) {
Map<String, XSSFCellStyle> map = new HashMap<>();
XSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
XSSFFont fontStyle = workbook.createFont();
fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellStyle.setFont(fontStyle);
XSSFCellStyle cellStyle2 = workbook.createCellStyle();
cellStyle2.setFont(fontStyle);
cellStyle2.setBorderBottom(XSSFCellStyle.BORDER_THIN);
cellStyle2.setBorderLeft(XSSFCellStyle.BORDER_THIN);
cellStyle2.setBorderTop(XSSFCellStyle.BORDER_THIN);
cellStyle2.setBorderRight(XSSFCellStyle.BORDER_THIN);
XSSFCellStyle cellStyle3 = workbook.createCellStyle();
cellStyle3.setBorderBottom(XSSFCellStyle.BORDER_THIN);
cellStyle3.setBorderLeft(XSSFCellStyle.BORDER_THIN);
cellStyle3.setBorderTop(XSSFCellStyle.BORDER_THIN);
cellStyle3.setBorderRight(XSSFCellStyle.BORDER_THIN);
map.put("cellStyle", cellStyle);
map.put("cellStyle2", cellStyle2);
map.put("cellStyle3", cellStyle3);
return map;
}
}
|