Excel导入导出工具优化,支持多Sheet。

This commit is contained in:
thinkgem
2018-01-17 22:09:19 +08:00
parent 1c5e7f5c83
commit caa7b9e4f1
6 changed files with 1100 additions and 1041 deletions

View File

@@ -25,14 +25,13 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.junit.Assert;
import com.jeesite.common.io.FileUtils; import com.jeesite.common.io.FileUtils;
import com.jeesite.common.text.DiffMatchPatch.Diff; import com.jeesite.common.text.DiffMatchPatch.Diff;
import com.jeesite.common.text.DiffMatchPatch.LinesToCharsResult; import com.jeesite.common.text.DiffMatchPatch.LinesToCharsResult;
import com.jeesite.common.text.DiffMatchPatch.Patch; import com.jeesite.common.text.DiffMatchPatch.Patch;
import junit.framework.Assert;
@SuppressWarnings("deprecation")
public class DiffMatchPatchTest { public class DiffMatchPatchTest {
private DiffMatchPatch dmp; private DiffMatchPatch dmp;

View File

@@ -75,12 +75,13 @@ public class ExcelExport {
/** /**
* 注解列表Object[]{ ExcelField, Field/Method } * 注解列表Object[]{ ExcelField, Field/Method }
*/ */
List<Object[]> annotationList = ListUtils.newArrayList(); List<Object[]> annotationList;
/** /**
* 构造函数 * 构造函数
* @param title 表格标题,传“空值”,表示无标题 * @param title 表格标题,传“空值”,表示无标题
* @param cls 实体对象通过annotation.ExportField获取标题 * @param cls 实体对象通过annotation.ExportField获取标题
* @param type 导出类型1:导出数据)
*/ */
public ExcelExport(String title, Class<?> cls){ public ExcelExport(String title, Class<?> cls){
this(title, cls, Type.EXPORT); this(title, cls, Type.EXPORT);
@@ -94,7 +95,7 @@ public class ExcelExport {
* @param groups 导入分组 * @param groups 导入分组
*/ */
public ExcelExport(String title, Class<?> cls, Type type, String... groups){ public ExcelExport(String title, Class<?> cls, Type type, String... groups){
this(null, null, title, cls, type, groups); this(null, title, cls, type, groups);
} }
/** /**
@@ -106,7 +107,57 @@ public class ExcelExport {
* @param type 导出类型1:导出数据2导出模板 * @param type 导出类型1:导出数据2导出模板
* @param groups 导入分组 * @param groups 导入分组
*/ */
public ExcelExport(Workbook wb, String sheetName, String title, Class<?> cls, Type type, String... groups){ public ExcelExport(Workbook wb, String title, Class<?> cls, Type type, String... groups){
if (wb != null){
this.wb = wb;
}else{
this.wb = createWorkbook();
}
this.createSheet(null, title, cls, type, groups);
}
/**
* 构造函数
* @param title 表格标题,传“空值”,表示无标题
* @param headerList 表头数组
*/
public ExcelExport(String title, List<String> headerList) {
this(null, null, title, headerList);
}
/**
* 构造函数
* @param wb 工作簿对象支持多个Sheet通过ExcelExport.createWorkbook()创建
* @param sheetName指定Sheet名称
* @param title 表格标题,传“空值”,表示无标题
* @param headerList 表头列表
*/
public ExcelExport(Workbook wb, String sheetName, String title, List<String> headerList) {
if (wb != null){
this.wb = wb;
}else{
this.wb = createWorkbook();
}
this.createSheet(sheetName, title, headerList, null);
}
/**
* 创建一个工作簿
*/
private Workbook createWorkbook(){
return new SXSSFWorkbook(500);
}
/**
* 创建工作表
* @param sheetName指定Sheet名称
* @param title 表格标题,传“空值”,表示无标题
* @param cls 实体对象通过annotation.ExportField获取标题
* @param type 导出类型1:导出数据2导出模板
* @param groups 导入分组
*/
public void createSheet(String sheetName, String title, Class<?> cls, Type type, String... groups){
this.annotationList = ListUtils.newArrayList();
// Get annotation field // Get annotation field
Field[] fs = cls.getDeclaredFields(); Field[] fs = cls.getDeclaredFields();
for (Field f : fs){ for (Field f : fs){
@@ -155,34 +206,8 @@ public class ExcelExport {
headerList.add(headerTitle); headerList.add(headerTitle);
headerWidthList.add(ef.width()); headerWidthList.add(ef.width());
} }
initialize(wb, sheetName, title, headerList, headerWidthList); // 创建工作表
} this.createSheet(sheetName, title, headerList, headerWidthList);
/**
* 构造函数
* @param title 表格标题,传“空值”,表示无标题
* @param headers 表头数组
*/
public ExcelExport(String title, List<String> headerList) {
this(null, null, title, headerList);
}
/**
* 构造函数
* @param wb 工作簿对象支持多个Sheet通过ExcelExport.createWorkbook()创建
* @param sheetName指定Sheet名称
* @param title 表格标题,传“空值”,表示无标题
* @param headerList 表头列表
*/
public ExcelExport(Workbook wb, String sheetName, String title, List<String> headerList) {
initialize(wb, sheetName, title, headerList, null);
}
/**
* 创建一个工作簿
*/
public static Workbook createWorkbook(){
return new SXSSFWorkbook(500);
} }
/** /**
@@ -212,14 +237,14 @@ public class ExcelExport {
} }
/** /**
* 初始化函数 * 创建工作表
* @param sheetName指定Sheet名称
* @param title 表格标题,传“空值”,表示无标题 * @param title 表格标题,传“空值”,表示无标题
* @param headerList 表头列表 * @param cls 实体对象通过annotation.ExportField获取标题
* @param type 导出类型1:导出数据2导出模板
* @param groups 导入分组
*/ */
private void initialize(Workbook wb, String sheetName, String title, List<String> headerList, List<Integer> headerWidth) { public void createSheet(String sheetName, String title, List<String> headerList, List<Integer> headerWidthList) {
if (wb == null){
this.wb = createWorkbook();
}
this.sheet = wb.createSheet(StringUtils.defaultString(sheetName, "Export")); this.sheet = wb.createSheet(StringUtils.defaultString(sheetName, "Export"));
this.styles = createStyles(wb); this.styles = createStyles(wb);
// Create title // Create title
@@ -253,11 +278,11 @@ public class ExcelExport {
} }
// sheet.autoSizeColumn(i); // sheet.autoSizeColumn(i);
} }
boolean isDefWidth = (headerWidth != null && headerWidth.size() == headerList.size()); boolean isDefWidth = (headerWidthList != null && headerWidthList.size() == headerList.size());
for (int i = 0; i < headerList.size(); i++) { for (int i = 0; i < headerList.size(); i++) {
int colWidth = -1; int colWidth = -1;
if (isDefWidth){ if (isDefWidth){
colWidth = headerWidth.get(i); colWidth = headerWidthList.get(i);
} }
if (colWidth == -1){ if (colWidth == -1){
colWidth = sheet.getColumnWidth(i)*2; colWidth = sheet.getColumnWidth(i)*2;
@@ -269,9 +294,29 @@ public class ExcelExport {
sheet.setColumnWidth(i, colWidth); sheet.setColumnWidth(i, colWidth);
} }
} }
log.debug("Initialize success."); log.debug("Create sheet {0} success.", sheetName);
} }
// /**
// * 构造函数
// * @param title 表格标题,传“空值”,表示无标题
// * @param headers 表头数组
// */
// public ExcelExport(String title, List<String> headerList) {
// this(null, null, title, headerList);
// }
//
// /**
// * 构造函数
// * @param wb 工作簿对象支持多个Sheet通过ExcelExport.createWorkbook()创建
// * @param sheetName指定Sheet名称
// * @param title 表格标题,传“空值”,表示无标题
// * @param headerList 表头列表
// */
// public ExcelExport(Workbook wb, String sheetName, String title, List<String> headerList) {
// initialize(wb, sheetName, title, headerList, null);
// }
/** /**
* 创建表格样式 * 创建表格样式
* @param wb 工作薄对象 * @param wb 工作薄对象

View File

@@ -131,15 +131,7 @@ public class ExcelImport {
}else{ }else{
throw new ExcelException("文档格式不正确!"); throw new ExcelException("文档格式不正确!");
} }
if (sheetIndexOrName instanceof Integer || sheetIndexOrName instanceof Long){ this.setSheet(sheetIndexOrName, headerNum);
this.sheet = this.wb.getSheetAt(ObjectUtils.toInteger(sheetIndexOrName));
}else{
this.sheet = this.wb.getSheet(ObjectUtils.toString(sheetIndexOrName));
}
if (this.sheet == null){
throw new ExcelException("没有找到工作表!");
}
this.headerNum = headerNum;
log.debug("Initialize success."); log.debug("Initialize success.");
} }
@@ -169,6 +161,30 @@ public class ExcelImport {
} }
} }
/**
* 获取当前工作薄
* @author ThinkGem
*/
public Workbook getWorkbook() {
return wb;
}
/**
* 设置当前工作表和标题行数
* @author ThinkGem
*/
public void setSheet(Object sheetIndexOrName, int headerNum) {
if (sheetIndexOrName instanceof Integer || sheetIndexOrName instanceof Long){
this.sheet = this.wb.getSheetAt(ObjectUtils.toInteger(sheetIndexOrName));
}else{
this.sheet = this.wb.getSheet(ObjectUtils.toString(sheetIndexOrName));
}
if (this.sheet == null){
throw new ExcelException("没有找到‘"+sheetIndexOrName+"’工作表!");
}
this.headerNum = headerNum;
}
/** /**
* 获取行对象 * 获取行对象
* @param rownum * @param rownum
@@ -191,7 +207,7 @@ public class ExcelImport {
* @return * @return
*/ */
public int getLastDataRowNum(){ public int getLastDataRowNum(){
//return this.sheet.getLastRowNum()+headerNum; //return this.sheet.getLastRowNum() + headerNum;
return this.sheet.getLastRowNum() + 1; return this.sheet.getLastRowNum() + 1;
} }

View File

@@ -12,7 +12,6 @@
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
<% if(__info_type == '0'){ %> <% if(__info_type == '0'){ %>
<li><a href="javascript:" data-href="http://jeesite.com" class="addTabPage"><i class="fa fa-diamond"></i> 官方网站</a></li> <li><a href="javascript:" data-href="http://jeesite.com" class="addTabPage"><i class="fa fa-diamond"></i> 官方网站</a></li>
<li><a href="javascript:" data-href="http://blog.jeesite.com" class="addTabPage"><i class="fa fa-codepen"></i> 作者博客</a></li>
<% } %> <% } %>
<li><a href="javascript:" id="fullScreen" title="全屏"><i class="fa fa-arrows-alt"></i></a></li> <li><a href="javascript:" id="fullScreen" title="全屏"><i class="fa fa-arrows-alt"></i></a></li>
<li><a href="javascript:" id="switchSkin" title="切换主题" style="margin-top:-1px;"><i class="fa fa-dashboard"></i></a></li> <li><a href="javascript:" id="switchSkin" title="切换主题" style="margin-top:-1px;"><i class="fa fa-dashboard"></i></a></li>

View File

@@ -24,14 +24,14 @@ public class InitCoreData extends com.jeesite.modules.db.InitCoreData {
initConfig(); initConfig();
initModule(); initModule();
initDict(); initDict();
// initRole(); initRole();
// initMenu(); initMenu();
// initUser(); initUser();
// initArea(); // initArea();
// initOffice(); initOffice();
// initCompany(); initCompany();
// initPost(); initPost();
// initEmpUser(); initEmpUser();
} }
} }