资源描述:
《JXL设置字体、颜色、背景等》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、使用JXLJExcelApi生成Excel文档,并且设置字体、颜色、背景等2009-05-2011:38packagecom.test.excel;importjava.io.File;importjava.io.IOException;importjxl.Workbook;importjxl.format.Border;importjxl.format.Colour;importjxl.format.UnderlineStyle;importjxl.format.BorderLineStyle;importjxl.write.Formula;impor
2、tjxl.write.Label;importjxl.write.Number;importjxl.write.WritableCell;importjxl.write.WritableCellFormat;importjxl.write.WritableFont;importjxl.write.WritableSheet;importjxl.write.WritableWorkbook;importjxl.write.WriteException;publicclassTestExcel{publicstaticvoidwriteExcel(Filef
3、ile){ try{ //创建一个Excel文档 WritableWorkbookworkbook=Workbook.createWorkbook(file); //创建一个Sheet WritableSheetsheet=workbook.createSheet("Report-1",0); //是否显示网格 sheet.getSettings().setShowGridLines(true); //设置列宽 sheet.getSettings().setDefaultColumnWidth(9); //设置行高 s
4、heet.getSettings().setDefaultRowHeight(500); Labellabel=newLabel(1,1,"ThisisaLable"); sheet.addCell(label); //设置字体 WritableFontfont=newWritableFont(WritableFont.ARIAL,20,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.RED); WritableCellFormatcFormat=newW
5、ritableCellFormat(font); //设置背景色 cFormat.setBackground(Colour.LIGHT_BLUE); Labellabel2=newLabel(5,13,"hellonewLabel",cFormat); sheet.addCell(label2); WritableSheetsheet2=workbook.createSheet("report-2",1); WritableCellFormatnewFormat=newWritableCellFormat(); newFo
6、rmat.setBorder(Border.ALL,BorderLineStyle.DOUBLE); Labellabel3=newLabel(3,6,"border",newFormat); sheet.addCell(label3); Formulal3=newFormula(10,10,"=SUM(C4:C5)"); //公式暂未成功,本来准备计算l1与l2的和,并显示到l3,未成功 //WritableCellcell=l3.copyTo(2,5); Numberl1=newNumber(2,3,3); N
7、umberl2=newNumber(2,4,4); sheet.addCell(l1); sheet.addCell(l2); //sheet.addCell(cell); workbook.write(); workbook.close(); }catch(IOExceptione){ e.printStackTrace(); }catch(WriteExceptione){ e.printStackTrace(); }}publicstaticvoidmain(Stringargs[]){ Filefile=new
8、File("d:/test.xls"); writeExcel(file);}