Commit 9bee2e7b by 钟明宏

增加对字段公司的解析 和 日期格式处理

parent 2166f298
package cn.yunmaozj.tools.excel.core.utils;
import cn.yunmaozj.tools.excel.exception.ExcelToolsException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
......@@ -25,6 +26,12 @@ public class ExcelRowTitleModel {
this.row = row;
}
/**
* 获取指定索引的excel表头
*
* @param index
* @return
*/
public String getTitle(int index) {
if (titles == null) {
titles = new Cell[row.getLastCellNum()];
......@@ -32,7 +39,20 @@ public class ExcelRowTitleModel {
titles[i] = row.getCell(i);
}
}
return titles[index].getStringCellValue();
if (index >= titles.length) {
StringBuilder titleBuilder = new StringBuilder("[");
for (Cell cell : titles) titleBuilder.append(cell == null ? "" : cell.getStringCellValue()).append(",");
if (titleBuilder.length() > 2) titleBuilder.deleteCharAt(titleBuilder.length()-1).append("]");
throw new ExcelToolsException("表头读取错误 表头实际列数:" + titles.length + "当前读取列数:" + index, 0, index, titleBuilder.toString(), null);
}
Cell cell = titles[index];
if (cell == null) {
StringBuilder titleBuilder = new StringBuilder("[");
for (Cell c : titles) titleBuilder.append(c == null ? "" : c.getStringCellValue()).append(",");
if (titleBuilder.length() > 2) titleBuilder.deleteCharAt(titleBuilder.length() - 1).append("]");
throw new ExcelToolsException("表头读取错误 表头实际列数:" + titles.length + "当前读取列数:" + index, 0, index, titleBuilder.toString(), null);
}
return cell.getStringCellValue();
}
public void setTitle(String[] titles) {
......
......@@ -40,7 +40,7 @@ public class ValueSelector<T> {
return new BigDecimal(cell.getNumericCellValue()).toString();
}
if (cell.getCellTypeEnum() == FORMULA) {
return new StringBuilder().append(cell.getCellFormula()).toString();
return new StringBuilder().append(cell.getRichStringCellValue()).toString();
}
return null;
}
......@@ -56,7 +56,7 @@ public class ValueSelector<T> {
return new BigDecimal(cell.getNumericCellValue()).intValue();
}
if (cell.getCellTypeEnum() == FORMULA) {
return Double.valueOf(cell.getNumericCellValue()).intValue();
return Integer.parseInt(cell.getRichStringCellValue().getString());
}
return 0;
}
......@@ -72,7 +72,7 @@ public class ValueSelector<T> {
return new BigDecimal(cell.getNumericCellValue()).intValue();
}
if (cell.getCellTypeEnum() == FORMULA) {
return Double.valueOf(cell.getNumericCellValue()).intValue();
return Double.valueOf(cell.getRichStringCellValue().getString()).intValue();
}
}
if (field.getType().equals(Double.class)) {
......@@ -87,7 +87,7 @@ public class ValueSelector<T> {
return new BigDecimal(cell.getNumericCellValue()).doubleValue();
}
if (cell.getCellTypeEnum() == FORMULA) {
return cell.getNumericCellValue();
return Double.valueOf(cell.getRichStringCellValue().getString());
}
}
if (field.getType().equals(double.class)) {
......@@ -102,7 +102,7 @@ public class ValueSelector<T> {
return new BigDecimal(cell.getNumericCellValue()).doubleValue();
}
if (cell.getCellTypeEnum() == FORMULA) {
return cell.getNumericCellValue();
return Double.valueOf(cell.getRichStringCellValue().getString());
}
}
if (field.getType().equals(Date.class)) {
......@@ -110,7 +110,17 @@ public class ValueSelector<T> {
return HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
}
if (cell.getCellTypeEnum() == FORMULA) {
return HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
String value = cell.getRichStringCellValue().getString();
ExcelFieldRow excelFieldRow = field.getAnnotation(ExcelFieldRow.class);
if (excelFieldRow == null || StringUtils.isEmpty(excelFieldRow.format())) {
// 假设这个只是时间戳
return new Date(Long.parseLong(value));
}
try {
return new SimpleDateFormat(excelFieldRow.format()).parse(value);
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
}
if (cell.getCellTypeEnum() == STRING) {
if (StringUtils.isEmpty(cell.getStringCellValue())) {
......
......@@ -4,6 +4,7 @@ import cn.yunmaozj.tools.excel.core.Excel;
import cn.yunmaozj.tools.excel.model.annotations.ExcelFieldRow;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.xmlbeans.SchemaType;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument;
......@@ -17,6 +18,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.WorkbookDocument
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class UserModel {
@ExcelFieldRow(title = "年龄")
......
......@@ -5,6 +5,8 @@ import cn.yunmaozj.tools.excel.model.annotations.Sheet;
import lombok.Getter;
import lombok.Setter;
import java.util.Date;
/**
* @author zhongminghong
* @email zhongmh@yunmaozj.com
......@@ -27,8 +29,8 @@ public class UserModel {
@ExcelFieldRow(title = "性别", allowableValues = {"男", "女"})
private String gender;
@ExcelFieldRow(title = "生日")
private String birthday;
@ExcelFieldRow(title = "生日",format = "yyyy/MM/dd")
private Date birthday;
@ExcelFieldRow(title = "个性签名")
private String signature;
......
......@@ -3,15 +3,17 @@ package com.yunmaozj.excel.opus;
import cn.yunmaozj.tools.excel.core.DefaultExcelExport;
import cn.yunmaozj.tools.excel.core.DefaultExcelImport;
import com.yunmaozj.excel.model.OpusModel;
import com.yunmaozj.excel.model.OpusModel;
import com.yunmaozj.excel.model.UserModel;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
......@@ -53,26 +55,80 @@ public class DefaultExcelImportTest {
List<OpusModel> list = defaultExcelImport.getData(1, new DefaultExcelImport.ImportSheet(OpusModel.class));
Assert.assertTrue(list.size() == 1);
OpusModel opusModel = list.get(0);
Assert.assertEquals("test1",opusModel.getTopics());
Assert.assertEquals("test1", opusModel.getTopics());
list = defaultExcelImport.getData(0, new DefaultExcelImport.ImportSheet(OpusModel.class));
Assert.assertTrue(list.size() == 1);
opusModel = list.get(0);
Assert.assertEquals("test",opusModel.getTopics());
Assert.assertEquals("test", opusModel.getTopics());
list = defaultExcelImport.getData("estimate", new DefaultExcelImport.ImportSheet(OpusModel.class));
Assert.assertTrue(list.size() == 1);
opusModel = list.get(0);
Assert.assertEquals("test1",opusModel.getTopics());
Assert.assertEquals("test1", opusModel.getTopics());
list = defaultExcelImport.getData(new DefaultExcelImport.ImportSheet(OpusModel.class));
Assert.assertTrue(list.size() == 1);
opusModel = list.get(0);
Assert.assertEquals("test",opusModel.getTopics());
Assert.assertEquals("test", opusModel.getTopics());
}
@Ignore
@Test
public void demo(){
public void demo() throws IOException {
DefaultExcelImport defaultExcelImport = new DefaultExcelImport("E:\\tmp\\avatar\\用户表-陈大为.xlsx");
List<UserModel> list_1 = defaultExcelImport.getData(0, new DefaultExcelImport.ImportSheet(UserModel.class));
defaultExcelImport = new DefaultExcelImport("E:\\tmp\\avatar\\用户表-孙嘉䜣.xlsx");
List<UserModel> list_2 = defaultExcelImport.getData(0, new DefaultExcelImport.ImportSheet(UserModel.class));
defaultExcelImport = new DefaultExcelImport("E:\\tmp\\avatar\\用户表-唐诗雨.xlsx");
List<UserModel> list_3 = defaultExcelImport.getData(0, new DefaultExcelImport.ImportSheet(UserModel.class));
defaultExcelImport = new DefaultExcelImport("E:\\tmp\\avatar\\用户表-文强.xlsx");
List<UserModel> list_4 = defaultExcelImport.getData(0, new DefaultExcelImport.ImportSheet(UserModel.class));
defaultExcelImport = new DefaultExcelImport("E:\\tmp\\avatar\\用户表-郑瀚.xlsx");
List<UserModel> list_5 = defaultExcelImport.getData(0, new DefaultExcelImport.ImportSheet(UserModel.class));
setRealAvatar(list_1,"/avatar/cdw");
setRealAvatar(list_2,"/avatar/sjx");
setRealAvatar(list_3,"/avatar/tsy");
setRealAvatar(list_4,"/avatar/wq");
setRealAvatar(list_5,"/avatar/zh");
DefaultExcelExport excelExport = new DefaultExcelExport();
excelExport.writeData(new DefaultExcelExport.ExportSheet(UserModel.class, "用户表-陈大为", list_1));
excelExport.writeData(new DefaultExcelExport.ExportSheet(UserModel.class, "用户表-孙嘉䜣", list_2));
excelExport.writeData(new DefaultExcelExport.ExportSheet(UserModel.class, "用户表-唐诗雨", list_3));
excelExport.writeData(new DefaultExcelExport.ExportSheet(UserModel.class, "用户表-文强", list_4));
excelExport.writeData(new DefaultExcelExport.ExportSheet(UserModel.class, "用户表-郑瀚", list_5));
excelExport.writeFile("E:\\tmp\\avatar\\运营用户表.xlsx");
//反写到每个单独的文件
excelExport = new DefaultExcelExport();
excelExport.writeData(new DefaultExcelExport.ExportSheet(UserModel.class, "用户表-陈大为", list_1));
excelExport.writeFile("E:\\tmp\\avatar\\用户表-陈大为.xlsx");
excelExport = new DefaultExcelExport();
excelExport.writeData(new DefaultExcelExport.ExportSheet(UserModel.class, "用户表-孙嘉䜣", list_2));
excelExport.writeFile("E:\\tmp\\avatar\\用户表-孙嘉䜣.xlsx");
excelExport = new DefaultExcelExport();
excelExport.writeData(new DefaultExcelExport.ExportSheet(UserModel.class, "用户表-唐诗雨", list_3));
excelExport.writeFile("E:\\tmp\\avatar\\用户表-唐诗雨.xlsx");
excelExport = new DefaultExcelExport();
excelExport.writeData(new DefaultExcelExport.ExportSheet(UserModel.class, "用户表-文强", list_4));
excelExport.writeFile("E:\\tmp\\avatar\\用户表-文强.xlsx");
excelExport = new DefaultExcelExport();
excelExport.writeData(new DefaultExcelExport.ExportSheet(UserModel.class, "用户表-郑瀚", list_5));
excelExport.writeFile("E:\\tmp\\avatar\\用户表-郑瀚.xlsx");
}
public void setRealAvatar(List<UserModel> list, String prefix) {
for (UserModel userModel : list) {
if(userModel != null && userModel.getAvatar() != null){
File file = new File(userModel.getAvatar());
userModel.setAvatar(prefix + "/" + file.getName());
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment