Commit 88147fb0 by zhongmh

add

parent 3d4fb17e
......@@ -35,7 +35,8 @@ public class ExcelRowDataModel<T> {
PropertyDescriptor propertyDescriptor = modelPropertyDescriptor.getPropertyDescriptorByExcelTitle(excelTitle);
Field field = modelPropertyDescriptor.getFiledByExcelTitle(excelTitle);
if (propertyDescriptor != null && field != null) {
propertyDescriptor.getWriteMethod().invoke(entity, ValueSelector.select(cell, field));
Object value = ValueSelector.select(cell, field);
propertyDescriptor.getWriteMethod().invoke(entity, value);
}
}
return entity;
......@@ -58,7 +59,7 @@ public class ExcelRowDataModel<T> {
Field field = modelPropertyDescriptor.getFiledByExcelTitle(excelTitle);
try {
Object value = propertyDescriptor.getReadMethod().invoke(entity);
ValueSelector.selectAndSet(row,i,field,value);
ValueSelector.selectAndSet(row, i, field, value);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
......
......@@ -2,6 +2,7 @@ package cn.yunmaozj.tools.excel.utils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.util.StringUtil;
import java.lang.reflect.Field;
import java.math.BigDecimal;
......@@ -32,7 +33,8 @@ public class ValueSelector {
}
if (field.getType().equals(Integer.class)) {
if (cell.getCellTypeEnum() == STRING) {
return Integer.parseInt(cell.getStringCellValue());
String value = cell.getStringCellValue();
return StringUtils.hasLength(value) ? Integer.parseInt(cell.getStringCellValue()) : null;
}
if (cell.getCellTypeEnum() == BOOLEAN) {
return cell.getBooleanCellValue() ? 0 : 1;
......@@ -42,13 +44,83 @@ public class ValueSelector {
}
return false;
}
if(field.getType().equals(int.class)){
if (cell.getCellTypeEnum() == STRING) {
String value = cell.getStringCellValue();
return StringUtils.hasLength(value) ? Integer.parseInt(cell.getStringCellValue()) : 0;
}
if (cell.getCellTypeEnum() == BOOLEAN) {
return cell.getBooleanCellValue() ? 0 : 1;
}
if (cell.getCellTypeEnum() == NUMERIC) {
return new BigDecimal(cell.getNumericCellValue()).intValue();
}
}
if(field.getType().equals(Double.class)){
if (cell.getCellTypeEnum() == STRING) {
String value = cell.getStringCellValue();
return StringUtils.hasLength(value) ? Double.parseDouble(cell.getStringCellValue()) : null;
}
if (cell.getCellTypeEnum() == BOOLEAN) {
return cell.getBooleanCellValue() ? 0 : 1;
}
if (cell.getCellTypeEnum() == NUMERIC) {
return new BigDecimal(cell.getNumericCellValue()).doubleValue();
}
}
if(field.getType().equals(double.class)){
if (cell.getCellTypeEnum() == STRING) {
String value = cell.getStringCellValue();
return StringUtils.hasLength(value) ? Double.parseDouble(cell.getStringCellValue()) : 0;
}
if (cell.getCellTypeEnum() == BOOLEAN) {
return cell.getBooleanCellValue() ? 0 : 1;
}
if (cell.getCellTypeEnum() == NUMERIC) {
return new BigDecimal(cell.getNumericCellValue()).doubleValue();
}
}
return cell;
}
public static void selectAndSet(Row row, int index, Field field, Object value) {
if (field.getType().equals(String.class)) {
Cell cell = row.createCell(index);
Cell cell = row.createCell(index, STRING);
cell.setCellValue((String) value);
}
if (field.getType().equals(Integer.class)) {
Cell cell = row.createCell(index, NUMERIC);
if (value == null) {
cell.setCellValue("");
} else {
cell.setCellValue(value.toString());
}
}
if (field.getType().equals(int.class)) {
Cell cell = row.createCell(index, NUMERIC);
if (value == null) {
cell.setCellValue(0);
} else {
cell.setCellValue(value.toString());
}
}
if (field.getType().equals(Double.class)) {
Cell cell = row.createCell(index, NUMERIC);
if (value == null) {
cell.setCellValue("");
} else {
cell.setCellValue((Double) value);
}
}
if (field.getType().equals(double.class)) {
Cell cell = row.createCell(index, NUMERIC);
if (value == null) {
cell.setCellValue(0);
} else {
cell.setCellValue((Double) value);
}
}
}
}
......@@ -43,4 +43,89 @@ public class OpusModel {
@ExcelFieldRow("like number")
private int likeNumber;
@ExcelFieldRow("opus weight")
private Double opusWeight;
@ExcelFieldRow("sys weight")
private double sysWeight;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getResource() {
return resource;
}
public void setResource(String resource) {
this.resource = resource;
}
public String getOpusType() {
return opusType;
}
public void setOpusType(String opusType) {
this.opusType = opusType;
}
public String getTopics() {
return topics;
}
public void setTopics(String topics) {
this.topics = topics;
}
public Integer getBrowseNumber() {
return browseNumber;
}
public void setBrowseNumber(Integer browseNumber) {
this.browseNumber = browseNumber;
}
public int getLikeNumber() {
return likeNumber;
}
public void setLikeNumber(int likeNumber) {
this.likeNumber = likeNumber;
}
public Double getOpusWeight() {
return opusWeight;
}
public void setOpusWeight(Double opusWeight) {
this.opusWeight = opusWeight;
}
public double getSysWeight() {
return sysWeight;
}
public void setSysWeight(double sysWeight) {
this.sysWeight = sysWeight;
}
}
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 org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class DefaultExcelExportTest {
......@@ -33,10 +35,17 @@ public class DefaultExcelExportTest {
model.setBrowseNumber(0);
model.setLikeNumber(100);
model.setOpusWeight(10.34324324);
model.setSysWeight(89.42343243243);
DefaultExcelExport excelExport = new DefaultExcelExport();
excelExport.writeData(new DefaultExcelExport.ExportSheet(OpusModel.class, Arrays.asList(model)));
excelExport.writeData(new DefaultExcelExport.ExportSheet(OpusModel.class, "sheet 2", Arrays.asList(model)));
excelExport.writeFile(excelFile);
DefaultExcelImport excelImport = new DefaultExcelImport(excelFile);
List<OpusModel> list = excelImport.getData(new DefaultExcelImport.ImportSheet<>(OpusModel.class));
System.out.println(list.get(0));
}
}
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