Emin Çiftçi
Emin Çiftçi

Reputation: 139

Apache poi the file is corrupted

I am working with apache poi and I create a HSSF workbook and try to open a xlsx file. But when I open with excel, it says the file is corrupted. Here is my code.

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.FileOutputStream;

public class Excel {

    public static void main(String[] args) {

        Workbook workbook = new HSSFWorkbook();

        try {
            FileOutputStream output = new FileOutputStream("Test1.xls");
            workbook.write(output);
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 1

Views: 2862

Answers (1)

druid1123
druid1123

Reputation: 108

You have to use XSSFWorkbook for XLSX.

And try creating at least one sheet and see if it is opening correctly.

HSSFWorkbook workbook = new HSSFWorkbook();
try {
    FileOutputStream output = new FileOutputStream("Test1.xls");
    workbook.createSheet("sheet1")
    workbook.write(output);
    output.close();
} catch (Exception e) {
    e.printStackTrace();
}

Upvotes: 5

Related Questions