Commit f8bb7e43 by zhongmh

add export function

parent 9d20efe8
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" name="Maven: org.apache.poi:poi:3.17" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.10" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-collections4:4.1" level="project" />
<orderEntry type="library" name="Maven: org.apache.poi:poi-scratchpad:3.17" level="project" />
<orderEntry type="library" name="Maven: org.apache.poi:poi-examples:3.17" level="project" />
<orderEntry type="library" name="Maven: org.apache.poi:poi-excelant:3.17" level="project" />
<orderEntry type="library" name="Maven: org.apache.ant:ant:1.8.2" level="project" />
<orderEntry type="library" name="Maven: org.apache.ant:ant-launcher:1.8.2" level="project" />
<orderEntry type="library" name="Maven: org.apache.poi:poi-ooxml-schemas:3.17" level="project" />
<orderEntry type="library" name="Maven: org.apache.xmlbeans:xmlbeans:2.6.0" level="project" />
<orderEntry type="library" name="Maven: stax:stax-api:1.0.1" level="project" />
<orderEntry type="library" name="Maven: org.apache.poi:poi-ooxml:3.17" level="project" />
<orderEntry type="library" name="Maven: com.github.virtuald:curvesapi:1.04" level="project" />
<orderEntry type="library" name="Maven: com.monitorjbl:xlsx-streamer:1.2.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.poi:ooxml-schemas:1.3" level="project" />
<orderEntry type="library" name="Maven: xerces:xercesImpl:2.11.0" level="project" />
<orderEntry type="library" name="Maven: xml-apis:xml-apis:1.4.01" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.12" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.8" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.8" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-log4j12:1.7.25" level="project" />
<orderEntry type="library" name="Maven: log4j:log4j:1.2.17" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: mysql:mysql-connector-java:8.0.15" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.google.protobuf:protobuf-java:3.6.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.projectlombok:lombok:1.18.12" level="project" />
</component>
</module>
\ No newline at end of file
package cn.yunmaozj.tools.excel.core;
import cn.yunmaozj.tools.excel.core.utils.ExcelRowDataModel;
import cn.yunmaozj.tools.excel.core.utils.ExcelRowTitleModel;
import cn.yunmaozj.tools.excel.model.ModelPropertyDescriptor;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
public class DefaultExcelExport {
private Workbook workbook;
public DefaultExcelExport() {
this.workbook = new SXSSFWorkbook();
}
public DefaultExcelExport writeData(ExportSheet sheet) {
sheet.writeData(workbook);
return this;
}
public void writeFile(String filePath) throws IOException {
writeFile(new FileOutputStream(filePath));
}
public void writeFile(OutputStream outputStream) throws IOException {
workbook.write(outputStream);
}
public static class ExportSheet<T> {
private ModelPropertyDescriptor<T> modelPropertyDescriptor;
private String sheetName;
private List<T> datas;
public ExportSheet(Class<T> model, List<T> datas) {
this.modelPropertyDescriptor = new ModelPropertyDescriptor(model);
sheetName = modelPropertyDescriptor.getModelName();
this.datas = datas;
}
public ExportSheet(Class<T> model,String sheetName,List<T> datas) {
this.modelPropertyDescriptor = new ModelPropertyDescriptor(model);
this.sheetName = sheetName;
this.datas = datas;
}
private void writeData(Workbook workbook) {
Sheet sheet = workbook.createSheet(sheetName);
int rowIndex = 0;
ExcelRowTitleModel titleModel = new ExcelRowTitleModel(sheet.createRow(rowIndex));
titleModel.setTitle(modelPropertyDescriptor.excelTitle().toArray(new String[modelPropertyDescriptor.excelTitle().size()]));
ExcelRowDataModel<T> rowDataModel = new ExcelRowDataModel<>(modelPropertyDescriptor, titleModel);
for (int i = 0; i < datas.size(); i++) {
rowDataModel.readEntity(sheet.createRow(++rowIndex), datas.get(i));
}
}
}
}
......@@ -24,38 +24,69 @@ import java.util.Map;
*/
public class DefaultExcelImport<T> {
private ModelPropertyDescriptor modelPropertyDescriptor;
private InputStream inputStream;
private Workbook workbook;
public DefaultExcelImport(Class<T> model, String path) throws FileNotFoundException {
this(model, new File(path));
public DefaultExcelImport(String path) throws IOException {
this(new File(path));
}
public DefaultExcelImport(Class<T> model, File file) throws FileNotFoundException {
this(model, new FileInputStream(file));
public DefaultExcelImport(File file) throws IOException {
this(new FileInputStream(file));
}
public DefaultExcelImport(Class<T> model, InputStream inputStream) {
this.modelPropertyDescriptor = new ModelPropertyDescriptor(model);
this.inputStream = inputStream;
public DefaultExcelImport(InputStream inputStream) throws IOException {
this.workbook = new XSSFWorkbook(inputStream);
}
public List<T> getData() throws IOException {
List<T> data = new ArrayList<>();
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheet("Sheet1");
Iterator<Row> iterable = sheet.rowIterator();
ExcelRowDataModel<T> excelRowDataModel = null;
int i = 0;
while (iterable.hasNext()) {
if (i++ == 0) {
ExcelRowTitleModel excelRowTitleModel = new ExcelRowTitleModel(iterable.next());
excelRowDataModel = new ExcelRowDataModel<>(modelPropertyDescriptor, excelRowTitleModel);
} else {
T entity = excelRowDataModel.getEntity(iterable.next());
data.add(entity);
public List<T> getData(ImportSheet<T> sheet) throws IOException {
return sheet.getData(workbook);
}
public List<T> getData(String sheetName,ImportSheet<T> sheet) throws IOException {
return sheet.getData(sheetName,workbook);
}
public List<T> getData(int index,ImportSheet<T> sheet) throws IOException {
return sheet.getData(index,workbook);
}
public static class ImportSheet<T> {
private ModelPropertyDescriptor<T> modelPropertyDescriptor;
public ImportSheet(Class<T> model) {
this.modelPropertyDescriptor = new ModelPropertyDescriptor<>(model);
}
private List<T> getData(Workbook workbook) {
return getData(modelPropertyDescriptor.getModelName(), workbook);
}
private List<T> getData(String sheetName, Workbook workbook) {
Sheet sheet = workbook.getSheet(sheetName);
return getData(sheet);
}
private List<T> getData(int index, Workbook workbook) {
Sheet sheet = workbook.getSheetAt(index);
return getData(sheet);
}
private List<T> getData(Sheet sheet) {
List<T> data = new ArrayList<>();
Iterator<Row> iterable = sheet.rowIterator();
ExcelRowDataModel<T> excelRowDataModel = null;
int i = 0;
while (iterable.hasNext()) {
if (i++ == 0) {
ExcelRowTitleModel excelRowTitleModel = new ExcelRowTitleModel(iterable.next());
excelRowDataModel = new ExcelRowDataModel<>(modelPropertyDescriptor, excelRowTitleModel);
} else {
T entity = excelRowDataModel.getEntity(iterable.next());
data.add(entity);
}
}
return data;
}
return data;
}
}
......@@ -51,4 +51,20 @@ public class ExcelRowDataModel<T> {
return null;
}
public void readEntity(Row row, T entity) {
for (int i = 0; i < excelRowTitleModel.length(); i++) {
String excelTitle = excelRowTitleModel.getTitle(i);
PropertyDescriptor propertyDescriptor = modelPropertyDescriptor.getPropertyDescriptorByExcelTitle(excelTitle);
Field field = modelPropertyDescriptor.getFiledByExcelTitle(excelTitle);
try {
Object value = propertyDescriptor.getReadMethod().invoke(entity);
ValueSelector.selectAndSet(row,i,field,value);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
package cn.yunmaozj.tools.excel.core.utils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import java.util.List;
import static org.apache.poi.ss.usermodel.CellType.STRING;
/**
* @author zhongminghong
* @email zhongmh@yunmaozj.com
......@@ -16,15 +19,33 @@ public class ExcelRowTitleModel {
* 表头
*/
private Cell[] titles;
private String[] excelTitles;
private Row row;
public ExcelRowTitleModel(Row row) {
titles = new Cell[row.getLastCellNum()];
for (int i = 0; i < titles.length; i++) {
titles[i] = row.getCell(i);
}
this.row = row;
}
public String getTitle(int index) {
if (titles == null) {
titles = new Cell[row.getLastCellNum()];
for (int i = 0; i < titles.length; i++) {
titles[i] = row.getCell(i);
}
}
return titles[index].getStringCellValue();
}
public void setTitle(String[] titles) {
this.titles = new Cell[titles.length];
for (int i = 0; i < titles.length; i++) {
Cell cell = row.createCell(i, STRING);
cell.setCellValue(titles[i]);
this.titles[i] = cell;
}
}
public int length() {
return titles.length;
}
}
package cn.yunmaozj.tools.excel.model;
import cn.yunmaozj.tools.excel.model.annotations.ExcelFieldRow;
import cn.yunmaozj.tools.excel.model.annotations.Sheet;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
......@@ -137,4 +138,12 @@ public class ModelPropertyDescriptor<T> {
public T getInstance() throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
return this.model.getDeclaredConstructor().newInstance();
}
public String getModelName() {
Sheet sheet = model.getAnnotation(Sheet.class);
if (sheet != null) {
return sheet.value();
}
return model.getName();
}
}
package cn.yunmaozj.tools.excel.utils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import java.lang.reflect.Field;
import java.math.BigDecimal;
......@@ -43,4 +44,11 @@ public class ValueSelector {
}
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.setCellValue((String) value);
}
}
}
......@@ -17,7 +17,7 @@ import lombok.ToString;
@ToString
@ExcelSummaryInformation
@Sheet("作品")
public class OpusImportModel {
public class OpusModel {
@ExcelFieldRow("标题")
private String title;
......
package com.yunmaozj.excel.opus;
import cn.yunmaozj.tools.excel.core.DefaultExcelExport;
import com.yunmaozj.excel.model.OpusModel;
import org.junit.Test;
import java.io.IOException;
import java.util.Arrays;
public class DefaultExcelExportTest {
@Test
public void writeFileTest() throws IOException {
OpusModel model = new OpusModel();
model.setContent("I wang ride bicycle");
model.setTitle("English");
model.setMobile("1521521234");
model.setResource("/2020/22/2/2/2.jpg");
model.setOpusType("video");
model.setTopics("test");
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("/tmp/test.xlsx");
}
}
package com.yunmaozj.excel.opus;
import cn.yunmaozj.tools.excel.core.DefaultExcelExport;
import cn.yunmaozj.tools.excel.core.DefaultExcelImport;
import com.yunmaozj.excel.model.OpusImportModel;
import com.yunmaozj.excel.model.OpusModel;
import com.yunmaozj.excel.model.OpusModel;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
/**
......@@ -18,13 +23,42 @@ import java.util.List;
*/
public class DefaultExcelImportTest {
@Ignore
private String excelFile;
@Before
public void generateFile() throws IOException {
excelFile = new StringBuilder().append("/tmp/").append(System.currentTimeMillis()).append(".xlsx").toString();
OpusModel model = new OpusModel();
model.setContent("I wang ride bicycle");
model.setTitle("English");
model.setMobile("1521521234");
model.setResource("/2020/22/2/2/2.jpg");
model.setOpusType("video");
model.setTopics("test");
DefaultExcelExport excelExport = new DefaultExcelExport();
excelExport.writeData(new DefaultExcelExport.ExportSheet(OpusModel.class, Arrays.asList(model)));
model.setTopics("test1");
excelExport.writeData(new DefaultExcelExport.ExportSheet(OpusModel.class, "estimate", Arrays.asList(model)));
excelExport.writeFile(excelFile);
}
@Test
public void getData() throws IOException {
DefaultExcelImport defaultExcelImport = new DefaultExcelImport(OpusImportModel.class,"E://tmp//作品批量导入模板.xlsx");
List<OpusImportModel> list = defaultExcelImport.getData();
for(OpusImportModel model : list){
System.out.println(model);
}
DefaultExcelImport defaultExcelImport = new DefaultExcelImport(excelFile);
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());
list = defaultExcelImport.getData(0, new DefaultExcelImport.ImportSheet(OpusModel.class));
Assert.assertTrue(list.size() == 1);
opusModel = list.get(0);
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());
}
}
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