package com.yuancredit.gathering.feedback.util;
import ****.TaskFeedBackExcel;// 实体类
import org.apache.poi.hssf.usermodel.HSSFRichTextString;// poi 帮助类
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.jeecgframework.poi.exception.excel.ExcelExportException;
import org.jeecgframework.poi.exception.excel.enums.ExcelExportEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileOutputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
public class ExportExcelUtil {
private static Logger logger = LoggerFactory.getLogger(ExportExcelUtil.class);
public static void main(String[] args) throws Exception{
String sheetName="testsheet";
List<Title> titles = new ArrayList<>();
Title t1 = new Title("名称", "cusName");
Title t2 = new Title("电话", "cusMobile");
Title t3 = new Title("状态", "cusStatus");
titles.add(t1);
titles.add(t2);
titles.add(t3);
List<TaskFeedBackExcel> list = new ArrayList<>();
for (int i=0;i<3;i++){
TaskFeedBackExcel t = new TaskFeedBackExcel();
t.setCusName("Nmae"+(i+1));
t.setCusMobile("molibe"+(i+1));
t.setOverdueStatus("status"+(i+1));
list.add(t);
}
Workbook wb = exportExcel(sheetName, titles , list, TaskFeedBackExcel.class, ExcelType.HSSF);
FileOutputStream out = new FileOutputStream("E:/test.xls");
try {
wb.write(out);
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(out != null) out.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
public static Workbook exportExcel(String sheetName, List<Title> titles, Collection<?> dataSet, Class<?> pojoClass, ExcelType type) {
Object workbook;
if (ExcelType.HSSF.equals(type)) {
workbook = new HSSFWorkbook();
} else if (dataSet.size() < 10000) {
workbook = new XSSFWorkbook();
} else {
workbook = new SXSSFWorkbook();
}
createSheet((Workbook)workbook, sheetName, titles, dataSet, pojoClass, type);
return (Workbook)workbook;
}
public static void createSheet(Workbook workbook, String sheetName, List<Title> titles, Collection<?> dataSet, Class<?> pojoClass, ExcelType type) {
if (workbook != null && sheetName != null && titles != null && dataSet != null) {
try {
// sheet
Sheet sheet = null;
try {
sheet = workbook.createSheet(sheetName);
} catch (Exception var7) {
sheet = workbook.createSheet();
}
int rowCount = 0;
Row rowTitle = sheet.createRow(rowCount);
// title
for (int index=0; index < titles.size(); index++){
Title t = titles.get(index);
Cell cell = rowTitle.createCell(index);
RichTextString text = null;
if (type.equals(ExcelType.HSSF)) {
text = new HSSFRichTextString(t.name);
} else {
text = new XSSFRichTextString(t.name);
}
cell.setCellValue(text);
}
rowCount++;
// 列表
Method method = null;
Iterator<?> its = dataSet.iterator();
while(its.hasNext()) {
Object object = its.next();
Row rowData = sheet.createRow(rowCount);
for (int index = 0; index < titles.size(); index++){
Title t = titles.get(index);
Cell cell = rowData.createCell(index);
Object value = null;
try {
method = pojoClass.getMethod("get" + toUpperCase4Index(t.key));
value = method.invoke(object);
}catch (Exception e){
logger.error(e.getMessage());
e.printStackTrace();
cell.setCellValue("");
continue;
}
String str = objectToString(value);
RichTextString text = null;
if (type.equals(ExcelType.HSSF)) {
text = new HSSFRichTextString(str);
} else {
text = new XSSFRichTextString(str);
}
cell.setCellValue(text);
}
}
} catch (Exception var9) {
throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, var9.getCause());
}
} else {
throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR);
}
}
public static String objectToString(Object object){
if(object != null){// 可根据对象具体类型做转换
object.toString();
}
return "";
}
public static String
toUpperCase4Index(String string) {
char[] methodName = string.toCharArray()
;
methodName[
0] =
toUpperCase(methodName[
0])
;
return String.
valueOf(methodName)
;
}
public static char
toUpperCase(
char chars) {
if (
97 <= chars && chars <=
122) { chars ^=
32
;
}
return chars
;
}
public static class Title{ String
name
;
String
key
;
public
Title(String name
, String key)
throws Exception {
if(name ==
null || name.length() ==
0){
throw new Exception(
"参数异常")
;
}
if(key ==
null || key.length() ==
0){
throw new Exception(
"参数异常")
;
}
this.
name = name
;
this.
key = key
;
} }}