|
|
@ -63,8 +63,7 @@ public class ExcelUtil {
|
|
|
|
HSSFWorkbook workbook = new HSSFWorkbook();
|
|
|
|
HSSFWorkbook workbook = new HSSFWorkbook();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
//创建临时文件
|
|
|
|
//创建临时文件
|
|
|
|
System.out.println(exportClass.getSimpleName() + TimeTool.getNowTime(true));
|
|
|
|
excelFile = File.createTempFile(exportClass.getSimpleName() + new Date().getTime(), ".xls");
|
|
|
|
excelFile = File.createTempFile(exportClass.getSimpleName() + new Date().getTime(),".xls");
|
|
|
|
|
|
|
|
LOGGER.info("临时文件所在的本地路径:" + excelFile.getCanonicalPath());
|
|
|
|
LOGGER.info("临时文件所在的本地路径:" + excelFile.getCanonicalPath());
|
|
|
|
fos = new FileOutputStream(excelFile);
|
|
|
|
fos = new FileOutputStream(excelFile);
|
|
|
|
|
|
|
|
|
|
|
@ -192,11 +191,14 @@ public class ExcelUtil {
|
|
|
|
Row row;
|
|
|
|
Row row;
|
|
|
|
Object obj;
|
|
|
|
Object obj;
|
|
|
|
Object cellValue = null;
|
|
|
|
Object cellValue = null;
|
|
|
|
for (int i = 0; i < sheet.getLastRowNum(); i++) {
|
|
|
|
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
|
|
|
|
row = sheet.getRow(i);
|
|
|
|
row = sheet.getRow(i);
|
|
|
|
obj = importClass.newInstance();
|
|
|
|
obj = importClass.newInstance();
|
|
|
|
for (int j = 0; j < fields.length; j++) {
|
|
|
|
for (int j = 0; j < fields.length; j++) {
|
|
|
|
if (fields[j].getType() == String.class) {
|
|
|
|
row.getCell(j).setCellType(CellType.STRING);
|
|
|
|
|
|
|
|
if("".equals(row.getCell(j).getStringCellValue())){
|
|
|
|
|
|
|
|
cellValue = null;
|
|
|
|
|
|
|
|
}else if (fields[j].getType() == String.class) {
|
|
|
|
cellValue = row.getCell(j).getStringCellValue();
|
|
|
|
cellValue = row.getCell(j).getStringCellValue();
|
|
|
|
} else if (fields[j].getType() == Integer.class) {
|
|
|
|
} else if (fields[j].getType() == Integer.class) {
|
|
|
|
cellValue = Integer.parseInt(row.getCell(j).getStringCellValue());
|
|
|
|
cellValue = Integer.parseInt(row.getCell(j).getStringCellValue());
|
|
|
@ -204,7 +206,7 @@ public class ExcelUtil {
|
|
|
|
cellValue = Long.parseLong(row.getCell(j).getStringCellValue());
|
|
|
|
cellValue = Long.parseLong(row.getCell(j).getStringCellValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
importClass.getDeclaredMethod("set" + StringTool.toUpperCaseFirstOne(fields[i].getName()), fields[i].getType())
|
|
|
|
importClass.getDeclaredMethod("set" + StringTool.toUpperCaseFirstOne(fields[j].getName()), fields[j].getType())
|
|
|
|
.invoke(obj, cellValue);
|
|
|
|
.invoke(obj, cellValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dataList.add(obj);
|
|
|
|
dataList.add(obj);
|
|
|
@ -252,13 +254,19 @@ public class ExcelUtil {
|
|
|
|
* 导入模板
|
|
|
|
* 导入模板
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param exportClass
|
|
|
|
* @param exportClass
|
|
|
|
* @param exportCol
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static byte[] importTemplate(Class exportClass, String[] exportCol) {
|
|
|
|
public static byte[] importTemplate(Class exportClass) {
|
|
|
|
|
|
|
|
File excelFile = null;
|
|
|
|
|
|
|
|
FileOutputStream fos = null;
|
|
|
|
//创建HSSFWorkbook对象(excel的文档对象)
|
|
|
|
//创建HSSFWorkbook对象(excel的文档对象)
|
|
|
|
HSSFWorkbook workbook = new HSSFWorkbook();
|
|
|
|
HSSFWorkbook workbook = new HSSFWorkbook();
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
//创建临时文件
|
|
|
|
|
|
|
|
excelFile = File.createTempFile(exportClass.getSimpleName() + new Date().getTime(), "Tp.xls");
|
|
|
|
|
|
|
|
LOGGER.info("临时文件所在的本地路径:" + excelFile.getCanonicalPath());
|
|
|
|
|
|
|
|
fos = new FileOutputStream(excelFile);
|
|
|
|
|
|
|
|
|
|
|
|
//建立新的sheet对象(excel的表单)
|
|
|
|
//建立新的sheet对象(excel的表单)
|
|
|
|
HSSFSheet sheet = workbook.createSheet(exportClass.getSimpleName());
|
|
|
|
HSSFSheet sheet = workbook.createSheet(exportClass.getSimpleName());
|
|
|
|
|
|
|
|
|
|
|
@ -268,27 +276,23 @@ public class ExcelUtil {
|
|
|
|
|
|
|
|
|
|
|
|
// 类数据
|
|
|
|
// 类数据
|
|
|
|
Field[] declaredFields = exportClass.getDeclaredFields();
|
|
|
|
Field[] declaredFields = exportClass.getDeclaredFields();
|
|
|
|
Field[] fields = new Field[exportCol.length];
|
|
|
|
|
|
|
|
ApiParam fieldAnno;
|
|
|
|
ApiParam fieldAnno;
|
|
|
|
|
|
|
|
int col = 0;
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < exportCol.length; i++) {
|
|
|
|
for (int i = 0; i < declaredFields.length; i++) {
|
|
|
|
for (Field field : declaredFields) {
|
|
|
|
if (declaredFields[i].isAnnotationPresent(ApiParam.class)) {
|
|
|
|
if (field.getName().equals(exportCol[i])) {
|
|
|
|
fieldAnno = declaredFields[i].getAnnotation(ApiParam.class);
|
|
|
|
fields[i] = field;
|
|
|
|
if (!fieldAnno.hidden()) {
|
|
|
|
|
|
|
|
tableHeader.createCell(col, CellType.STRING).setCellValue(fieldAnno.value());
|
|
|
|
if (field.isAnnotationPresent(ApiParam.class)) {
|
|
|
|
|
|
|
|
fieldAnno = field.getAnnotation(ApiParam.class);
|
|
|
|
|
|
|
|
tableHeader.createCell(i, CellType.STRING).setCellValue(fieldAnno.value());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tableData.createCell(i, CellType.STRING).setCellValue(fieldAnno.example());
|
|
|
|
tableData.createCell(col, CellType.STRING).setCellValue(fieldAnno.example());
|
|
|
|
}
|
|
|
|
col++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
|
workbook.write(fos);
|
|
|
|
workbook.write(os);
|
|
|
|
return file2Byte(excelFile);
|
|
|
|
return os.toString("utf-8").getBytes();
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
|
|
|
@ -298,12 +302,36 @@ public class ExcelUtil {
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
workbook.close();
|
|
|
|
workbook.close();
|
|
|
|
|
|
|
|
fos.close();
|
|
|
|
|
|
|
|
excelFile.deleteOnExit();
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOGGER.error("IOException!", e);
|
|
|
|
LOGGER.error("IOException!", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取实体列集合
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param pojoClass
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static Map getColName(Class pojoClass) {
|
|
|
|
|
|
|
|
Map colName = new LinkedHashMap();
|
|
|
|
|
|
|
|
Field[] field = pojoClass.getDeclaredFields();
|
|
|
|
|
|
|
|
ApiParam fieldAnno;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < field.length; i++) {
|
|
|
|
|
|
|
|
if (field[i].isAnnotationPresent(ApiParam.class)) {
|
|
|
|
|
|
|
|
fieldAnno = field[i].getAnnotation(ApiParam.class);
|
|
|
|
|
|
|
|
if (!fieldAnno.hidden()) {
|
|
|
|
|
|
|
|
colName.put(field[i].getAnnotation(ApiParam.class).value(), field[i].getName());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return colName;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
List<SysTool> sysTools = new ArrayList<>();
|
|
|
|
List<SysTool> sysTools = new ArrayList<>();
|
|
|
@ -327,30 +355,22 @@ public class ExcelUtil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static byte[] file2Byte(File file) {
|
|
|
|
public static byte[] file2Byte(File file)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
byte[] buffer = null;
|
|
|
|
byte[] buffer = null;
|
|
|
|
try
|
|
|
|
try {
|
|
|
|
{
|
|
|
|
|
|
|
|
FileInputStream fis = new FileInputStream(file);
|
|
|
|
FileInputStream fis = new FileInputStream(file);
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
byte[] b = new byte[1024];
|
|
|
|
byte[] b = new byte[1024];
|
|
|
|
int n;
|
|
|
|
int n;
|
|
|
|
while ((n = fis.read(b)) != -1)
|
|
|
|
while ((n = fis.read(b)) != -1) {
|
|
|
|
{
|
|
|
|
|
|
|
|
bos.write(b, 0, n);
|
|
|
|
bos.write(b, 0, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fis.close();
|
|
|
|
fis.close();
|
|
|
|
bos.close();
|
|
|
|
bos.close();
|
|
|
|
buffer = bos.toByteArray();
|
|
|
|
buffer = bos.toByteArray();
|
|
|
|
}
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
catch (FileNotFoundException e)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
catch (IOException e)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
return buffer;
|
|
|
|