2016-03-29 21 views
0
File iwb = new File("D:\\f.xls"); 
     Workbook wb; 
     try 
     { 
     wb = Workbook.getWorkbook(iwb); 
     WritableWorkbook copy = Workbook.createWorkbook(new File("D:\\f.xls"),wb); 
     WritableSheet sheet = copy.getSheet(0); 

     jxl.write.Label label,label1,label2,label3; 
     int i=sheet.getRows(); 
     int j=sheet.getColumns(); 
      System.out.println(i+j); 
     label = new jxl.write.Label(0, 0, "N0."); 
     label1 = new jxl.write.Label(1, 0, "Name"); 
     label2 = new jxl.write.Label(4, 0, "PIN"); 
     label3 = new jxl.write.Label(6, 0, "Date"); 
     sheet.addCell(label); 
     sheet.addCell(label1); 
     sheet.addCell(label2); 
     sheet.addCell(label3); 

     copy.write(); 
     copy.close(); 
     } catch (IOException ex) { 
      Logger.getLogger(test1.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (WriteException ex) { 
      Logger.getLogger(test1.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (BiffException ex) { 
      Logger.getLogger(test1.class.getName()).log(Level.SEVERE, null, ex); 
     } 

回答

0

我建議使用Apache poi庫來做到這一點。 下面是一個示例代碼,用於從包含僱員數據的文本文件中生成excel,該文件包含標題,名稱和位置,使用apache poi xssf。 (這三個字段由文本文件中的分隔符「 - 」分隔)

try { 
     String DIR_PATH = "D:\\f.txt"; 
     FileReader read = new FileReader(new File(DIR_PATH)); 
     BufferedReader bfrRead = new BufferedReader(read); 
     String oneLine = null; 
     //Blank workbook 
     XSSFWorkbook workbook = new XSSFWorkbook(); 

     //Create a blank sheet 
     XSSFSheet sheet = workbook.createSheet("Employee Data"); 
     int rownum = 0; 
     Row row0 = sheet.createRow(rownum++); 
     Cell cell01 = row0.createCell(0); 
     cell01.setCellValue("Title"); 
     Cell cell02 = row0.createCell(1); 
     cell02.setCellValue("Name"); 
     Cell cell03 = row0.createCell(2); 
     cell03.setCellValue("Location"); 
     while((oneLine = bfrRead.readLine())!=null){ 
      if(oneLine.isEmpty()) 
       continue; 
      Row row = sheet.createRow(rownum++); 


      StringBuilder title = new StringBuilder(); 
      StringBuilder name= new StringBuilder(); 
      StringBuilder loc = new StringBuilder(); 
      int switch1 = 1; 

      //logic to parse from text file 
      for(int i = 0; i < oneLine.length(); i++){ 
       if(oneLine.charAt(i) != '-'){ 
        if(switch1 == 1) 
         title.append(oneLine.charAt(i)); 
        else if(switch1 == 2) 
         name.append(oneLine.charAt(i)); 
        else if (switch1 == 3) 
         loc.append(oneLine.charAt(i)); 
       } 
       else 
        if(oneLine.charAt(i+1) == '-'){ 
         i=i+1; 
         switch1++; 
        } 
        else 
         switch1++; 

      } 
      Cell cell1 = row.createCell(0); 
      cell1.setCellValue((String)title.toString()); 
      Cell cell2 = row.createCell(1); 
      cell2.setCellValue((String)name.toString()); 
      Cell cell3 = row.createCell(2); 
      cell3.setCellValue((String)loc.toString()); 
     } 
     //Write the workbook in file system 
     FileOutputStream out = new FileOutputStream(new File("D:\\f.xlsx")); 
     workbook.write(out); 
     out.close(); 
     bfrRead.close(); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
    }  
相關問題