私は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");
}
}
バージョンをしていますあなたは使っていますか? – sirandy
私はPOIを使用しています3.15 – Deep
なぜすべての行が変更された後にファイルを書き込もうとしていますか? – Gagravarr