java实现可变表头的表格_如何生成可变表头的excel

importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.OutputStream;importjava.lang.reflect.InvocationTargetException;importjava.lang.reflect.Method;importjava.text.SimpleDateFormat;importjava.util.ArrayList;importjava.util.Calendar;importjava.util.List;importjava.util.UUID;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFCellStyle;importorg.apache.poi.hssf.usermodel.HSSFRow;importorg.apache.poi.hssf.usermodel.HSSFSheet;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.hssf.util.Region;importorg.apache.poi.ss.usermodel.CellStyle;importcom.zcr.until.GetFilePlace;importcom.zcr.until.User;/*** 生成excel

*@authorzcr

**/

public classGenerateExcel

{/*** 通过关键字查询properties文件相应文件的存储位置,根据表头顺序将数据保存到相应文件路径的xls文件中, 文件的命名规则是时间戳加一串全球唯一编码

*@paramfileDir //查找文件存储根目录

*@paramhead //表头

*@paramlist //数据

*@return//文件的保存路径及其名字的字符串*/

public String generateExcels(String fileDir,String [] head,Listlist)

{//获得存储的路径//String savePath = new GetFilePlace().getFileDirFromProperties(key);//文件存储名字

String saveFileName = "";

SimpleDateFormat format= new SimpleDateFormat("yyyyMMddHHmmssSS");

saveFileName+=format.format(Calendar.getInstance().getTime());

UUID uuid= UUID.randomUUID(); //全球唯一编码

saveFileName+= "-" +uuid.toString();

HSSFWorkbook workbook= newHSSFWorkbook();

HSSFSheet sheet=workbook.createSheet();

workbook.setSheetName(0,"APP数据"); //设置表格工作簿名称

HSSFCellStyle cellStyle =workbook.createCellStyle();

cellStyle.setAlignment(CellStyle.ALIGN_CENTER);

cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

HSSFRow titleRow= sheet.createRow(0);

sheet.addMergedRegion(new Region(0,(short)0,0,(short)(head.length-1)));

HSSFCell titleCell= titleRow.createCell(0);

titleCell.setCellValue("AAP数据____ ");

titleCell.setCellStyle(cellStyle);

HSSFRow row1= sheet.createRow(1);//设置表头

for(int i = 0 ; i < head.length ; i++)

{

HSSFCell cell=row1.createCell(i);

cell.setCellValue(head[i]);//设置值

cell.setCellStyle(cellStyle);//设置样式

}if(null != list && list.size() > 0)

{int size =list.size();

Class classType= list.get(0).getClass();for(int i = 0,rowNum=2 ; i < size ; i ++,rowNum++)

{

HSSFRow rows=sheet.createRow(rowNum);

T t=list.get(i);//添加数据行

for(int j = 0 ; j < head.length ; j++)

{//获得首字母

String firstLetter = head[j].substring(0,1).toUpperCase();//获得get方法,getName,getAge等

String getMethodName = "get" + firstLetter + head[j].substring(1);

Method method;try{//通过反射获得相应的get方法,用于获得相应的属性值

method = classType.getMethod(getMethodName, newClass[]{});

HSSFCell dataCell=rows.createCell(j);try{

System.out.print(getMethodName+":" + method.invoke(t, new Class[]{}) +",");

dataCell.setCellValue(method.invoke(t,newClass[]{}).toString());

}catch(IllegalArgumentException e)

{

e.printStackTrace();

}catch(IllegalAccessException e)

{

e.printStackTrace();

}catch(InvocationTargetException e)

{

e.printStackTrace();

}//设置值

dataCell.setCellStyle(cellStyle);//设置样式

}catch(SecurityException e)

{

e.printStackTrace();

}catch(NoSuchMethodException e)

{

e.printStackTrace();

}

}

System.out.println();

}

}else{

System.out.println("没有数据");

}//获得文件存储路径//String fileDir = new GetFilePlace().getFileDirFromProperties(key);

saveFileName += ".xls";

String saveFilePathAndName= fileDir + File.separator +saveFileName;

OutputStream out= null;try{

out= newFileOutputStream(saveFilePathAndName);try{

workbook.write(out);//保存文件

}catch(IOException e)

{//TODO Auto-generated catch block

e.printStackTrace();

}

}catch(FileNotFoundException e)

{//TODO Auto-generated catch block

e.printStackTrace();

}finally{try{

out.close();

}catch(IOException e)

{//TODO Auto-generated catch block

e.printStackTrace();

}

}returnsaveFilePathAndName;

}/*** 提供外界调用的接口,生成以head为表头,list为数据的excel

*@paramhead //数据表头

*@paramlist //数据

*@return//excel所在的路径*/

public String generateExcel(String [] head,Listlist)

{final String FilePath = "filePath";

String saveFilePathAndName= "";//获得存储的根目录

String savePath = newGetFilePlace().getFileDirFromProperties(FilePath);//获得当天存储的路径

String realSavePath = newGenerateFold().getFold(savePath);//生成excel并将存储的路径返回(包含文件名)

saveFilePathAndName =generateExcels(realSavePath, head, list);returnsaveFilePathAndName;

}public static voidmain(String[] args)

{

String [] head= {"name","sex","adress","height","age","jj"};

List list = new ArrayList();

User user1= new User("zhangsan",1,1.1f,"北京","男","AA");

User user2= new User("lisi",22222,3.2f,"上海","女","BB");

list.add(user1);

list.add(user2);

System.out.println(newGenerateExcel().generateExcel(head,list));//System.out.println(new GenerateExcel().generateExcels("E:\\appData\\20151104",head,list));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值