How to read .xlsx sheet from Client Side using poi 3.5
Anshul Agrawal
Greenhorn
Joined: Jul 15, 2008
Posts: 18
posted
0
Hello All
Please somebody explain and send me code to read .xlsx file from client side .. read its cell contents and then manipulate the data using POI 3.5 API ?
Please help
Regards
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
7
posted
0
The POI site has lots of examples: http://poi.apache.org/spreadsheet/quick-guide.html That page should also be part of the POI distribution you downloaded. You need to use the "XSSF" API calls; the "HSSF" calls are for the old binary Excel format (.xls).
I tried ..but i am really not getting it.
First i have to create a file ???
Then read it contents?
I have a file directly at client FTP server which is a .xlsx .
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
7
posted
0
First i have to create a file ?
No; why do you think that? The sections "Reading and Rewriting Workbooks" and "Iterate over rows and cells" should be particularly helpful.
Anshul Agrawal
Greenhorn
Joined: Jul 15, 2008
Posts: 18
posted
0
When i am running my thread it says.
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. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
Code >>>
salesFileName is the name of the File .
//File salesFile = new File(destDir + salesFileName);
try {
inputStream = new FileInputStream(destDir);
} catch (FileNotFoundException e) {
System.out.println("File not found in the specified path.");
e.printStackTrace();
}
try {
fileSystem = new POIFSFileSystem(inputStream);
XSSFWorkbook workBook = new XSSFWorkbook(salesFileName);
XSSFSheet sheet = workBook.getSheetAt(0);
totalRows = sheet.getPhysicalNumberOfRows();
System.out.println("total no of rows >>>>"+totalRows);
Is "destDir" a directory or a file? If the former, then this doesn't do anything useful. But since you're not using the "inputStream" field later it probably doesn't matter.
Also In Reading and Writing section It is using WorkBookFactory...
Workbook wb = WorkbookFactory.create(inp);
Since the POI user guide recommends to use this for reading a file, that's where I would start.
If we are talikng about poi then why my IDE is asking to import .jxl (API for Workbook)
I don't understand this. Where is a message appearing about ".jxl"? How is that related to the code posted above?
Anshul Agrawal
Greenhorn
Joined: Jul 15, 2008
Posts: 18
posted
0
I got it sir.
Thank you very much.
I applied
try {
System.out.println("destDir==> "+destDir);
XSSFWorkbook workBook = new XSSFWorkbook(destDir);
XSSFSheet sheet = workBook.getSheetAt(0);
totalRows = sheet.getPhysicalNumberOfRows();
System.out.println("total no of rows >>>>"+totalRows);
Just got the rows will now be applying to fetch the cell data.
vamshi kiran
Greenhorn
Joined: Dec 08, 2009
Posts: 2
posted
0
hi Anshul Agrawal,
i also had the same problem of "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. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
".
i will try the sample you wrote in this forum
try {
System.out.println("destDir==> "+destDir);
XSSFWorkbook workBook = new XSSFWorkbook(destDir);
XSSFSheet sheet = workBook.getSheetAt(0);
totalRows = sheet.getPhysicalNumberOfRows();
System.out.println("total no of rows >>>>"+totalRows);
I'm sure I remember using that in some code I wrote last year. Could you not find any suitable methods in the API documentation for XSSFSheet? I don't have it available right now, so it should be faster for you to have a look than for me to search out the docs.
Murty Medisetti
Greenhorn
Joined: Jan 23, 2010
Posts: 22
posted
0
I didn't find any method to get data directly of Row,column position. I am iterating through the row by using Iterator then iterating through the cell to get particular position data.
String currencyVal = row.getCell(56).getStringCellValue(); // gets cel's data
teja chandra
Greenhorn
Joined: Sep 13, 2011
Posts: 5
posted
0
Hi, Please help me out with code for reading an excel file of xlsx format (excel2007) using XSSFWorkBook. its urgent
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
7
posted
0
No doubt it's urgent for you, but that doesn't make it urgent for us. This topic as well as the POI web site have examples of how to read XLSX spreadsheets; what specifically are you looking for that isn't covered in either place?
teja chandra
Greenhorn
Joined: Sep 13, 2011
Posts: 5
posted
0
Hi Ulf Dittmer,
Pleasefind the below code and i amgetting the exception
i will import the jar. but the problem is related to reading the stream. can you help me out
Teja
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
7
posted
0
If you aren't using at least Java 6, then you need to find a JAXP version that includes the javax.xml.stream package and use that instead of the JAXP version built into your JRE.
teja chandra
Greenhorn
Joined: Sep 13, 2011
Posts: 5
posted
0
Hi Friends,
please help me out in loading the 2007 excel using ss.usermodel or XSSFWorkbook. I am getting the error below.
Error Message: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
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. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
Is there any sample code of the correct implementation.
Teja
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
7
posted
0
Is this question about the exact same code as your previous question? That would mean you got previous problem resolved? If it's about different code, then post the relevant extract from the code you're asking about.
It sounds as if you are using HSSF classes to work with an XLSX document, which won't work.
teja chandra
Greenhorn
Joined: Sep 13, 2011
Posts: 5
posted
0
Hi Ulf Dittmer,
yes, the same code. can you let me know the root cause and the code snippet i need to add. also if there is some sample code, please send me the same.
please send me the code for uploading an excel 2007 using ss.usermodel or xssfworkbook.
thanks,
Teja
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
7
posted
0
Ulf Dittmer wrote:It sounds as if you are using HSSF classes to work with an XLSX document, which won't work.
You will need to investigate why that is.
teja chandra
Greenhorn
Joined: Sep 13, 2011
Posts: 5
posted
0
I pasted the code also right. Am i using HSSF? and i put dicussion in the forums after all analysis my friend. its easy to advise.
file.org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:55)
in my code...
InputStream fis=fieldItem.getInputStream();
//POIFSFileSystem fs = new POIFSFileSystem(fis);
XSSFWorkbook workbook1=new XSSFWorkbook(fis);