我想写成线我的excel文件行写入excel文件行。代码运行良好,没有错误。但是当我尝试打开excel文件时,会出现一个弹出窗口并提示: “我们发现excel文件中的某些内容存在问题,您希望我们尝试尽可能恢复吗?”是否有可能通过线使用Java和Apache POI库
通常,我使用FileOutputStream中,其中我与数据操作和使用.setCellValue(价值)的环的外侧。但是当我在循环中使用它时,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");
}
}
哪个版本的POI的是你在用吗? – sirandy
我正在使用POI 3.15 – Deep
为什么你想在每次换行后写入文件? – Gagravarr