Reputation: 139
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
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