2017-04-11 12 views
0

私はExcelファイルを1行ずつ書き込もうとしています。コードはエラーなく正常に実行されます。しかし、私がExcelファイルを開こうとすると、1つのポップアップが表示され、それが表示されます。 "Excelファイル内のいくつかのコンテンツに問題が見つかりました。できるだけ多くのファイルを回復しようと思いますか?"javaとApache POIライブラリを使用して1行ずつExcelファイルに書き込むことは可能ですか?

通常、私はデータで作業し、.setCellValue(Value)を使用するループ外でFileOutputStreamを使用します。しかし、ループの中で使用すると、Excelファイルは更新されません。下のコードでは、Excelを記述した後、Excelファイルを閉じて再オープンします。私は利用可能な解決策を試みたが、うまくいかなかった。 何か助けていただければ幸いです。以下

はコードです:

Package Excel; 

import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.util.Properties; 

import org.apache.poi.xssf.usermodel.XSSFCell;  
import org.apache.poi.xssf.usermodel.XSSFRow;  
import org.apache.poi.xssf.usermodel.XSSFSheet;  
import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
import org.openqa.selenium.WebDriver; 

public class testTool { 

static WebDriver driver; 
public static FileInputStream ACF; 
public static FileOutputStream fos; 

public static void main(String[] args) throws IOException { 

    Properties properties = new Properties(); 
    InputStream inputStream = new FileInputStream(System.getProperty("user.dir") + "/PACon_InputPath.properties"); 
    properties.load(inputStream); 

    int i = 1; 

    ACF = new FileInputStream(properties.getProperty("inputFilePath")); 
    XSSFWorkbook workBk = new XSSFWorkbook(ACF); 
    XSSFSheet Workable_Dump_Data = workBk.getSheet("Workable Dump Data"); 

    for (i=1;i<=5;i++) 
    { 

     System.out.println("\n````````````````````````````````````````````````````````````Row - " + i); 

     XSSFRow Rw = Workable_Dump_Data.getRow(i); 

     XSSFCell Account_Name = Rw.getCell(3); // Fetching Account name from the excel 
     String accountName = Account_Name.getStringCellValue(); 
     System.out.println("Account Name : " + accountName); 

     XSSFCell Physical_address = Rw.getCell(3); // Fetching Physical Address from the excel 
     String physicalAddress = Physical_address.getStringCellValue(); 
     System.out.println("Physical Address : " + physicalAddress); 

     boolean flag1 = false; 
     if (accountName.equals("") || physicalAddress.equals("")) 
     { 

      XSSFCell str13 = Rw.getCell(15); 
      str13.setCellValue("Empty Fields"); 
      fos = new FileOutputStream(properties.getProperty("inputFilePath"), true); 
       workBk.write(fos); // writing to Excel and continue 
       fos.close(); 
       workBk = new XSSFWorkbook(new FileInputStream(properties.getProperty("inputFilePath"))); 
       continue; 
     } 
     else 
     { 
      XSSFCell str13 = Rw.getCell(15); 
      str13.setCellValue("Fields Are Available"); 
      fos = new FileOutputStream(properties.getProperty("inputFilePath"), true); 
       workBk.write(fos); // writing to Excel and continue 
       fos.close(); 
       workBk = new XSSFWorkbook(new FileInputStream(properties.getProperty("inputFilePath"))); 
       continue; 
     } 

    } 

    System.out.println("Successfully writen in the excel sheet"); 

} 

} 
+1

バージョンをしていますあなたは使っていますか? – sirandy

+0

私はPOIを使用しています3.15 – Deep

+1

なぜすべての行が変更された後にファイルを書き込もうとしていますか? – Gagravarr

答えて

0

私は私の要件のために周りの仕事を得ました。 FileOutputStreamをループの中に置くことができます。私たちが念頭に置く必要があることの1つは、すべての反復でファイルを書き込んだ後にファイルを保存して開くことです。

手順は次のようになりますように:細胞(.setCellValue())Excelに 3.書き込み(workBk.write(FOS))へ 1. Excelファイル(のFileInputStream) 2.設定値 4.保存第一

:Excelファイルは、(fos.close()) 5. Excelを開き、再び

....私は私の以前のコードで行われた変更を 6.インクリメントループ (以下のコードを参照)、リピート(最後から真実を取り除いた)

fos = new FileOutputStream(properties.getProperty("inputFilePath")); 

第二(継続の前に追加しました;)

Workable_Dump_Data = workBk.getSheet("Workable Dump Data"); 

は、以下の完全なコードを参照してください:ヘルプ@Gagravarrと@sirandyため

import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Properties; 

import org.apache.poi.xssf.usermodel.XSSFCell; 
import org.apache.poi.xssf.usermodel.XSSFRow; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

public class testTool { 

public static FileOutputStream fos; 
public static FileInputStream ACF; 

public static void main(String[] args) throws IOException { 

    Properties properties = new Properties(); 
    InputStream inputStream = new FileInputStream(System.getProperty("user.dir") + "/PACon_InputPath.properties"); 
    properties.load(inputStream); 

    try { 
     ACF = new FileInputStream(properties.getProperty("inputFilePath")); 
     XSSFWorkbook workBk = new XSSFWorkbook(ACF); 
     XSSFSheet Workable_Dump_Data = workBk.getSheet("Workable Dump Data"); 

     int i = 1; 

     for (i = 1; i <= 10; i++) { 

      System.out.println("\n````````````````````````````````````````````````````````````Row - " + i); 

      XSSFRow Rw = Workable_Dump_Data.getRow(i); 

      /*if (i == 6) // test if it update excel, if tool stops in between 
      { 
       String accountTest = ""; 
       XSSFCell Account_Name = Rw.getCell(22); 
       accountTest = Account_Name.getStringCellValue(); 
      }*/ 

      XSSFCell Account_Name = Rw.getCell(3); // Fetching Account name from the excel 
      String accountName = Account_Name.getStringCellValue(); 
      System.out.println("Account Name : " + accountName); 

      XSSFCell Physical_address = Rw.getCell(3); // Fetching Physical Address from the excel 
      String physicalAddress = Physical_address.getStringCellValue(); 
      System.out.println("Physical Address : " + physicalAddress); 

      boolean flag1 = false; 
      if (accountName.equals("") || physicalAddress.equals("")) 
      { 

       XSSFCell str13 = Rw.getCell(15); 
       str13.setCellValue("Empty Fields"); 
       fos = new FileOutputStream(properties.getProperty("inputFilePath")); 
        workBk.write(fos); // writing to Excel and continue 
        fos.close(); 
        workBk = new XSSFWorkbook(new FileInputStream(properties.getProperty("inputFilePath"))); 
        Workable_Dump_Data = workBk.getSheet("Workable Dump Data"); 
        continue; 
      } 
      else 
      { 
       XSSFCell str13 = Rw.getCell(15); 
       str13.setCellValue("Fields Are Available" + i); 
       fos = new FileOutputStream(properties.getProperty("inputFilePath")); 
        workBk.write(fos); // writing to Excel and continue 
        fos.close(); 
        workBk = new XSSFWorkbook(new FileInputStream(properties.getProperty("inputFilePath"))); 
        Workable_Dump_Data = workBk.getSheet("Workable Dump Data"); 
        continue; 
      } 

     } 

    } catch (Exception e) { 
     //Do something better with the Exception 
     e.printStackTrace(); 
    } 
    finally{ 
     fos.close(); 
     System.out.println("Successfully writen in the excel sheet"); 
    } 
} 

} 

感謝:) POIの

1

ただのFileOutputStreamコンストラクタで真を奪います。

はまたここにあなたのコードがリファクタリング少しです:

import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Properties; 

import org.apache.poi.xssf.usermodel.XSSFCell; 
import org.apache.poi.xssf.usermodel.XSSFRow; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

public class testTool { 

    public static FileInputStream ACF; 
    public static FileOutputStream fos; 

    public static void main(String[] args) throws IOException { 

     Properties properties = new Properties(); 
     InputStream inputStream = new FileInputStream(System.getProperty("user.dir") + "/PACon_InputPath.properties"); 
     properties.load(inputStream); 

     try { 
      ACF = new FileInputStream(properties.getProperty("inputFilePath")); 
      XSSFWorkbook workBk = new XSSFWorkbook(ACF); 
      XSSFSheet Workable_Dump_Data = workBk.getSheet("Workable Dump Data"); 

      int i = 1; 

      for (i = 1; i <= 5; i++) { 

       System.out.println("\n````````````````````````````````````````````````````````````Row - " + i); 

       XSSFRow Rw = Workable_Dump_Data.getRow(i); 

       XSSFCell Account_Name = Rw.getCell(3); // Fetching Account name from the excel 
       String accountName = Account_Name.getStringCellValue(); 
       System.out.println("Account Name : " + accountName); 

       XSSFCell Physical_address = Rw.getCell(3); // Fetching Physical Address from the excel 
       String physicalAddress = Physical_address.getStringCellValue(); 
       System.out.println("Physical Address : " + physicalAddress); 

       XSSFCell str13 = Rw.getCell(15); 
       boolean flag1 = false; 
       if (accountName.equals("") || physicalAddress.equals("")) { 
        str13.setCellValue("Empty Fields"); 
       } else { 
        str13.setCellValue("Fields Are Available"); 
       } 
      } 
      FileOutputStream fos = new FileOutputStream(properties.getProperty("inputFilePath")); 
      workBk.write(fos); // writing to Excel and continue 
      fos.close(); 
      ACF.close(); 
     } catch (Exception e) { 
      //Do something better with the Exception 
      e.printStackTrace(); 
     } 
     finally{ 
      System.out.println("Successfully writen in the excel sheet"); 
     } 
    } 

} 
+0

返信のために@ sirandyに感謝しますが、それでも私の問題は解決しません。コード内のFileOutputStreamは、** forループ**の外側にあります。つまり、ループが終了した後、各繰り返しではなく、最後にExcelに書き込むことを意味します。 – Deep

+0

@Dep通常、ファイルを変更するまでファイルを書き出すべきではないので、このコードは正しいアプローチのようです。 – Gagravarr

+0

そうです。実際、私はそれに約1000レコードの大きなエクセルファイルを作成しています。この問題は、ツールを実行してエラーが発生した場合(行991など)、取得されたすべてのデータが失われます。だから私は、同じレコードセットに対してツールを再度実行する必要があります。それは私が各繰り返しでExcelファイルを書き込んで保存する方法を必要とする方法です。これが理にかなってほしい.. .. – Deep

関連する問題