From c0239a9c6b931aa624c4a95c149fd5ab5591bbf4 Mon Sep 17 00:00:00 2001 From: chenlw <874313221@qq.com> Date: Fri, 21 Oct 2016 09:03:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/com/platform/test/ExcelTest.java | 101 +++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 3 deletions(-) diff --git a/test/com/platform/test/ExcelTest.java b/test/com/platform/test/ExcelTest.java index 8988970c..85fbf02a 100644 --- a/test/com/platform/test/ExcelTest.java +++ b/test/com/platform/test/ExcelTest.java @@ -5,15 +5,23 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; +import java.util.Collection; import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFWorkbook; +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.base.Constant; +import com.platform.utils.BeanCopy; +import com.platform.utils.Constant; import com.platform.entities.PreDataInfo; +import com.platform.entities.PreDataInfoFull; +import com.platform.entities.RegionalismEntity; import com.platform.utils.excelUtils.ExcelOperation; public class ExcelTest { @@ -26,14 +34,19 @@ public class ExcelTest { info.setAreaCode("321167"); info.setBeginUseTime("201107"); info.setFunctionDetails("这是一个神奇的功能"); + info.setDataVersion("2"); List list = new ArrayList(); for (int i = 0; i < 400; 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 { - result = ExcelOperation.writeExcelTemplate(Constant.SYSTEM_INFO_EXCEL_TEMPLATE_FIEL_PATH, list); + workbook = new XSSFWorkbook(res.getInputStream()); + result = ExcelOperation.writeExcelTemplate(workbook, Constant.EXCEL_TEMPLATE_INIT_ROW, list); } catch (IllegalArgumentException | IllegalAccessException | IOException e) { // TODO Auto-generated catch block @@ -59,5 +72,87 @@ public class ExcelTest { Date date2 = new Date(); System.out.println("complete: "+ (date2.getTime()-date.getTime())); } + + @SuppressWarnings("unchecked") + @Test + public void readData() { + List list = new ArrayList(); + //此处 耗时太久:(原因: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, PreDataInfo.class); + } 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 all = new ArrayList(); + all.add("a"); + all.add("b"); + List sub = new ArrayList(); + 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"); + + PreDataInfoFull full = new PreDataInfoFull(); + try { + BeanCopy.copyBean(info, full); + } 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 areaMap = new HashMap(); + 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 list = new ArrayList(); +// list.addAll((Collection) areaMap); + ; + for ( String key : areaMap.keySet()) { + list.add(areaMap.get(key)); + } + System.err.println(list.size()); + System.err.println(list.get(0).getCityName()); + } }