2016-01-06 32 views
0

我想在Excel中的Excel工作表中读取数据使用JXL库

我的情况我有以下数据如何读取基于在Excel中输入标签的数据: |网址|状态| www1.com 2016年1月6日 www2.com 2016年1月6日 www3.com

我的情况是,我想获得的URL,其状态未设置,也需要在状态当前日期写领域。

回答

0
here is the working code:  

package projects; 

    import java.io.File; 
    import java.text.SimpleDateFormat; 
    import java.util.Date; 

    import jxl.Workbook; 
    import jxl.biff.CellFinder; 
    import jxl.write.Label; 
    import jxl.write.WritableSheet; 
    import jxl.write.WritableWorkbook; 

    public class Test { 

     public static void main(String[] args) 
     { 
      try 
      { 
       System.out.println("Starting Program: "); 

       File file = new File("/Users/Pankaj/Desktop/Test.xls"); 

       Workbook book = Workbook.getWorkbook(file); 
       WritableWorkbook copiedBook = Workbook.createWorkbook(file, book); 
       WritableSheet sheet = copiedBook.getSheet(0); 

       CellFinder cellFind = new CellFinder(sheet); 
       for(int row=1;row<sheet.getRows();row++) 
       { 
        int urlColumn = cellFind.findLabelCell("URL").getColumn(); 
        int statusColumn = cellFind.findLabelCell("Status").getColumn(); 

        String urlNoStatus = sheet.getCell(urlColumn, row).getContents(); 
        String noStatus = sheet.getCell(statusColumn, row).getContents(); 

        if(noStatus.isEmpty()) 
        { 
         System.out.println("URL WITHOUT STATUS: "+urlNoStatus); 

         Date date = new Date(); 
         SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
         String formattedDate = sdf.format(date); 

         Label status = new Label(statusColumn, row, formattedDate); 
         sheet.addCell(status); 
        } 
       } 


       copiedBook.write(); 
       copiedBook.close(); 
       book.close(); 

       System.out.println("Ending Program: "); 

      }catch(Exception e) 
      { 
       e.printStackTrace(); 
      } 

     } 

    }