package com.zhjy.common.excel;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.regex.Pattern;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.eos.foundation.common.io.FileUtil;
import commonj.sdo.DataObject;
/**
* @author
* @Email 826331692@qq.com
* @date 2016年7月18日 下午9:03:29
* @version 1.0
*/
public class ExportExcelPage<T> {
public int INDEX = 0;//表头位置
public int NUM =0;//偏移量
public String fileName = null;
public ArrayList<String> titles =null;
public SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public Pattern p = Pattern.compile("^//d+(//.//d+)?$");
public Object value;
public java.util.regex.Matcher matcher;
public DataObject content;
public int sheetNum=0;
public ArrayList<String> fileNameArr = new ArrayList<String>();//文件路径列表
public String tempName;
public String outPath;
public SXSSFWorkbook wb;
public FileOutputStream out;
public Sheet sheet ;
public ExportExcelPage(int index,int num,String tempName,String outPath) {
this.INDEX=index;
this.NUM=num;
this.tempName=tempName;
this.outPath=outPath;
init(tempName,outPath);
}
public ExportExcelPage() {
super();
}
public String getFileName(){
return fileName;
}
/*
* 初始化方法(加载模板)
*/
public void init(String tempName,String outPath){
try {
File file = new File(outPath);
if (!file.exists()) {
// 创建临时目录
try {
FileUtil.mkDir(outPath);
} catch (IOException e) {
e.printStackTrace();
}
}
XSSFWorkbook templet = (XSSFWorkbook) ReadWriteExcel.getTemplet(tempName);
Sheet sheet = templet.getSheetAt(0);
titles = ReadWriteExcel.getTitles(sheet);
Date date = new Date();
Long time = date.getTime();
fileName = time.toString();
fileName = outPath+fileName+".xlsx";
fileNameArr.add(fileName);
out = new FileOutputStream(fileName);
// FileOutputStream exportXls = new FileOutputStream(fileName);
wb = new SXSSFWorkbook(templet,100);
wb.setCompressTempFiles(true);
this.sheet = wb.getSheetAt(0); // 获取到工作表,因为一个excel可能有多个工作表
// templet.write(exportXls);
// exportXls.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public void exportExcel(Collection<T> dataset) {
String path = fileName;
try {
// InputStream fs = new FileInputStream(path); //
// XSSFWorkbook wb = new XSSFWorkbook(new BufferedInputStream(fs));
// SXSSFWorkbook wb = new SXSSFWorkbook(wb_old);
Sheet sheet = wb.getSheetAt(0); // 获取到工作表,因为一个excel可能有多个工作表
Row row = sheet.getRow(0); //
// 获取第一行(excel中的行默认从0开始,所以这就是为什么,一个excel必须有字段列头),即,字段列头,便于赋值
System.out.println("写入位置");
System.out.println("第" + sheet.getLastRowNum()); // 分别得到最后一行的行号,和一条记录的最后一个单元格
// FileOutputStream out = new FileOutputStream(path); // 向d://test.xls中写数据
//
// 遍历集合数据,产生数据行
Iterator<T> it = dataset.iterator();
int index = sheet.getLastRowNum();
if(index==INDEX){
index=index-NUM;
}
while (it.hasNext()) {
index++;
row = sheet.createRow(index);
T t = (T) it.next();
// 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
Field[] fields = t.getClass().getDeclaredFields();
// for (short i = 0; i < fields.length; i++) {
for (short i = 0; i < titles.size(); i++) {
Cell cell = row.createCell(i);
String fieldName = titles.get(i);
// Field field = fields[i];
// String fieldName = field.getName();
String getMethodName = "get"
+ fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);
try {
Class tCls = t.getClass();
Method getMethod = tCls.getMethod(getMethodName,
new Class[] {});
Object value = getMethod.invoke(t, new Object[] {});
// 判断值的类型后进行强制类型转换
String textValue = null;
if (value instanceof Date) {
Date date = (Date) value;
textValue = sdf.format(date);
} else {
// 其它数据类型都当作字符串简单处理
textValue = value.toString();
}
// 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成
if (textValue != null) {
Pattern p = Pattern.compile("^//d+(//.//d+)?$");
matcher = p.matcher(textValue);
if (matcher.matches()) {
// 是数字当作double处理
cell.setCellValue(Double.parseDouble(textValue));
} else {
XSSFRichTextString richString = new XSSFRichTextString(
textValue);
XSSFFont font3 = (XSSFFont) wb.createFont();
font3.setColor(HSSFColor.BLUE.index);
richString.applyFont(font3);
cell.setCellValue(richString);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
out.flush();
wb.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public void exportExcelByEOS(DataObject[] exportObjects) {
// String path = fileName;
// XSSFWorkbook wb_old;
// XSSFWorkbook wb_old;
// SXSSFWorkbook wb;
// InputStream fs;
try {
// fs = new FileInputStream(path);
// wb = new XSSFWorkbook(new BufferedInputStream(fs));
// wb = new SXSSFWorkbook(100);
// newSXSSFWorkbook
Row row ;
// 获取第一行(excel中的行默认从0开始,所以这就是为什么,一个excel必须有字段列头),即,字段列头,便于赋值
System.out.println("第" + sheet.getLastRowNum()); // 分别得到最后一行的行号,和一条记录的最后一个单元格
// 遍历集合数据,产生数据行
int index = sheet.getLastRowNum();
if(index==INDEX){
index=index-NUM;
}
Cell cell ;
for (int i = 0; i < exportObjects.length; i++) {
content = exportObjects[i];
index++;
row = sheet.createRow(index);
for (int j = 0; j < titles.size(); j++) {
cell = row.createCell(j);
String fieldName = titles.get(j);
value = content.get(fieldName);
if(value == null){
continue;
}
try {
// 判断值的类型后进行强制类型转换
String textValue = null;
if (value instanceof Date) {
Date date = (Date) value;
textValue = sdf.format(date);
} else {
// 其它数据类型都当作字符串简单处理
if(value!= null){
textValue = value.toString();
}
}
// 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成
if (textValue != null) {
matcher = p.matcher(textValue);
if (matcher.matches()) {
// 是数字当作double处理
cell.setCellValue(Double.parseDouble(textValue));
} else {
// XSSFRichTe