資源描述:
《JXL設(shè)置字體、顏色、背景等》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、使用JXLJExcelApi生成Excel文檔,并且設(shè)置字體、顏色、背景等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{???//創(chuàng)建一個(gè)Excel文檔???WritableWorkbookworkbook=Workbook.createWorkbook(file);??//創(chuàng)建一個(gè)Sheet???WritableSheetsheet=workbook.createSheet("Report-1",0);???//是否顯示網(wǎng)格???sheet.getSettings().setShowGridLines(true);??//設(shè)置列寬???sheet.getSettings().setDefaultColumnWidth(9);??//設(shè)置行高???s
4、heet.getSettings().setDefaultRowHeight(500);??????Labellabel=newLabel(1,1,"ThisisaLable");???sheet.addCell(label);?????//設(shè)置字體???WritableFontfont=newWritableFont(WritableFont.ARIAL,20,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.RED);???WritableCellFormatcFormat=newW
5、ritableCellFormat(font);??//設(shè)置背景色???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)");??????//公式暫未成功,本來準(zhǔn)備計(jì)算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);}