欢迎来到天天文库
浏览记录
ID:12122103
大小:42.00 KB
页数:4页
时间:2018-07-15
《java导出excel总结》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、写Excel的方法类为JxlExcelWriter:importjxl.Workbook;importjxl.format.UnderlineStyle;importjxl.format.Colour;importjxl.write.*;importjxl.write.biff.RowsExceededException;importjava.io.IOException;importjava.io.OutputStream;importjava.util.Date;importjava.util.List;publicclassJxlExcelWriter{ publicvoi
2、dwriteExcel(OutputStreamout,Listdatas,String[]title){ if(datas==null){ thrownewIllegalArgumentException("写excel流需要List参数!"); } try{ WritableWorkbookworkbook=Workbook.createWorkbook(out); WritableSheetws=workbook.createSheet("sheet1",0);
3、 introwNum=1; //要写的行 if(title!=null){ putDescription(ws); putTitle(ws,1,title);//压入标题 Object[]cells=(Object[])datas.get(0); putRow(ws,rowNum,cells); //压一行到sheet } intdataNum=2;//要写的数据行 for(inti=1;i4、tas.size();i++,dataNum++){//把数据写进sheet Object[]cells=(Object[])datas.get(i); putDatas(ws,dataNum,cells);//压入一行数据到sheet } putTime(ws);//压入时间 workbook.write(); workbook.close(); //一定要关闭,否则没有保存Excel }catch(RowsExceededExcepti5、one){ System.out.println("jxlwriteRowsExceededException:"+e.getMessage()); }catch(WriteExceptione){ System.out.println("jxlwriteWriteException:"+e.getMessage()); }catch(IOExceptione){ System.out.println("jxlwritefilei/oexception!,causeby:"+e.getMessage()); 6、 } } privatevoidputTime(WritableSheetws)throwsRowsExceededException,WriteException{ DateFormatdf=newDateFormat("yyyy-MM-ddhh:mm:ss"); WritableCellFormatwcfDF=newWritableCellFormat(df); DateTimelabelDT=newDateTime(4,10,newDate(),wcfDF); ws.addCell(labelDT); } 7、 privatevoidputDescription(WritableSheetws)throwsRowsExceededException,WriteException{ WritableFontwfc=newWritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,
4、tas.size();i++,dataNum++){//把数据写进sheet Object[]cells=(Object[])datas.get(i); putDatas(ws,dataNum,cells);//压入一行数据到sheet } putTime(ws);//压入时间 workbook.write(); workbook.close(); //一定要关闭,否则没有保存Excel }catch(RowsExceededExcepti
5、one){ System.out.println("jxlwriteRowsExceededException:"+e.getMessage()); }catch(WriteExceptione){ System.out.println("jxlwriteWriteException:"+e.getMessage()); }catch(IOExceptione){ System.out.println("jxlwritefilei/oexception!,causeby:"+e.getMessage());
6、 } } privatevoidputTime(WritableSheetws)throwsRowsExceededException,WriteException{ DateFormatdf=newDateFormat("yyyy-MM-ddhh:mm:ss"); WritableCellFormatwcfDF=newWritableCellFormat(df); DateTimelabelDT=newDateTime(4,10,newDate(),wcfDF); ws.addCell(labelDT); }
7、 privatevoidputDescription(WritableSheetws)throwsRowsExceededException,WriteException{ WritableFontwfc=newWritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,
此文档下载收益归作者所有