public class ExportToExcelServiceImpl implements ExportToExcelService{
private HSSFWorkbook workbook;
private String[] tableNames;
public ExportToExcelServiceImpl(){
};
/**
*/
public HSSFWorkbook exportToExcel(String title, List values,String[] excelHeader) {
workbook = new HSSFWorkbook();
int tableNum = 0;
if (values.size() % 50000 == 0) {
tableNum = values.size() / 50000 ;
} else {
tableNum = values.size() / 50000 +1;
}
for (int i = 1; i <= tableNum; i++) {
HSSFSheet sheet = workbook.createSheet(title + i );
HSSFRow row = sheet.createRow((int) 0);
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
if(excelHeader!=null){
for (int j = 0; j < excelHeader.length; j++) {
HSSFCell cell = row.createCell((short) j);
cell.setCellStyle(style);
cell.setCellValue(excelHeader[j]);
}
}else{
Field[] fieldNum = values.get(i).getClass().getDeclaredFields();
int serial=0;
for (int j = 0; j < fieldNum.length; j++) {
if(fieldNum[j].getName().equals("serialVersionUID")){
serial=1;
continue;
}
HSSFCell cell = row.createCell((short) (j - serial));
cell.setCellStyle(style);
cell.setCellValue(fieldNum[j].getName());
}
}
int pageSize=0;
if(i==tableNum){
pageSize=values.size();
}else{
pageSize=i*50000;
}
for (int j =(i -1) * 50000; j < pageSize; j++) {
row = sheet.createRow(j-(i-1)*50000+1);
Field[] fields = values.get(j).getClass().getDeclaredFields();
int serial=0;
for (int k = 0; k < fields.length; k++) {
Field field = fields[k];
String fieldName = field.getName();
String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
if(fieldName.equals("serialVersionUID")){
serial=1;
continue;
}
HSSFCell cell = row.createCell((short)(k-serial));
cell.setCellStyle(style);
try {
Class clazz = values.get(j).getClass();
Method getMethod = clazz.getMethod(getMethodName, new Class[]{});
Object value = getMethod.invoke(values.get(j), new Object[]{});
if (value instanceof Integer) {
int intValue = (Integer) value;
cell.setCellValue(intValue);
} else if (value instanceof Float) {
float fValue = (Float) value;
cell.setCellValue(fValue);
} else if (value instanceof Double) {
double dValue = (Double) value;
cell.setCellValue(dValue);
} else if (value instanceof Long) {
long longValue = (Long) value;
cell.setCellValue(longValue);
} else if (value instanceof Date) {
Date date = (Date) value;
cell.setCellValue(date);
} else if(value instanceof String){
String valueString=(String)value;
cell.setCellValue(valueString);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
return workbook;
}
public static void main(String[] args){
String [] params={"xuehao","xingming","sex","gun","6","4","43","4"};
List<MbChlRspMapDO> list=new ArrayList<MbChlRspMapDO>();
for(int i=0; i<50050; i++){
MbChlRspMapDO mb=new MbChlRspMapDO();
mb.setChannelId("1234"+i);
mb.setChlRspCode("2235"+i);
mb.setChlRspMsg("woshihaoren"+i);
mb.setCreator("zhangbo"+i);
mb.setId("323"+i);
mb.setStatus("4340"+i);
mb.setUpdator("dao"+i);
mb.setFlag("Y"+i);
list.add(mb);
}
HSSFWorkbook workbook=new ExportToExcelServiceImpl().exportToExcel("woshihaoren", list,null);
OutputStream out=null;
try{
out=new FileOutputStream("D:\\a.xls");
workbook.write(out);
}catch (Exception e){
e.printStackTrace();
}
}
}
< dependency >
< groupId > org.apache.poi</ groupId >
< artifactId > poi</ artifactId >
< version > 3.10-FINAL</ version >
</ dependency >
< dependency >
< groupId > org.apache.poi</ groupId >
< artifactId > poi-ooxml</ artifactId >
< version > 3.10-FINAL</ version >
</ dependency >
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=customer.xls");
OutputStream ouputStream = response.getOutputStream();
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();