Java导出数据到Excel分3步处理
1、构建Workbook 数据
2、设置Workbook 格式
3、导出到Excel
public static void buildData(Workbook wb, List<Person> list) {
Sheet sheetName = wb.createSheet("sheetName");
Row row = sheetName.createRow(0);
row.createCell(0).setCellValue("Id");
row.createCell(1).setCellValue("Name");
row.createCell(2).setCellValue("Age");
row.createCell(3).setCellValue("NickName");
for (int i = 0; i < list.size(); i++) {
Person person = list.get(i);
row = sheetName.createRow(i + 1);
row.createCell(0).setCellValue(person.getId());
row.createCell(1).setCellValue(person.getName());
row.createCell(2).setCellValue(person.getAge());
row.createCell(3).setCellValue(person.getNickName());
}
wb.setSheetName(0, "sheet");
}
public static void buildStyle(Workbook wb) {
Sheet sheet;
Row row;
for (int s = 0; s < wb.getNumberOfSheets(); s++) {
sheet = wb.getSheetAt(s);
for (int r = sheet.getFirstRowNum(); r <= sheet.getLastRowNum(); r++) {
row = sheet.getRow(r);
for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
if (r == 0) { // 表头
row.getCell(c).setCellStyle(ExcelStyleUtils.headerStyle(wb));
sheet.autoSizeColumn(c);
} else { // 数据
row.getCell(c).setCellStyle(ExcelStyleUtils.cellsStyle(wb));
if (sheet.getColumnWidth(c) > 50 * 256) {
sheet.setColumnWidth(c, 50 * 256);
}
}
}
}
}
}
public static void writeExcel(Workbook workbook) throws Exception {
ExcelUtils.deleteOldExcelFile(OUT_PATH);
ExcelUtils.makeExcelFile(OUT_PATH);
FileOutputStream fileOutputStream = new FileOutputStream(genExcelName(OUT_PATH), false);
workbook.write(fileOutputStream);
fileOutputStream.close();
}
public static void writeExcel2(Workbook workbook) throws Exception {
ExcelUtils.deleteOldExcelFile(OUT_PATH);
ExcelUtils.makeExcelFile(OUT_PATH);
OutputStream outputStream = new FileOutputStream(genExcelName(OUT_PATH));
workbook.write(outputStream);
outputStream.close();
}
private static String genExcelName(String path) {
String postName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHMMSS"));
return path + "/输出" + postName + ".xlsx";
}
@Test
public void testWriteExcel2() throws Exception {
List<Person> persons = new ArrayList<>();
persons.add(new Person(10, "John", 11, "nickName"));
persons.add(new Person(11, "John1", 11, "nickName1"));
persons.add(new Person(12, "John2", 11, "nickName2"));
persons.add(new Person(13, "John3", 11, "nickName3"));
persons.add(new Person(14, "John4", 11, "nickName4"));
XSSFWorkbook wb = new XSSFWorkbook();
buildData(wb, persons); // 构建数据
buildStyle(wb); // 设置格式
writeExcel(wb); 或 writeExcel2(wb); // 导出数据
}
import model.Person;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poiartifactId>
<version>5.2.3version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-ooxmlartifactId>
<version>5.2.3version>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.11.0version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-apiartifactId>
<version>2.15.0version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.15.0version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-slf4j-implartifactId>
<version>2.15.0version>
dependency>