Java - 只写最后一个数据

时间:2013-12-19 04:16:12

标签: java excel fileoutputstream hssf

    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中更新了最后的数据。请告诉我哪里做错了。

编辑

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 个答案:

答案 0 :(得分: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);

因此,您始终会附加到原始文件,而不是更新文件。

为什么不打开它(并关闭它一次)?