2017-04-11 49 views
0

我想写成线我的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"); 

} 

} 
+1

哪个版本的POI的是你在用吗? – sirandy

+0

我正在使用POI 3.15 – Deep

+1

为什么你想在每次换行后写入文件? – Gagravarr

回答

0

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

步骤将是这样的: 1.打开Excel文件(的FileInputStream) 2.设定值到细胞(.setCellValue()) 3.写入到Excel(workBk.write(FOS)) 4.保存Excel文件(fos.close())再次 5.打开Excel(请参见下面的代码) 6.增量环 并重复....

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

1 (从最后删除)

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

2日(由之前继续;)

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 :)

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

@Deep在修改之前,你通常不应该写出文件,所以这段代码看起来是正确的方法! – Gagravarr

+0

这是对的。其实我正在处理一个大型的excel文件,里面有大约1000条记录。当我运行工具并出现错误事件时(例如在第991行),所捕获的所有数据都丢失,问题就出现了。所以我将不得不再次运行该工具以获取同一组记录。这就是我需要的一种方式来写和保存每次迭代的Excel文件。希望这是有道理的..! – Deep