增加Excel导入导出测试类,增加模板导出。
This commit is contained in:
@@ -59,7 +59,7 @@ public class ExcelExport implements Closeable{
|
|||||||
/**
|
/**
|
||||||
* 当前行号
|
* 当前行号
|
||||||
*/
|
*/
|
||||||
private int rownum;
|
private int rowNum;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注解列表(Object[]{ ExcelField, Field/Method })
|
* 注解列表(Object[]{ ExcelField, Field/Method })
|
||||||
@@ -158,6 +158,16 @@ public class ExcelExport implements Closeable{
|
|||||||
}
|
}
|
||||||
this.createSheet(sheetName, title, headerList, headerWidthList);
|
this.createSheet(sheetName, title, headerList, headerWidthList);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造函数
|
||||||
|
*/
|
||||||
|
public ExcelExport(ExcelImport excelImport) {
|
||||||
|
this.wb = excelImport.getWorkbook();
|
||||||
|
this.styles = createStyles(wb);
|
||||||
|
this.sheet = excelImport.getSheet();
|
||||||
|
this.rowNum = excelImport.getHeaderNum();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建一个工作簿
|
* 创建一个工作簿
|
||||||
@@ -286,12 +296,12 @@ public class ExcelExport implements Closeable{
|
|||||||
* @param headerWidthList 表头字段宽度设置
|
* @param headerWidthList 表头字段宽度设置
|
||||||
*/
|
*/
|
||||||
public void createSheet(String sheetName, String title, List<String> headerList, List<Integer> headerWidthList) {
|
public void createSheet(String sheetName, String title, List<String> headerList, List<Integer> headerWidthList) {
|
||||||
this.sheet = wb.createSheet(StringUtils.defaultString(sheetName, StringUtils.defaultString(title, "Sheet1")));
|
this.sheet = wb.createSheet(StringUtils.defaultIfBlank(sheetName, StringUtils.defaultIfBlank(title, "Sheet1")));
|
||||||
this.styles = createStyles(wb);
|
this.styles = createStyles(wb);
|
||||||
this.rownum = 0;
|
this.rowNum = 0;
|
||||||
// Create title
|
// Create title
|
||||||
if (StringUtils.isNotBlank(title)){
|
if (StringUtils.isNotBlank(title)){
|
||||||
Row titleRow = sheet.createRow(rownum++);
|
Row titleRow = sheet.createRow(rowNum++);
|
||||||
titleRow.setHeightInPoints(30);
|
titleRow.setHeightInPoints(30);
|
||||||
Cell titleCell = titleRow.createCell(0);
|
Cell titleCell = titleRow.createCell(0);
|
||||||
titleCell.setCellStyle(styles.get("title"));
|
titleCell.setCellStyle(styles.get("title"));
|
||||||
@@ -303,7 +313,7 @@ public class ExcelExport implements Closeable{
|
|||||||
if (headerList == null){
|
if (headerList == null){
|
||||||
throw new ExcelException("headerList not null!");
|
throw new ExcelException("headerList not null!");
|
||||||
}
|
}
|
||||||
Row headerRow = sheet.createRow(rownum++);
|
Row headerRow = sheet.createRow(rowNum++);
|
||||||
headerRow.setHeightInPoints(16);
|
headerRow.setHeightInPoints(16);
|
||||||
for (int i = 0; i < headerList.size(); i++) {
|
for (int i = 0; i < headerList.size(); i++) {
|
||||||
Cell cell = headerRow.createCell(i);
|
Cell cell = headerRow.createCell(i);
|
||||||
@@ -412,7 +422,7 @@ public class ExcelExport implements Closeable{
|
|||||||
* @return 行对象
|
* @return 行对象
|
||||||
*/
|
*/
|
||||||
public Row addRow(){
|
public Row addRow(){
|
||||||
return sheet.createRow(rownum++);
|
return sheet.createRow(rowNum++);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -633,54 +643,5 @@ public class ExcelExport implements Closeable{
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**
|
|
||||||
// * 导出测试
|
|
||||||
// */
|
|
||||||
// public static void main(String[] args) throws Throwable {
|
|
||||||
//
|
|
||||||
// // 初始化表头
|
|
||||||
// List<String> headerList = ListUtils.newArrayList();
|
|
||||||
// for (int i = 1; i <= 10; i++) {
|
|
||||||
// headerList.add("表头"+i);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // 初始化数据集
|
|
||||||
// List<String> rowList = ListUtils.newArrayList();
|
|
||||||
// for (int i = 1; i <= headerList.size(); i++) {
|
|
||||||
// rowList.add("数据"+i);
|
|
||||||
// }
|
|
||||||
// List<List<String>> dataList = ListUtils.newArrayList();
|
|
||||||
// for (int i = 1; i <=100; i++) {
|
|
||||||
// dataList.add(rowList);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // 创建一个Sheet表,并导入数据
|
|
||||||
// try(ExcelExport ee = new ExcelExport("表格1", "表格标题1", headerList, null)){
|
|
||||||
//
|
|
||||||
// for (int i = 0; i < dataList.size(); i++) {
|
|
||||||
// Row row = ee.addRow();
|
|
||||||
// for (int j = 0; j < dataList.get(i).size(); j++) {
|
|
||||||
// ee.addCell(row, j, dataList.get(i).get(j));
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // 再创建一个Sheet表,并导入数据
|
|
||||||
// ee.createSheet("表格2", "表格标题2", headerList, null);
|
|
||||||
// for (int i = 0; i < dataList.size(); i++) {
|
|
||||||
// Row row = ee.addRow();
|
|
||||||
// for (int j = 0; j < dataList.get(i).size(); j++) {
|
|
||||||
// ee.addCell(row, j, dataList.get(i).get(j)+"2");
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // 输出到文件
|
|
||||||
// ee.writeFile("target/export.xlsx");
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// log.debug("Export success.");
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,13 +68,32 @@ public class ExcelImport implements Closeable {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造函数
|
* 构造函数
|
||||||
* @param path 导入文件对象,读取第一个工作表
|
* @param fileName 导入文件对象,读取第一个工作表
|
||||||
|
* @throws InvalidFormatException
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public ExcelImport(String fileName) throws InvalidFormatException, IOException {
|
||||||
|
this(new File(fileName));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造函数
|
||||||
|
* @param file 导入文件对象,读取第一个工作表
|
||||||
* @throws InvalidFormatException
|
* @throws InvalidFormatException
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public ExcelImport(File file) throws InvalidFormatException, IOException {
|
public ExcelImport(File file) throws InvalidFormatException, IOException {
|
||||||
this(file, 0, 0);
|
this(file, 0, 0);
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 构造函数
|
||||||
|
* @param fileName 导入文件对象,读取第一个工作表
|
||||||
|
* @throws InvalidFormatException
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public ExcelImport(String fileName, int headerNum) throws InvalidFormatException, IOException {
|
||||||
|
this(new File(fileName), headerNum);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造函数
|
* 构造函数
|
||||||
@@ -88,6 +107,19 @@ public class ExcelImport implements Closeable {
|
|||||||
this(file, headerNum, 0);
|
this(file, headerNum, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造函数
|
||||||
|
* @param fileName 导入文件对象
|
||||||
|
* @param headerNum 标题行数,数据行号=标题行数+1
|
||||||
|
* @param sheetIndexOrName 工作表编号或名称,从0开始
|
||||||
|
* @throws InvalidFormatException
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public ExcelImport(String fileName, int headerNum, Object sheetIndexOrName)
|
||||||
|
throws InvalidFormatException, IOException {
|
||||||
|
this(new File(fileName), headerNum, sheetIndexOrName);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造函数
|
* 构造函数
|
||||||
* @param file 导入文件对象
|
* @param file 导入文件对象
|
||||||
@@ -170,7 +202,21 @@ public class ExcelImport implements Closeable {
|
|||||||
public Workbook getWorkbook() {
|
public Workbook getWorkbook() {
|
||||||
return wb;
|
return wb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前工作表
|
||||||
|
*/
|
||||||
|
public Sheet getSheet() {
|
||||||
|
return sheet;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前标题行数
|
||||||
|
*/
|
||||||
|
public int getHeaderNum() {
|
||||||
|
return headerNum;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置当前工作表和标题行数
|
* 设置当前工作表和标题行数
|
||||||
* @author ThinkGem
|
* @author ThinkGem
|
||||||
@@ -528,25 +574,4 @@ public class ExcelImport implements Closeable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**
|
|
||||||
// * 导入测试
|
|
||||||
// */
|
|
||||||
// public static void main(String[] args) throws Throwable {
|
|
||||||
//
|
|
||||||
// ImportExcel ei = new ImportExcel("target/export.xlsx", 1);
|
|
||||||
//
|
|
||||||
// for (int i = ei.getDataRowNum(); i < ei.getLastDataRowNum(); i++) {
|
|
||||||
// Row row = ei.getRow(i);
|
|
||||||
// if (row == null){
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
// for (int j = 0; j < ei.getLastCellNum(); j++) {
|
|
||||||
// Object val = ei.getCellValue(row, j);
|
|
||||||
// System.out.print(val+", ");
|
|
||||||
// }
|
|
||||||
// System.out.println();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,92 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
|
||||||
|
* No deletion without permission, or be held responsible to law.
|
||||||
|
*/
|
||||||
|
package com.jeesite.test.utils.excel;
|
||||||
|
|
||||||
|
import com.jeesite.common.collect.ListUtils;
|
||||||
|
import com.jeesite.common.utils.excel.ExcelExport;
|
||||||
|
import com.jeesite.common.utils.excel.ExcelImport;
|
||||||
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 导出Excel文件测试类
|
||||||
|
* @author ThinkGem
|
||||||
|
* @version 2020-3-5
|
||||||
|
*/
|
||||||
|
public class ExcelExportTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 导出测试
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) throws Throwable {
|
||||||
|
|
||||||
|
File classPath = new File(ExcelExportTest.class.getResource("/").getFile());
|
||||||
|
String fileName = classPath.getParentFile().getAbsoluteFile() + "/export.xlsx";
|
||||||
|
|
||||||
|
// 初始化表头
|
||||||
|
List<String> headerList = ListUtils.newArrayList();
|
||||||
|
for (int i = 1; i <= 10; i++) {
|
||||||
|
headerList.add("表头"+i);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化数据集
|
||||||
|
List<String> rowList = ListUtils.newArrayList();
|
||||||
|
for (int i = 1; i <= headerList.size(); i++) {
|
||||||
|
rowList.add("数据"+i);
|
||||||
|
}
|
||||||
|
List<List<String>> dataList = ListUtils.newArrayList();
|
||||||
|
for (int i = 1; i <=100; i++) {
|
||||||
|
dataList.add(rowList);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建一个Sheet表,并导入数据
|
||||||
|
try(ExcelExport ee = new ExcelExport("表格1", "表格标题1", headerList, null)){
|
||||||
|
|
||||||
|
for (int i = 0; i < dataList.size(); i++) {
|
||||||
|
Row row = ee.addRow();
|
||||||
|
for (int j = 0; j < dataList.get(i).size(); j++) {
|
||||||
|
ee.addCell(row, j, dataList.get(i).get(j));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 再创建一个Sheet表,并导入数据
|
||||||
|
ee.createSheet("表格2", "表格标题2", headerList, null);
|
||||||
|
for (int i = 0; i < dataList.size(); i++) {
|
||||||
|
Row row = ee.addRow();
|
||||||
|
for (int j = 0; j < dataList.get(i).size(); j++) {
|
||||||
|
ee.addCell(row, j, dataList.get(i).get(j)+"2");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 输出到文件
|
||||||
|
ee.writeFile(fileName);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Export success.");
|
||||||
|
|
||||||
|
// 按模板导出,从第 3 行开始
|
||||||
|
ExcelImport ei = new ExcelImport(fileName, 3);
|
||||||
|
try(ExcelExport ee = new ExcelExport(ei)){
|
||||||
|
|
||||||
|
for (int i = 0; i < dataList.size(); i++) {
|
||||||
|
Row row = ee.addRow();
|
||||||
|
for (int j = 0; j < dataList.get(i).size(); j++) {
|
||||||
|
ee.addCell(row, j, dataList.get(i).get(j) + "-plus");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 输出到文件
|
||||||
|
ee.writeFile(fileName);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Export success by template.");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
|
||||||
|
* No deletion without permission, or be held responsible to law.
|
||||||
|
*/
|
||||||
|
package com.jeesite.test.utils.excel;
|
||||||
|
|
||||||
|
import com.jeesite.common.utils.excel.ExcelImport;
|
||||||
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 导入Excel文件测试类
|
||||||
|
* @author ThinkGem
|
||||||
|
* @version 2020-3-5
|
||||||
|
*/
|
||||||
|
public class ExcelImportTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 导入测试
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) throws Throwable {
|
||||||
|
|
||||||
|
File classPath = new File(ExcelExportTest.class.getResource("/").getFile());
|
||||||
|
String fileName = classPath.getParentFile().getAbsoluteFile() + "/export.xlsx";
|
||||||
|
ExcelImport ei = new ExcelImport(fileName, 1);
|
||||||
|
|
||||||
|
for (int i = ei.getDataRowNum(); i < ei.getLastDataRowNum(); i++) {
|
||||||
|
Row row = ei.getRow(i);
|
||||||
|
if (row == null){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (int j = 0; j < ei.getLastCellNum(); j++) {
|
||||||
|
Object val = ei.getCellValue(row, j);
|
||||||
|
System.out.print(val+", ");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Import success.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user