Generic Way of Creating workbook Apache POI - java

I have a method that reads an excel sheet using Apache POI library. To make that method generic, I need to to write some code in a catch block. Here's the method:
private List<String[]> readFile(String filePath) throws IOException{
List<String[]> sheetValues = new ArrayList<String[]>();
//Two types of fileInputStreams for both the types of workbook i am about to use
//
FileInputStream fileInputStream = new FileInputStream(new File(filePath));
FileInputStream fileInputStream2 = new FileInputStream(new File(filePath));
LineItemFileReader fileReader = new LineItemFileReaderImpl();
//If the file is in xls format i should use HSSFWorkbook
//If the file is in xlsx format i should use XSSFWorkbook
try {
//If the file is in xlsx format then the below line will throw the
//Exception and catch block code will be executed
HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
sheetValues = fileReader.ParseSheet(workbook.getSheetAt(1),evaluator);
} catch (OfficeXmlFileException exception){
XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream2);
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
sheetValues = fileReader.ParseSheet(workbook.getSheetAt(1),evaluator);
} finally{
fileInputStream.close();
fileInputStream2.close();
}
return sheetValues;
}
So what's happening here is that I dont know what type of file I am being supplied. I am deciding that on the basis of the try/catch block given above. So is there any generic way of deciding which workbook to use? Since the above code has flow control logic written in a catch block, it's a bad practice.

Use WorkbookFactory.create(...) for opening the Workbook. It will create the appropriate subclass internally:
Workbook workbook = WorkbookFactory.create(new File(filePath));
// ...
workbook.close();

Related

reading from excel using Java poi 4.11

Why it outputs an error"Exception in thread "main" java.lang.IllegalArgumentException: Sheet index (0) is out of range (no sheets)"
import org.apache.poi.ss.usermodel.Workbook;
import java.io.FileInputStream;
import java.io.IOException;
public class test {
public static void main(String[] args) throws IOException {
Workbook wb = new HSSFWorkbook();
FileInputStream fis = new FileInputStream("C:/Users/KP/IdeaProjects/JavaExcel/read.xls");
String result = wb.getSheetAt(1).getRow(1).getCell(1).getStringCellValue();
System.out.println(result);
fis.close();
}
}
You created a new Workbook and access the sheet at Index 1.
If you want to load your Inputstream to your workbook you need to create a new Workbook with the InputStream.
You should also use try with resources to close your stream and workbook.
try (InputStream in = new FileInputStream("C:/Users/KP/IdeaProjects/JavaExcel/read.xls");
Workbook wb = new HSSFWorkbook(in)) {
String result = wb.getSheetAt(1).getRow(1).getCell(1).getStringCellValue();
System.out.println(result);
}
Please notice that getSheetAt(1) returns the second sheet. If you want the first sheet you need to use getSheetAt(0). Same goes for getRow and getCell. So if you want the first cell in the first row in the first workbook use:
String result = wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue();
declare/ Initialize the workbook after file input stream like below by passing fis object
FileInputStream fis = new FileInputStream("C:/Users/KP/IdeaProjects/JavaExcel/read.xls");
Workbook wb = new HSSFWorkbook(fis);
hope you have data in the second sheet of excel file as mentioned "wb.getSheetAt(1)", if not make it wb.getSheetAt(0)

NotOLE2FileException: Invalid header signature; read 0x0000000000000000, expected 0xE11AB1A1E011CFD0 - file appears not to be a valid OLE2 document

try {
File file = new File("file4.xls");
if (!file.exists()) file.createNewFile();
FileInputStream fis = new FileInputStream(file);
POIFSFileSystem fileSystem = new POIFSFileSystem(fis);
// FileInputStream fis = new FileInputStream(file);
HSSFWorkbook workbook = new HSSFWorkbook(fileSystem);
//Sheet sheet = workbook.createSheet("sheet0");
Sheet sheet = workbook.getSheet("sheet1");
sheet.createRow(0).createCell(0).setCellValue("HelloWorld");
Cell cell = sheet.createRow(1).createCell(0);
cell.setCellValue("Value_1_1");
fis.close();
FileOutputStream fos = new FileOutputStream(file);
workbook.write(fos);
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
I am trying to create simple excel file using java. I am getting above mentioned error.
Open the file once.
Check your file once on click save as. If it is generated from HTML it will show as Web page(*.htm , .html) below drop down to the file.
You can save as Excel 97-2003 Workbook (.xls) and retry to read from code. It worked in my case.
Your problem is this line here:
if (!file.exists()) file.createNewFile();
That creates a brand new 0 byte file, which is not a valid Excel file. That's why POI objects.
Also, you're using an old version of Apache POI. Newer ones give a more helpful exception if you're silly enough to ask them to read a zero byte file
Taking account of the advice on Files vs InputStreams, but noting you're doing an in-place write which isn't yet fully supported on an opened File, change your code to be more like:
Workbook workbook = null;
File file = new File("file4.xls");
if (!file.exists()) {
if (file.toString().endsWith(".xlsx")) {
workbook = new XSSFWorkbook();
} else {
workbook = new HSSFWorkbook();
}
} else {
workbook = WorkbookFactory.create(new FileInputStream(file));
}
That will work for both .xls and .xlsx files, and avoids your errro
Though you really ought to upgrade your version of Apache POI too...

Appending sheet to existing excel file using Java and Apache POI

I have a Java method writeToExcel(String sheetName, Map), which creates a new sheet with name 'sheetName' in a new excel file and write the map data into it . When I call the method with different sheetName arguments more than once, existing sheet gets replaced by the last called one. I want to create new sheets in the same excel file each time whenever the method is called with different sheetName argument, without losing existing sheets. Here is my code.
public static void writeToExcel(String fileName,Map<Integer,String[]> excelData){
String filePath="/Data/excel.xlsx";
XSSFWorkbook workbook=new XSSFWorkbook();
XSSFSheet sheet=workbook.createSheet(fileName);
Set<Integer> keySet=excelData.keySet();
int passedCount=0;
int failedCount=0;
int rowNo=0;
int cellNo=0;
Row row;
Cell cell;
try{
File file=new File(filePath);
FileOutputStream output=new FileOutputStream(file);
for(Integer key:keySet){
row=sheet.createRow(rowNo++);
String[] dataToWrite=excelData.get(key);
cellNo=0;
for(String str:dataToWrite){
cell=row.createCell(cellNo++);
cell.setCellValue(str);
}
}
workbook.write(output);
output.close();
workbook.close();
}
catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
}
Seems like you're always creating a new workbook in the second line of the method. So it is not the sheet that is replaced but the entire workbook. Better use
XSSFWorkbook workbook=new XSSFWorkbook(new File(filePath));
That should do the trick.
XSSFWorkbook workbook=new XSSFWorkbook(new File(filePath)); => XSSFWorkbook workbook=new XSSFWorkbook(filePath); as no suit constructor for XSSFWorkbook workbook=new XSSFWorkbook(new File(filePath))

File Excel From Apache POI Cant Open by Ms Excel (corrupt)

I don't know why the file I write using POI cant be opened by Ms Excel 2013, but the file is still readable by POI. (cell value can be changed)
this is the error from file
here is the code
FileInputStream fis = null;
try {
fis = new FileInputStream(fileUri); //not error at fileUri
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String urii = fileUri.replace(".xls", "0.xls"); //not error
File fisx = new File(urii);
Workbook workbook = null;
workbook = new HSSFWorkbook(fis);
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
String p = cell.getStringCellValue();
TextView a = (TextView) findViewById(R.id.txtUri);
cell.setCellValue(new String("popo"));
String x = cell.getStringCellValue();
TextView b = (TextView) findViewById(R.id.txtFile);
a.setText(p);
b.setText(x);
OutputStream fos = null;
fos = new FileOutputStream(fisx);
workbook.write(fos); //main problem
fos.flush();
fos.close();
Thanks for your help!!
There are two issues with your code. Firstly this:
FileInputStream fis = null;
try {
fis = new FileInputStream(fileUri);
As explained in the Apache POI Docs, don't use an InputStream if you have a File!
Secondly, this:
Workbook workbook = null;
workbook = new HSSFWorkbook(fis);
That will only work for .xls files, not for .xlsx ones. Instead, you need to use WorkbookFactory which identifies the type and gives you the right workbook for the format
So, change your code to be
File file = new File(fileUri);
Workbook workbook = WorkbookFactory.create(file);
The major problem that i see here is:
Workbook workbook = null;
workbook = new HSSFWorkbook(fis);
Instead you have to use:
Workbook workbook = null;
workbook = new XSSFWorkbook(fis);
TO be readable by MS EXCEL 2013.
Solved :
by using real android device instead of bluestack emulator,
I dont know why, but it works!!
Thanks everyone :D
You are calling getSheetAt(0) but you did not create any sheet before (workbook.createSheet(“name”)
The solution is to use the .xls extension and NOT .xlsx, as outlined in this answer

Java POI Create new Workbook with SS model?

I'm a little confused, I used to do this:
HSSFWorkbook wb = new HFFSWorkbook();
But with the new POI, I dont have to do that.
I can't do this:
Workbook wb = new Workbook();
I understand WorkbookFactory.create, but that is for opening a file.
How do I set up a new workbook with this ss model?
You can still use the SS model but need to decide on the file format at the time of creation.
For xls -> Workbook wb = new HSSFWorkbook();
For xlsx -> Workbook wb = new XSSFWorkbook();
In "New POI", you can write/read both XLS files and XLSX files. In any case, for XLS file-format you were using:
HSSFWorkbook wb = new HSSFWorkbook();
So for XLSX file-format, you have to use:
XSSFWorkbook wb = new XSSFWorkbook();
// you could also do below
// Workbook wb = new XSSFWorkbook();
Also it would be helpful for you if you refer below links for starting with XLS to XLSX migration.
1. http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFWorkbook.html
2. http://poi.apache.org/spreadsheet/converting.html
Make sure you download and add the POI JAR file to your project’s class path before running the code. The Apache POI JAR file can be found here.
public void main(String[] args) throws IOException {
// Directory path where the xls file will be created
String destinationFilePath = "C:/Users/devesh_/Documents/HelloWorld.xls";
// Create object of FileOutputStream
FileOutputStream fout = new FileOutputStream(destinationFilePath);
// Build the Excel File
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
HSSFWorkbook workBook = new HSSFWorkbook();
// Create the spreadsheet
HSSFSheet spreadSheet = workBook.createSheet("Hello_World");
// Create the first row
HSSFRow row = spreadSheet.createRow((short) 0);
// Create the cells and write to the file
HSSFCell cell;
// Write Hello
cell = row.createCell(0);
cell.setCellValue(new HSSFRichTextString("Hello"));
// Write World
cell = row.createCell(1);
cell.setCellValue(new HSSFRichTextString("World"));
workBook.write(outputStream);
outputStream.writeTo(fout);
outputStream.close();
fout.close();
}
When creating a file, you need to decide up front what format it'll be - you can't just wait until write-out time to do that. You code would be something like:
Workbook wb = null;
if (shouldBeXLS) {
wb = new HSSFWorkbook();
} else {
wb = new XSSFWorkbook();
}
// work on the file in a generic way
// save, with a suitable name
String filename = "test.xls";
if (!shouldBeXLS) { filename = filename + "x"; }
FileOutputStream fout = new FileOutputStream(filename);
wb.write(fout);
fout.close();
At the start, decide what format you want for this particular instance, and create that. Treat it as a general workbook, and write to it in the common way. At the end, remember what it is so you can give the file the right extension!
(When reading a file in, WorkbookFactory will let you load the appropriate instance for the file type. When creating a new file, you have to pick yourself as there's nothing there yet!)

Categories