是否有可能使用java和Apache POI库逐行写入excel文件

我正在尝试逐行写入我的excel文件。 代码将运行正常,没有错误。 但是当我尝试打开excel文件时,出现一个popup窗口,并说:“我们发现excel文件中的某些内容存在问题,您希望我们尽可能地恢复吗?

通常情况下,我使用FileOutputStream之外的工作与数据并使用.setCellValue(值)的循环。 但是当我在循环中使用它时,excel文件不会被更新。 在下面的代码中,写完excel后,我正在closures并重新打开excel文件。 我尝试了可用的解决scheme,但没有工作。 任何帮助表示赞赏。

下面是代码:

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"); } } 

只需在FileOutputStream构造函数中取出true即可。

这里也是你的代码有点重构:

 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"); } } } 

我为我的要求得到了一个工作。 我们可以把FileOutputStream放到循环中,我们需要记住的一件事就是在每次迭代写入之后保存并打开excel文件。

步骤如下:1.打开excel文件(FileInputStream)2.将值设置为单元格(.setCellValue())3.写入excel(workBk.write(fos))4.保存excel文件(fos.close() )5.再次打开Excel(见下面的代码)6.递增循环和重复….

我在以前的代码中所做的更改:

1st(从最后删除)

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

第二(在继续之前添加;)

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

请参阅以下完整代码:

 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"); } } } 

感谢您的帮助@Gagravarr和@sirandy 🙂