jxl 读写Excel实例(設置cell format 格式)

时间:2025-02-16 20:08:01
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  •   
  •   
  • /** 
  •  * 
  •  * @author yrs 
  •  */  
  • public class TWrite {  
  •       
  •     public static void  write(WritableWorkbook wb) throws WriteException {  
  •         WritableSheet wSheet = ("sheet1"0);  
  •           
  •         //添加Label对象   
  •         Label labelC = new Label(00"Label cell");   
  •         (labelC);  
  •           
  •         //添加Number对象   
  •         Number numberC = new Number(013.1415926);   
  •         (numberC);   
  •           
  •         // 3.添加Boolean对象   
  •         Boolean booleanC = new Boolean(02true);   
  •         (booleanC);   
  •           
  •         // 4.添加DateTime对象   
  •         DateTime dateTimeC = new DateTime(03new ());   
  •         (dateTimeC);   
  •           
  •         // 添加带有字型Formatting的对象   
  •         WritableFont wFont = new WritableFont(   
  •             , 18, , true);   
  •         WritableCellFormat wCellFormatC = new WritableCellFormat(wFont);   
  •         Label labelCF = new Label(10"Label Cell", wCellFormatC);   
  •         (labelCF);   
  •           
  •          // 添加带有formatting的Number对象   
  •         NumberFormat nFormat = new NumberFormat("#.##");   
  •         WritableCellFormat wCellFormatN = new WritableCellFormat(nFormat);  
  •         Number numberNF = new Number(113.1415926, wCellFormatN);   
  •         (numberNF);   
  •           
  •         // 添加带有formatting的DateFormat对象   
  •         DateFormat dFormat = new DateFormat(   
  •           "yyyy MM dd hh:mm:ss");   
  •         WritableCellFormat wCellFormatD = new WritableCellFormat(dFormat);   
  •         DateTime dateTimeDF = new DateTime(13new (), wCellFormatD);   
  •         (dateTimeDF);  
  •           
  •     }  
  •     public static void main(String [] args) throws IOException, BiffException, WriteException  {  
  •         try {  
  •               
  •             //文件D://JEtest// 不存在,若存在数据将会丢失  
  •             File file = new File("D://JEtest//");  
  •             //创建一个新的可写工作簿  
  •             WritableWorkbook wb = (file);   
  •             (wb);  
  •               
  •             // 写入Exel工作表   
  •             ();  
  •             // 关闭Excel工作薄对象   
  •             ();              
  •         }catch (IOException | WriteException e) {  
  •             ("Exception:  " + e);  
  •             throw e;  
  •         }  
  •     }    
  • }