excel写入

web_backend_develope
chenlw 9 years ago
parent fd2be08a8d
commit 6a6a0b98b9

@ -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;
}

Loading…
Cancel
Save