2013-12-19 54 views
0
int temp = 7; 
    File folder = new File("//Users//" + usr + "//Desktop//TNA//input1//"); 
    File[] listOfFiles = folder.listFiles(); 
    if (listOfFiles != null) { 
     for (int i = 0; i < listOfFiles.length; i++) { 

      if (listOfFiles[i].isFile() && (listOfFiles[i].getName()).endsWith(".pdf")) { 
       System.out.println(listOfFiles[i].getName()); 
       String fileName = "//Users//" + usr + "//Desktop//TNA//input1//" + listOfFiles[i].getName(); 
       try { 
        PdfReader reader = new PdfReader("//Users//gmuniandy//Desktop//TNA//input1//" + listOfFiles[i].getName()); 
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT2)); 
        AcroFields form = stamper.getAcroFields(); 
        String name = form.getField("Text1");//Check Box 1 
        System.out.println(name); 
        stamper.close(); 
        reader.close(); 
        FileInputStream file = new FileInputStream(new File("//Users//"+ usr +"//Desktop//TNA//input//FR-OPS-030 Master Training Plan_Rev4.xls")); 
        HSSFWorkbook workbook = new HSSFWorkbook(file); 
        HSSFSheet sheet = workbook.getSheet("Sheet1");// getSheetAt(0); 
        HSSFRow row = sheet.createRow((short) 0); 
        HSSFCellStyle style = workbook.createCellStyle(); 
        style.setFillForegroundColor(HSSFColor.DARK_BLUE.index); 
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
        HSSFRow row1 = sheet.createRow(temp); 
        HSSFCell name_c1 = row1.createCell(0); 
        name_c1.setCellValue(name); 
        name_c1.setCellStyle(style); 
        file.close(); 

        FileOutputStream outFile =new FileOutputStream(new File("//Users//"+ usr +"//Desktop//TNA//output//FR-OPS-030 Master Training Plan_Rev41w.xls")); 
        workbook.write(outFile); 
        outFile.close(); 
        temp++; 
       } catch (Exception ex) { 
       } 

      } 
     } 
    } 

我從PDF檢索值並寫入Excel。我設法寫在Excel,但只有最後的數據在Excel更新。請建議我哪裏做錯了。Java - 只寫excel最後的數據

編輯

if (listOfFiles[i].isFile() && (listOfFiles[i].getName()).endsWith(".pdf")) { 
        System.out.println(listOfFiles[i].getName()); 
        String fileName = "//Users//" + usr + "//Desktop//TNA//input1//" + listOfFiles[i].getName(); 
        FileInputStream file = new FileInputStream(new File("//Users//"+ usr +"//Desktop//TNA//input//FR-OPS-030 Master Training Plan_Rev4.xls")); 
         HSSFWorkbook workbook = new HSSFWorkbook(file); 
        try { 
         PdfReader reader = new PdfReader("//Users//gmuniandy//Desktop//TNA//input1//" + listOfFiles[i].getName()); 
         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT2)); 
         AcroFields form = stamper.getAcroFields(); 
         String name = form.getField("Text1");//Check Box 1 
         System.out.println(name); 
         stamper.close(); 
         reader.close(); 

         HSSFSheet sheet = workbook.getSheet("Sheet1");// getSheetAt(0); 
         HSSFRow row = sheet.createRow((short) 0); 
         HSSFCellStyle style = workbook.createCellStyle(); 
         style.setFillForegroundColor(HSSFColor.DARK_BLUE.index); 
         style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
         HSSFRow row1 = sheet.createRow(temp); 
         HSSFCell name_c1 = row1.createCell(0); 
         name_c1.setCellValue(name); 
         name_c1.setCellStyle(style); 
         file.close(); 


         temp++; 
        } catch (Exception ex) { 
        } 
        finally{ 
         FileOutputStream outFile =new FileOutputStream(new File("//Users//"+ usr +"//Desktop//TNA//output//FR-OPS-030 Master Training Plan_Rev41w.xls")); 
         workbook.write(outFile); 
         outFile.close();       
        } 

即使我想這一點,但它只是相同。

回答

1

輸入文件

FileInputStream file = new FileInputStream(new File("//Users//"+ usr +"//Desktop//TNA//input//FR-OPS-030 Master Training Plan_Rev4.xls")); 
HSSFWorkbook workbook = new HSSFWorkbook(file); 

是一個不同的文件輸出文件

FileOutputStream outFile =new FileOutputStream(new File("//Users//"+ usr +"//Desktop//TNA//output//FR-OPS-030 Master Training Plan_Rev41w.xls")); 
workbook.write(outFile); 

所以,你總是被追加到原始文件,而不是更新的文件。

爲什麼不打開它(關閉一次)?

相關問題