Are there any limitations to row Iterator in Apache POI? - java

try {File inputFile = new File(str2);
File inputFile1 = new File (str1);
XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(inputFile));
XSSFWorkbook wb1 =new XSSFWorkbook(new FileInputStream(inputFile1));
XSSFSheet sheet1 = wb.getSheetAt(0);
XSSFSheet sheet2;
Iterator<Row> iterator1 =sheet1.iterator();
while (iterator1.hasNext()){
row1=iterator1.next();
cell1= row1.getCell(4);
String output=cell1.toString();
System.out.println(cell1.toString());
//a1.add(cell1.toString());
// int m = jList1.getSelectedIndex();
}
catch (Exception e){
Logger.getLogger(def.class.getName()).log(Level.SEVERE, null, e);
}
}
It doesn't read print after 10k rows although there are 13k rows. Any solutions? Can SXSSF Sheet can be used?

As far as I know, generally there is no limit to iterator's size(assuming there is enough RAM or it is buffering from a file). in your case I suggest two things:
make sure the Apache POI doesn't have a default limit.
There is no weird character on the 10k'th that iterator may consider it as the end of the buffer

Related

How to Solve Apache POI "workbook" might not have been initialized

I need to read file excel, and I am working with java spring boot, Apache poi.
they told us
When working with the newer .xlsx file format, you would use the XSSFWorkbook, XSSFSheet, XSSFRow, and XSSFCell classes. To work with the older .xls format, use the HSSFWorkbook, HSSFSheet, HSSFRow, and HSSFCell classes.
this is my code:
Workbook workbook;
if (FileExtension == "xlsx"){
workbook = new XSSFWorkbook(excelFile);
}
else if (FileExtension == "xls"){
workbook = new HSSFWorkbook(excelFile);
}
Sheet datatypeSheet = workbook.getSheetAt(0);
Now im getting "workbook" might not have been initialized.
How to solve that?
Current apache poiversions provide WorkbookFactory. Using the create methods of that class there is no need for determining the file format (XSSF or HSSF) by file extension. The WorkbookFactory.create methods are creating XSSFWorkbook or HSSFWorkbook dependent of the file contents found.
So do using:
...
Workbook workbook = WorkbookFactory.create(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
...
I think what you want to express is this...
Workbook workbook;
if (FileExtension == "xlsx"){
workbook = new XSSFWorkbook(excelFile);
}
// for other case
else{
workbook = new HSSFWorkbook(excelFile);
}
Sheet datatypeSheet = workbook.getSheetAt(0);
Notice the comment in the code, I am sure can investigate the difference by yourself, whick helps a lot.
You need to use xmlbeans3.1 instead of xmlbeans4.0.
Try this...
Workbook workbook = null;
Not initialized.. you should do this
Workbook wb;
wb = Workbook.getWorkbook(new File("d:\\test\\book1.xls"));
I believe it is apart of JVM issues, but initializing and declaring at the sometime is sometimes the only way.
Try this:
if (FileExtension == "xlsx"){
Workbook workbook = new XSSFWorkbook(excelFile);
}
else if (FileExtension == "xls"){
Workbook workbook = new HSSFWorkbook(excelFile);
}
Sheet datatypeSheet = workbook.getSheetA

New Sheet is not getting created when i do Apache POI on a loop

I am facing an issue when i write huge set of data to a Excel file with multiple sheets. I am using apache POI for the excel export.
File file = new File("../path/file.xls");
FileOutputStream fout = new FileOutputStream(file);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int limit = 100000,offset=0,count=0,sheetIndex=0;
XSSFWorkbook workbook = new XSSFWorkbook();
do{
XSSFSheet sheet = null;
if (file.exists() && sheetIndex > 0) {
try {
workbook = (XSSFWorkbook)WorkbookFactory.create(file);
} catch (InvalidFormatException e) {
e.printStackTrace();
}
sheet = workbook.createSheet("Sheet-"+sheetIndex);
}else{
workbook = new XSSFWorkbook();
sheet = workbook.createSheet("Sheet-"+sheetIndex);
}
Row header = sheet.createRow(0);
//...Header row creation...
List<DataType> result = query(criteria,offset,limit);
offset = offset + limit;
count = results.size();
sheetIndex++;
int rowCount = 1;
for(DataType rowData : results){
Row row = sheet.createRow(rowCount++);
//row creation....
}
try {
workbook.write(outputStream);
outputStream.writeTo(fout);
} finally {
outputStream.flush();
}
}while(count == limit);
workbook.write(outputStream);
outputStream.writeTo(fout);
outputStream.close();
fout.close();
In the loop i am fetching 100k records from DB and writing it to the excel, and each 100k i am creating a new Sheet until there are no more records from the DB.
This code have 2 issues
1. I am facing issues in opening the file, the excel file alert me that it has issues when i try to open, eventually when i say ok it loads the data.
I can see there are only 1 sheet with 100k data though my DB contains 240M records. I also can see the loop is looping for number of times.
How can i get these issues resolved? really stucked!
Thanks in advance.
The XSSFWorkbook workbook is created multiple times and it overwrites the one created on previous loop. The workbook needs to be created only once.
I suggest changing the loop entry to the following:
XSSFWorkbook workbook = new XSSFWorkbook();
do {
XSSFSheet sheet = workbook.createSheet("Sheet-"+sheetIndex);
Row header = sheet.createRow(0);
//...Header row creation...
// remaining code
I have changed WorkBook type to SXSSFWorkbook and set the flush limit to 100 and it worked.
The performance has increased 5 times better than the XSSFWorkbook.

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

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

Apache POI xls file error

I want to read both xls and xlsx file format. It is working fine for xlsx format but I am getting following error while uploading xls file.
Code:
try {
FileInputStream fileInputStream = new FileInputStream("/apps/" + fileName);
//POIFSFileSystem fsFileSystem = new POIFSFileSystem(fileInputStream);
Workbook workBook = WorkbookFactory.create(OPCPackage.open(fileInputStream));
//XSSFWorkbook workBook1 = new XSSFWorkbook();
Sheet ssSheet = workBook.getSheetAt(0);
Iterator rowIterator = ssSheet.rowIterator();
while (rowIterator.hasNext()) {
Row ssRow = (Row) rowIterator.next();
Iterator iterator = ssRow.cellIterator();
List cellTempList = new ArrayList();
while (iterator.hasNext()) {
Cell ssCell = (Cell) iterator.next();
cellTempList.add(ssCell);
}
cellDataList.add(cellTempList);
}
} catch (Exception e) {
e.printStackTrace();
}
Error:
org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:148)
at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:623)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:230)
Please help.
-Thanks
I think that your problem is due to you trying to construct your workbook with the OPCPackage, even if you use WorkbookFactory. OPCPackage "unzip" your .xlsx in order to be able to read the xml files inside, but this should not work for HSSF since it is a binary file.
My recomendation would be that you use another constructor such as
WorkbookFactory.create(InputStream input)
I guess it should work fine.

Categories