JExcelAPI修改Excel的实例

package com.jExcelApi.test;  

    

 import java.io.File;  

 import java.io.IOException;  

    

 import jxl.Workbook;  

 import jxl.read.biff.BiffException;  

 import jxl.write.Label;  

 import jxl.write.Number;  

 import jxl.write.WritableSheet;  

 import jxl.write.WritableWorkbook;  

 import jxl.write.WriteException;  

    

     

    

 /**  

  * JExcelApi修改Excel的内容  

  * @author kanjingcai  

  *   注:必须加JexcelApi的jar执行  

  */ 

    

 public class UpdateExcel {  

    

  public static void main(String args[]){  

     

   try {  

    //获得创建名为Myfile.xls文件  

    Workbook workBook =Workbook.getWorkbook(new File("D:\\myFile.xls"));  

       

    

    //拷贝一个副本,并指定到原文件  

    WritableWorkbook copy1 =Workbook.createWorkbook(new File("D:\\myFile.xls"),workBook);  

       

    //拷贝一个副本名为新的output.xls  

   //  WritableWorkbook copy2 =Workbook.createWorkbook(new File("D:\\output.xls"),workBook);  

       

    //或者直接修改D:\\myFile.xls文件  

    WritableSheet sheet1 = copy1.getSheet(0);  

       

    //新的excel文件,拷贝第一页工作表到output.xls文件中,(0)代表第一页  

   // WritableSheet sheet2 =copy2.getSheet(0);  

       

    //获得第0行,第二列数据(0,2)  

   //   WritableCell cell1 =sheet1.getWritableCell(0,2);  

         

    //新的excel文件,获得第0行,第二列数据(0,2)  

   // WritableCell cell2 =sheet2.getWritableCell(0,2);  

    

      Label label =new Label(0, 2, "新的纪录");  

      sheet1.addCell(label);  

         

      Number number =new Number(3,4,3.141591);  

         

      sheet1.addCell(number);  

         

    //修改获得第0行,第二列数据(0,2)的内容  

      //获得单元格的类型  

    /*  

     if(cell1.getType() ==CellType.LABEL){  

        

     Label l =(Label)cell1;  

        

     //修改的内容  

     l.setString("修改MyFile数据单元格");  

    } */ 

       

    //修改数据,必须关闭文件  

    copy1.write();  

    copy1.close();  

       

   // copy2.write();  

   // copy2.close();  

       

   } catch (BiffException e) {  

    // TODO Auto-generated catch block  

    e.printStackTrace();  

   } catch (IOException e) {  

    // TODO Auto-generated catch block  

    e.printStackTrace();  

   } catch (WriteException e) {  

    // TODO Auto-generated catch block  

    e.printStackTrace();  

   }  

    

  }  

     

 } 

 

你可能感兴趣的:(Excel,jexcelapi)