I am reading excel file using Apache POI in gwt using java. I have one excel file with 15000 records and four columns Count, Name, Mob No and EmailID. I am uploading excel file in blobstore GAE then I am reading that excel file. I am just checking with logger all data is reading or not. when I deployed and test then only last 266 rows are reading and displayed in logger. Why? how to read all data from excel. my code is:
HSSFWorkbook workbook = new HSSFWorkbook(newBlobstoreInputStream(blobkey));
HSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
// Traversing over each row of XLSX file
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
logger.log(Level.SEVERE,cell.getStringCellValue() + "\t");
break;
case Cell.CELL_TYPE_NUMERIC:
logger.log(Level.SEVERE,cell.getNumericCellValue() + "\t");
break;
case Cell.CELL_TYPE_BOOLEAN:
logger.log(Level.SEVERE,cell.getBooleanCellValue() +"\t");
break;
default :
}
}
System.out.println("");
}
how shall i read all data from start to end?
any help Thanks in advance
And One more is when i change XSSFWorkbook and XSSFSheet instead of HSSFWorkbook and HSSFSheet to read Xlsx excel then its throw Exception : org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. how can i fix this
Any help
Thanks in advance.
For the exception you are getting, .xls files are read by HSSFWorkbook & HSSFSheet whereas .xlsx are read by XSSFWorkbook & XSSFSheet.
You can use the Factory class to get the appropriate Workbook if you are goin to handle both .xls & .xlsx format.
Related
I am new to java.I have been trying to automate a website using selenium using TestNG in POM model.I need to enter data for a web form,the data which is stored in a .xlsx file.There are multiple data in multiple columns having o data type string,integer,boolean.I am using Apache POI to extract data from the file using the following code:
ArrayList<Object> arrlist = new ArrayList<Object>(); try {
FileInputStream file = new FileInputStream( new File("C:\\Users\\dell i7\\Desktop\\imp docs\\TestData.xlsx"));
// Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);
// Get first/desired sheet from the workbook XSSFSheet sheet =
workbook.getSheetAt(0);
// Iterate through each rows one by one
Iterator<Row> rowIterator =sheet.iterator();
while (rowIterator.hasNext())
{ Row row = rowIterator.next(); // For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) { Cell cell = cellIterator.next();
// Check the cell type and format accordingly switch (cell.getCellType()) {
case NUMERIC:
//System.out.println((int) cell.getNumericCellValue());
arrlist.add((int) cell.getNumericCellValue());
break;
case STRING:
System.out.println(cell.getStringCellValue());
arrlist.add(cell.getStringCellValue());
break;
default: break; } }
System.out.println("");
} file.close(); }
catch (Exception e
) {
e.printStackTrace();
}
Now a TestNG Data provider requires an Object[][] to get the data and send it to the Test method.What is the best way to do so?
#DataProvider(name ="Testlogin")
public Object[][] getData()
{
//your data from excel sheet
return data;
}
I am reading an excel file as a text, and I am posting this string to server. However, I couldn't convert this string to a proper data structure to use it as an input for apache poi functions without creating a temp file in the file system. What I want to do is this;
Excel String -> An Excel File Object (imaginary) -> Reading data from this imaginary file
I don't want to create a temp file every time someone tries to upload an excel file.
I tried something like this;
InputStream stream = new ByteArrayInputStream(OntologyContentTxt.getBytes(StandardCharsets.UTF_8));
HSSFWorkbook wb = new HSSFWorkbook(stream);
HSSFSheet sheet=wb.getSheetAt(0);
HSSFRow row;
HSSFCell cell;
Iterator rows = sheet.rowIterator();
while (rows.hasNext())
{
row=(HSSFRow) rows.next();
Iterator cells = row.cellIterator();
while (cells.hasNext())
{
cell=(HSSFCell) cells.next();
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING)
{
System.out.print(cell.getStringCellValue()+" ");
}
else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
{
System.out.print(cell.getNumericCellValue()+" ");
}
}
System.out.println();
}}
But it gives me this error.
org.apache.poi.poifs.filesystem.NotOLE2FileException: Invalid header signature; read 0xE011BDBFEFBDBFEF, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document
How can I make this kind of convert?
Thanks.
I am trying to read data from one sheet of excel file and write to other sheet of same excel file.I have tried this :
FileInputStream file = new FileInputStream(new File("E:\\excel\\input.xlsx"));
//Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);
//Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
//Check the cell type and format accordingly
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
System.out.print((int)cell.getNumericCellValue()+ " ");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue());
break;
}
}
System.out.println("");
}
file.close();
CreationHelper createHelper = workbook.getCreationHelper();
XSSFSheet newSheet = workbook.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = newSheet.createRow((short)0);
// Create a cell and put a value in it.
Cell cell = row.createCell(0);
cell.setCellValue(1);
// Or do it on one line.
row.createCell(1).setCellValue(1.2);
row.createCell(2).setCellValue(
createHelper.createRichTextString("This is a string"));
row.createCell(3).setCellValue(true);
System.out.println("writing to file");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream(new File("E:\\excel\\input.xlsx"));
workbook.write(fileOut);
fileOut.close();
I have successfully read data from excel sheet But I am getting exception while writing in new sheet given below
java.io.FileNotFoundException: E:\excel\input.xlsx (The process cannot access the file because it is being used by another process)
This will happen when you are trying to execute this program while the "input.xlsx" file is already opened by another application.
Please close any instance of MS Excel and try to run it again
Just a guess but XSSFWorkbook should be implementing Closeable thus it has a .close() that should be called.
Maybe the file result still in use because of that?
You are probably going to need a middle step there: write to a new file, close the XSSFWorkbook and then copy the new file on the old one that you want to write on
I try to read this excel file: Test.xlsx, to do this I used an example I found on the internet, but
I used this link as en example: http://howtodoinjava.com/2013/06/19/readingwriting-excel-files-in-java-poi-tutorial/
it doens't work.
I copied the url for the file, so there is no error there.
Whenever I run it, it doensnt show errors just : []
When I debug it, it shows me that the listsize = 0
What should I change?
ArrayList<String> list = new ArrayList<String>();
#Override
public List<String> getExcel(){
try {
FileInputStream file = new FileInputStream(new File("C:\\Users\\user\\Documents\\Test.xlsx"));
//Create Workbook instance holding reference to .xlsx file
HSSFWorkbook workbook = new HSSFWorkbook(file);
//Get first/desired sheet from the workbook
HSSFSheet sheet = workbook.getSheet("Sheet1");
//Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through all the columns
if (row.getRowNum() <= 7) {
continue;// skip to read the first 7 row of file
}
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
list.add(cell.getStringCellValue());
}
//System.out.println("");
}
file.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
You are using the wrong class for the file you are trying to open (Test.xlsx). By the extension, I can assume this is an Excel 2007 or later document. Use HSSFWorkbook for Excel 2003 and XSSFWorkbook for Excel 2007 or later. Review Apache POI documentation that came with the downloaded package. It contains basic tutorials on how to accomplish this.
You will need to replace all of the 'HSSF' classes for the 'XSSF' equivalent. Beware that the methods called to create the parts of the document (i.e. Workbook, Sheet, etc) are not always the same.
Try this link. I created a small demo for a simple tutorial on Apache POI some time back. There is an Excel example you could follow. The location contains source code and a set of slides that you should be able to follow easily.
I am extracting Russian characters from db and generating one excel sheet. But unfortunately in extracted excel sheet, I have to change the character set manually to "UNICODE(UTF-8)" , otherwise I can't able to see Russian character. Is anybody aware how to update this character set through java(I am not asking to convert the value) ?
Let me know if anything needs to be provided from my end.
Is Apache POI an option ?
Apache POI manages the encoding for you. All the text returned is in Java Unicode format (which I believe is UTF-16, and not UTF-8)
Here is how you read Excel sheet in POI:
try {
FileInputStream file = new FileInputStream(new File("C:\\test.xls"));
//Get the workbook instance for XLS file
HSSFWorkbook workbook = new HSSFWorkbook(file);
//Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch(cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t\t");
break;
}
}
System.out.println("");
}
file.close();
FileOutputStream out =
new FileOutputStream(new File("C:\\test.xls"));
workbook.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Source: http://viralpatel.net/blogs/java-read-write-excel-file-apache-poi/