From 6a6a0b98b9b3e4ae0b82d3a70ba60d4502368ab5 Mon Sep 17 00:00:00 2001 From: chenlw <874313221@qq.com> Date: Wed, 19 Oct 2016 08:45:59 +0800 Subject: [PATCH] =?UTF-8?q?excel=E5=86=99=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utils/excelUtils/ExcelOperation.java | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/com/platform/utils/excelUtils/ExcelOperation.java b/src/com/platform/utils/excelUtils/ExcelOperation.java index 86def9b8..f962c28b 100644 --- a/src/com/platform/utils/excelUtils/ExcelOperation.java +++ b/src/com/platform/utils/excelUtils/ExcelOperation.java @@ -1,27 +1,24 @@ package com.platform.utils.excelUtils; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.lang.reflect.Field; +import java.util.Date; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; -import org.apache.poi.hssf.usermodel.HSSFFont; -import org.apache.poi.hssf.usermodel.HSSFRow; -import org.apache.poi.hssf.usermodel.HSSFSheet; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; -import org.apache.poi.xssf.usermodel.XSSFFactory; import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.core.io.ClassPathResource; +import com.base.Constant; + /** * @author chen * excel2007操作 @@ -37,15 +34,17 @@ public class ExcelOperation { * @throws IllegalAccessException * @throws IllegalArgumentException */ - public static XSSFWorkbook readExcelTemplate(String templateFilePath, List list) throws FileNotFoundException, IOException, IllegalArgumentException, IllegalAccessException { - System.out.println(templateFilePath); + public static XSSFWorkbook writeExcelTemplate(String templateFilePath, List list) throws FileNotFoundException, IOException, IllegalArgumentException, IllegalAccessException { // InputStreamReader in= new InputStreamReader(Resource.class.getResourceAsStream(templateFilePath), "UTF-8"); ClassPathResource res = new ClassPathResource(templateFilePath); - XSSFWorkbook workbook = new XSSFWorkbook(res.getInputStream());//getAbsolutePath() - + //此处 耗时太久:(原因:excel 2007 以后采用的方式不同造成的建议升级 到 poi 3.15以上) + Date date = new Date(); + XSSFWorkbook workbook = new XSSFWorkbook(res.getInputStream()); + Date date2 = new Date(); + System.out.println(" new XSSFWorkbook: "+ (date2.getTime()-date.getTime())); //从第几行 填充数据 - int excelInitRow = 10; + int excelInitRow = Constant.EXCEL_TEMPLATE_INIT_ROW; //读取工作表 XSSFSheet sheet = workbook.getSheetAt(0);//getSheet("Sheet1"); XSSFRow row; @@ -76,6 +75,12 @@ public class ExcelOperation { } for (int k = 0; k < lengths; k++) { sheet.autoSizeColumn(k); + int width = sheet.getColumnWidth(k); + if (width < 1600) { + sheet.setColumnWidth(k, 1600); + }else { + sheet.setColumnWidth(k, width + 1000); + } } return workbook; }