1.poi
先加载到内存,再写入文件
xlsx使用的是XSSF
Workbook workbook = new XSSFWorkbook(inputStream);
xls使用的是HSSF
Workbook workbook = new HSSFWorkbook(inputStream);
读取一个sheet中的所有数据
Sheet sheet = book.getSheetAt(page);
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
System.out.println(cell.toString());
}
}
将数据写入
sheet.creatRow(第几行)
row.createCell(第几列)
生成表
FileOutputStream fileOutputStream = new FileOutputStream("...xlsx");
book.writer(fileOutputStream)
poi的excel工具类
public class ExcelUtils {
private static final String XLS = "xls";
private static final String XLSX = "xlsx";
private static final DateFormat FORMAT = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
/**
* 输出数据到自定义模版的Excel输出流
*
* @param excelTemplate 自定义模版文件
* @param data 数据
* @param outputStream Excel输出流
* @throws IOException 错误时抛出异常,由调用者处理
*/
public static void writeDataToTemplateOutputStream(File excelTemplate, List<List<Object>> data, OutputStream outputStream) throws IOException {
Workbook book = ExcelUtils.getWorkbookFromExcel(excelTemplate);
ExcelUtils.writeDataToWorkbook(null, data, book, 0);
ExcelUtils.writeWorkbookToOutputStream(book, outputStream);
}
/**
* 从Excel文件获取Workbook对象
*
* @param excelFile Excel文件
* @return Workbook对象
* @throws IOException 错误时抛出异常,由调用者处理
*/
public static Workbook getWorkbookFromExcel(File excelFile) throws IOException {
try (
InputStream inputStream = new FileInputStream(excelFile);
) {
if (excelFile.getName().endsWith(XLS)) {
return new HSSFWorkbook(inputStream);
} else if (excelFile.getName().endsWith(XLSX)) {
return new XSSFWorkbook(inputStream);
} else {
throw new IOException("文件类型错误");
}
}
}
/**
* 把Workbook对象内容输出到Excel文件
*
* @param book Workbook对象
* @param file Excel文件
* @throws FileNotFoundException 找不到文件异常,文件已创建,实际不存在该异常
* @throws IOException 输入输出异常
*/
public static void writeWorkbookToFile(Workbook book, File file) throws FileNotFoundException, IOException {
if (!file.exists()) {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
}
try (
OutputStream outputStream = new FileOutputStream(file);
) {
writeWorkbookToOutputStream(book, outputStream);
}
}
/**
* 把Workbook对象输出到Excel输出流
*
* @param book Workbook对象
* @param outputStream Excel输出流
* @throws IOException 错误时抛出异常,由调用者处理
*/
public static void writeWorkbookToOutputStream(Workbook book, OutputStream outputStream) throws IOException {
book.write(outputStream);
}
/**
* 输出数据到Workbook对象中指定页码
*
* @param title 标题,写在第一行,可传null
* @param data 数据
* @param book Workbook对象
* @param page 输出数据到Workbook指定页码的页面数
*/
public static void writeDataToWorkbook(List<String> title, List<List<Object>> data, Workbook book, int page) {
Sheet sheet = book.getSheetAt(page);
Row row = null;
Cell cell = null;
// 设置表头
if (null != title && !title.isEmpty()) {
row = sheet.getRow(0);
if (null == row) {
row = sheet.createRow(0);
}

本文对比了POI的Apache POI库和EasyExcel在处理Excel数据的读写操作,包括读取Excel首页、写入模板及批量导入导出的异同。同时介绍了EasyExcel的行级读写方式和在Spring MVC中的应用实例。
最低0.47元/天 解锁文章
1037

被折叠的 条评论
为什么被折叠?



