Writing to an excel using Apache POI corrupts the excel file

Prabodh Ghosh

I am trying to write to excel using Apache POI. The code (below) executes fine, but when I am trying to open the excel, it is showing that the data inside the excel has been corrupted and it cannot be opened.

Excel version : Microsoft Office Excel 2007 and Microsoft Office Excel 2003 (tried both)

Apache POI Version: 3.6

import java.io.FileOutputStream;
import java.io.IOException;

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 WriteExcel{

    public String FilePath;
    XSSFWorkbook wb= null;
    XSSFSheet ws= null;
    XSSFRow xr=null;
    XSSFCell xc=null;
    FileOutputStream fout = null;

    public WriteExcel(String FilePath) throws IOException

    {
        this.FilePath=FilePath;
        fout=new FileOutputStream(FilePath);
        wb=new XSSFWorkbook();
        wb.write(fout);

    }

    //Write to a specific Cell

    public void writeToCell(String SheetName, int RowNum, int ColNum, String Data) throws IOException
    {
        ws=wb.createSheet(SheetName);
        xr=ws.createRow(RowNum);
        xc=xr.createCell(ColNum);
        xc.setCellValue(Data);
        fout.close();
    }


    public static void main(String[] args) throws IOException {

        WriteExcel WE= new WriteExcel("E://Learning//Learning//SoapUI//TestFiles//Write.xls");
        WE.writeToCell("Sheet1", 2, 2, "Pi");
        System.out.println("Written");

    }


} 

Image1; Image2; Image3

I believe the the code is fine because every time I am executing the code, the "Date Modified" shows the latest time stamp; so I suspect there might be something with versioning of either Microsoft Office (Excel) or Apache POI.

Could you please help?

ares

You need to write your excel to disk after you create the sheets, rows and cells.

public WriteExcel(String FilePath) throws IOException

{
    this.FilePath=FilePath;
}

//Write to a specific Cell

public void writeToCell(String SheetName, int RowNum, int ColNum, String Data) throws IOException
{
    fout=new FileOutputStream(FilePath);
    wb=new XSSFWorkbook();
    ws=wb.createSheet(SheetName);
    xr=ws.createRow(RowNum);
    xc=xr.createCell(ColNum);
    xc.setCellValue(Data);
    wb.write(fout);
    fout.close();
} 

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Cannot read from Excel file using apache poi

分類Dev

Read from excel file .xlsx using java Apache POI 3.9 Eclipse

分類Dev

Apache POI jar is not able to write a special character in excel file

分類Dev

Accessing Excel Sheets in Java using Apache POI with Eclipse IDE

分類Dev

Write to excel using Apache POI. FileNotFoundException:(The requested operation cannot be performed on a file with a user-mapped section open)

分類Dev

How to auto adjust the column in excel in apache POI

分類Dev

Can't read Excel 2010 file with Apache POI. First Row number is -1

分類Dev

Apache csv file download not in excel

分類Dev

Why format changes while reading data from excel file using poi api java?

分類Dev

Extracting from excel (.xlsx) writing to .txt file

分類Dev

Excel Apache Poiのトルコ通貨

分類Dev

How to make my path relative in selenium apache poi while I input my Excel file? and How to write the test result in excel after performing testing

分類Dev

Writing to a file in Apache Spark

分類Dev

writing to excel sheet in python

分類Dev

Writing a macro for a spreadsheet in excel

分類Dev

Apache POIを使用してExcelで棒グラフを作成する

分類Dev

Apache Poiの数式でExcelを保存する方法は?

分類Dev

Java Apache POIでの既存のExcelファイルの更新

分類Dev

Apache POIを使用したExcelファイルの更新

分類Dev

Java Apache POIを使用してExcelに行を挿入する

分類Dev

apache poi Excelの大きな自動列幅

分類Dev

Apache POIを使用して特定のExcel列を読み取る方法

分類Dev

Apache POIを使用してExcelシートを削除する

分類Dev

Apache POIでの自動折り返し(Excel)

分類Dev

Apache POIを使用してExcelでセルを結合する

分類Dev

Apache POIを使用した基本的なExcel通貨形式

分類Dev

Apache POIを使用してExcelチャートを作成する

分類Dev

Apache POIでのExcelテンプレートの使用

分類Dev

Apache POIを使用してExcelセルに番号を書き込む

Related 関連記事

  1. 1

    Cannot read from Excel file using apache poi

  2. 2

    Read from excel file .xlsx using java Apache POI 3.9 Eclipse

  3. 3

    Apache POI jar is not able to write a special character in excel file

  4. 4

    Accessing Excel Sheets in Java using Apache POI with Eclipse IDE

  5. 5

    Write to excel using Apache POI. FileNotFoundException:(The requested operation cannot be performed on a file with a user-mapped section open)

  6. 6

    How to auto adjust the column in excel in apache POI

  7. 7

    Can't read Excel 2010 file with Apache POI. First Row number is -1

  8. 8

    Apache csv file download not in excel

  9. 9

    Why format changes while reading data from excel file using poi api java?

  10. 10

    Extracting from excel (.xlsx) writing to .txt file

  11. 11

    Excel Apache Poiのトルコ通貨

  12. 12

    How to make my path relative in selenium apache poi while I input my Excel file? and How to write the test result in excel after performing testing

  13. 13

    Writing to a file in Apache Spark

  14. 14

    writing to excel sheet in python

  15. 15

    Writing a macro for a spreadsheet in excel

  16. 16

    Apache POIを使用してExcelで棒グラフを作成する

  17. 17

    Apache Poiの数式でExcelを保存する方法は?

  18. 18

    Java Apache POIでの既存のExcelファイルの更新

  19. 19

    Apache POIを使用したExcelファイルの更新

  20. 20

    Java Apache POIを使用してExcelに行を挿入する

  21. 21

    apache poi Excelの大きな自動列幅

  22. 22

    Apache POIを使用して特定のExcel列を読み取る方法

  23. 23

    Apache POIを使用してExcelシートを削除する

  24. 24

    Apache POIでの自動折り返し(Excel)

  25. 25

    Apache POIを使用してExcelでセルを結合する

  26. 26

    Apache POIを使用した基本的なExcel通貨形式

  27. 27

    Apache POIを使用してExcelチャートを作成する

  28. 28

    Apache POIでのExcelテンプレートの使用

  29. 29

    Apache POIを使用してExcelセルに番号を書き込む

ホットタグ

アーカイブ