java bean生成_JavaBean生成excel

必须使用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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值