必须使用2007以上版本 .xlsx文件名
poi包下载地址
http://poi.apache.org/download.html
package com.xls;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class DealxlsImpl {
/**@author johngf
* 传入一个list,将其写入指定的excel文件*/
public boolean generateXls(List> objs,File file)
{
if(objs.size()<=0)
{
return false;
}else if(!file.exists())
{
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(file.exists())
{
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//Create Blank workbook
Class> clazz = objs.get(0).getClass();
XSSFWorkbook workbook = new XSSFWorkbook();
int number = objs.size()/104857 + 1 ;
System.out.println("需要工作表的个数是"+number);
for(int i=0;i
{
XSSFSheet sheet = workbook.createSheet("sheet"+i);
Field[] fields = objs.get(0).getClass().getDeclaredFields();
System.out.println("列数是 "+fields.length);
// String[] titles = getTitle(objs.get(0));
//写标题行
Row row = sheet.createRow(0);
XSSFCellStyle style = workbook.createCellStyle();
style.setFillBackgroundColor(new XSSFColor(new byte[]{100,25,13}));
for(int j =0 ;j
{
Cell c = row.createCell(j);
c.setCellValue(fields[j].getName());
c.setCellStyle(style);
}
//标题已经写入
System.out.println("i="+i+"对象数"+ objs.size());
System.out.println(objs.size()-(104587 * i ));
for(int m = 104587 * i ;m < objs.size()-(104587 * i ); m++)
{
row = sheet.createRow( m + 1 );
System.out.println("已创建行 "+(m+1));
for(int n =0 ;n < fields.length;n++)
{
try {
PropertyDescriptor pd = new PropertyDescriptor(
fields[n].getName(), clazz);
Method method = pd.getReadMethod();
row.createCell(n).setCellValue(method.invoke(objs.get(m), null).toString());
} catch (IntrospectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//Create file system using specific name
FileOutputStream out;
try {
out = new FileOutputStream(file);
workbook.write(out);
workbook.close();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//write operation workbook using file out object
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public boolean parseXls(File file) {
// TODO Auto-generated method stub
return false;
}
}