You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
aggregation-platform/test/com/platform/test/ExcelTest.java

163 lines
5.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.platform.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import com.platform.entities.PreDataInfo;
import com.platform.entities.RegionalismEntity;
import com.platform.utils.BeanCopy;
import com.platform.utils.Constant;
import com.platform.utils.excelUtils.ExcelOperation;
public class ExcelTest {
@Test
public void writeData() {
Date date = new Date();
PreDataInfo info = new PreDataInfo();
info.setAnalysisOfFinancialEconomicProsperity("否");
info.setGovernmentDebtManage("是");;
info.setAreaCode("321167");
info.setBeginUseTime("201107");
info.setFunctionDetails("这是一个神奇的功能");
info.setDataVersion("2");
List<PreDataInfo> list = new ArrayList<PreDataInfo>();
for (int i = 0; i < 4; i++) {
list.add(info);
}
// InputStreamReader in= new InputStreamReader(Resource.class.getResourceAsStream(templateFilePath), "UTF-8");
ClassPathResource res = new ClassPathResource(Constant.SYSTEM_INFO_EXCEL_TEMPLATE_FIEL_PATH);
//此处 耗时太久原因excel 2007 以后采用的方式不同造成的建议升级 到 poi 3.15以上)
XSSFWorkbook workbook = null;
XSSFWorkbook result = null;
try {
workbook = new XSSFWorkbook(res.getInputStream());
result = ExcelOperation.writeExcelTemplate(workbook, Constant.EXCEL_TEMPLATE_INIT_ROW, 0, 25, list,"id", "workRange","sysCode","updateTime");
} catch (IllegalArgumentException
| IllegalAccessException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("----"+result);
try {
File f = new File("D:/test/6.xlsx");
FileOutputStream fileOut = null;
if (f.exists()) {
f.createNewFile();
fileOut = new FileOutputStream(f);
result.write(fileOut);
fileOut.close();
}
else {
f.createNewFile();
fileOut = new FileOutputStream(f);
result.write(fileOut);
fileOut.close();
}
File f3 = new File("D:/test/3.xlsx");
f3.delete();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Date date2 = new Date();
System.out.println("complete "+ (date2.getTime()-date.getTime()));
}
@SuppressWarnings("unchecked")
@Test
public void readData() {
List<PreDataInfo> list = new ArrayList<PreDataInfo>();
//此处 耗时太久原因excel 2007 以后采用的方式不同造成的建议升级 到 poi 3.15以上)
File f = new File("D:/test/4.xlsx");
Date d = new Date();
XSSFWorkbook workbook = null;
try {
workbook = new XSSFWorkbook(f);
list = ExcelOperation.readExcel4Update(workbook, Constant.EXCEL_TEMPLATE_INIT_ROW, 0, PreDataInfo.class, "workRange","id","sysCode","updateTime");
} catch (IllegalArgumentException
| IllegalAccessException | InstantiationException | InvalidFormatException | IOException e) {
e.printStackTrace();
}
System.err.println(new Date().getTime() - d.getTime());
System.out.println("----"+list.size());
}
@Test
public void listRemoveAllTest() {
// TODO Auto-generated method stub
List<String> all = new ArrayList<String>();
all.add("a");
all.add("b");
List<String> sub = new ArrayList<String>();
sub.add("a");
all.removeAll(sub);
System.out.println(all.size());
}
@Test
public void copyTest() {
// TODO Auto-generated method stub
PreDataInfo info = new PreDataInfo();
info.setAnalysisOfFinancialEconomicProsperity("否");
info.setAreaCode("321167");
info.setBeginUseTime("201107");
info.setFunctionDetails("这是一个神奇的功能");
info.setDataVersion("2");
PreDataInfo full = new PreDataInfo();
try {
BeanCopy.copyBean(info, full,"id");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.err.println(full.getAreaCode());
System.err.println(full.getBeginUseTime()+"------\n");
// PreDataInfoFull full2 = (PreDataInfoFull) info;
// System.err.println(full2.getAreaCode());
// System.err.println(full2.getBeginUseTime());
}
@Test
public void Map2List() {
Map<String,RegionalismEntity> areaMap = new HashMap<String, RegionalismEntity>();
RegionalismEntity region = new RegionalismEntity();
region.setCode("321");
region.setCityName("市");
region.setDistrictName("县");
areaMap.put(region.getCode(), region);
RegionalismEntity region2 = new RegionalismEntity();
region2.setCode("3221");
region2.setCityName("市2");
region2.setDistrictName("县2");
areaMap.put(region2.getCode(), region2);
List<RegionalismEntity> list = new ArrayList<RegionalismEntity>();
// list.addAll((Collection<? extends RegionalismEntity>) areaMap);
;
for ( String key : areaMap.keySet()) {
list.add(areaMap.get(key));
}
System.err.println(list.size());
System.err.println(list.get(0).getCityName());
}
}