一、引用包
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
二、工具类(创建excel)
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
public class PoiExcelUtils {
private static XSSFCellStyle getXSSFCellStyle(XSSFWorkbook workbook, boolean isSetCenter, boolean isSetBold, boolean isSetBorderStyle, int fontSize) {
XSSFCellStyle cellStyle = workbook.createCellStyle();
if (true == isSetCenter) {
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
}
XSSFFont font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) fontSize);
font.setBold(isSetBold);
cellStyle.setFont(font);
if (true == isSetBorderStyle) {
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
}
return cellStyle;
}
public static XSSFWorkbook createExcel() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("数据表");
sheet.setDefaultRowHeight((short) (3 * 256));
sheet.setDefaultColumnWidth(10);
XSSFRow row0 = sheet.createRow(0);
row0.setHeightInPoints(40);
row0.createCell(0).setCellValue("第一行");
XSSFRow row1 = sheet.createRow(1);
row1.setHeightInPoints(20);
row1.createCell(0).setCellValue("第二行");
XSSFRow row2 = sheet.createRow(2);
row2.setHeightInPoints(30);
row2.createCell(0).setCellValue("序号");
row2.createCell(1).setCellValue("名称");
for (int i = 0; i < 10; i++) {
XSSFRow row = sheet.createRow(i + 3);
row.setHeightInPoints(15);
row.createCell(0).setCellValue(i);
row.createCell(1).setCellValue("数据" + i);
}
CellRangeAddress cra = new CellRangeAddress(0, 0, 0, 1);
sheet.addMergedRegion(cra);
CellRangeAddress cra1 = new CellRangeAddress(1, 1, 0, 1);
sheet.addMergedRegion(cra1);
row0.getCell(0).setCellStyle(getXSSFCellStyle(workbook, true, false, false, 20));
row1.getCell(0).setCellStyle(getXSSFCellStyle(workbook, true, false, false, 12));
XSSFCellStyle xssfCellStyle = getXSSFCellStyle(workbook, true, false, true, 12);
for (int i = 0; i < 2; i++) {
sheet.setColumnWidth(i, 256 * 3);
}
return workbook;
}
}
三、下载文件或导出
import org.apache.poi.ss.usermodel.Workbook;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
public class ExcelFileUtil {
public void downExcel(Workbook workbook, String fileName, HttpServletResponse response) {
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
workbook.write(os);
byte[] bytes = os.toByteArray();
response.addHeader("Content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
if (bytes != null) {
OutputStream out = response.getOutputStream();
out.write(bytes, 0, bytes.length);
out.flush();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeExcel(Workbook workbook, String fileName) {
try {
File file = new File(fileName);
FileOutputStream flout = new FileOutputStream(file);
workbook.write(flout);
flout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}