package com.cyao.util.excel;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* poi处理Excel
*
* @author ChangYao
*/
public class PoiExcel {
private static final int maxRowCount = 65000;// 不能够超过Excel的最大容量
private PoiExcel() {
}
/**
* 导出数据到Excel 一个Excel页的最大行数为65536
*
* @param datas 目标数据
* @param titles 标题数组
* @param function Function实现类
* @param pathname 文件路径
* @param <T> 目标类型
*/
public static <T> void doExport(List<T> datas, String[] titles, Function<T, String[]> function, String pathname) {
try {
doExport(datas, titles, function, new FileOutputStream(pathname));
} catch (Exception ignored) {
}
}
/**
* 导出数据到Excel 一个Excel页的最大行数为65536
*
* @param datas 目标数据
* @param titles 标题数组
* @param function Function实现类
* @param os 输出流
* @param <T> 目标类型
*/
public static <T> void doExport(List<T> datas, String[] titles, Function<T, String[]> function, OutputStream os) {
Workbook workBook = new HSSFWorkbook();
int pageCount = (datas.size() + maxRowCount - 1) / maxRowCount;
for (int i = 0; i < pageCount; i++) {
workBook.createSheet("Sheet" + i);
}
for (int page = 0; page < pageCount; page++) {
Sheet sheet = workBook.getSheetAt(page);
Row titleRow = sheet.createRow(0);
for (int i = 0; i < titles.length; i++) {
titleRow.createCell(i).setCellValue(titles[i]);
}
int end = page == pageCount - 1 ? datas.size() : (page + 1) * maxRowCount;
for (int i = page * maxRowCount; i < end; i++) {
Row dataRow = sheet.createRow(i + 1 - page * maxRowCount);
T t = datas.get(i);
String[] values = function.apply(t);
for (int j = 0; j < values.length; j++) {
//dataRow.createCell(j).setCellValue(values[j]);
String value = values[j];
if (titles[j].contains("金额") && value.matches("\\d+(.\\d+)?")) {
dataRow.createCell(j).setCellValue(Double.parseDouble(value));
} else {
dataRow.createCell(j).setCellValue(value);
}
}
}
}
try {
workBook.write(os);
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 导入Excel数据到文件
*
* @param pathname 文件路径
* @param rowConsumer
*/
public static void doImport(String pathname, Consumer<FileRow> rowConsumer) {
try {
doImport(new FileInputStream(pathname), rowConsumer);
} catch (IOException ignored) {
}
}
/**
* 导入Excel数据
*
* @param inputStream 输入流
* @param rowConsumer
*/
public static void doImport(InputStream inputStream, Consumer<FileRow> rowConsumer) {
Workbook wb = null;
try {
wb = WorkbookFactory.create(inputStream);
} catch (Exception e) {
e.printStackTrace();
}
assert wb != null;
Sheet sheet = wb.getSheetAt(0);
int totalRows = sheet.getPhysicalNumberOfRows();
DataFormatter formatter = new DataFormatter();//正确获取String的方式
for (int i = 1; i < totalRows; i++) {
Row row = sheet.getRow(i);
if (row == null) {
continue;
}
FileRow fr = new FileRow(i);
int totalCells = row.getPhysicalNumberOfCells();
for (int j = 0; j < totalCells; j++) {
Cell cell = row.getCell(j);
fr.addCol(j, formatter.formatCellValue(cell));
}
rowConsumer.accept(fr);
}
}
public static class FileRow {
private List<String> data = new ArrayList<>(26);
private int row;
public FileRow(int row) {
this.row = row;
}
public int getRow() {
return row;
}
public void addCol(int colIndex, String value) {
this.data.add(colIndex, value);
}
public String getCol(int colIndex) {
return this.data.get(colIndex);
}
}
}
List<Map<String, String>> datas = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Map<String, String> map = new HashMap<>();
map.put("id", new DecimalFormat("H0000").format(i + 1L));
map.put("name", new DecimalFormat("Name0000").format(i + 1L));
map.put("age", String.valueOf(RandomUtils.nextInt(18, 33)));
datas.add(map);
}
String[] titles = new String[]{"编号", "姓名", "年龄"};
String pathname = "d://test.xls";
PoiExcel.doExport(datas, titles, t -> new String[]{t.get("id"), t.get("name"), t.get("age")}, pathname);