簡體   English   中英

是否可以使用Java和Apache POI庫逐行寫入excel文件

[英]Is it possible to write into excel file line by line using java and Apache POI libraries

我正在嘗試逐行寫入我的excel文件。 代碼可以正常運行,沒有錯誤。 但是,當我嘗試打開excel文件時,出現一個彈出窗口,並說:“我們在excel文件中發現了一些內容問題。您是否希望我們嘗試盡可能多地恢復?”

通常,我在處理數據的循環之外使用FileOutputStream並使用.setCellValue(Value)。 但是當我在循環中使用它時,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");

}

}

只需刪除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(從末尾刪除True)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM