How can i get a cells value from an excel sheet using java swing ?
I progressed till opening the excel app using Desktop.getDesktop().open().
My aim is to get a value from an excel sheet and display the value in my java swing.
Is it possible using any libraries.
Please help. Thanks in advance
Use Apache POI. It can open Excel files and access individual cells: http://poi.apache.org/
Here's an example on how to use it:
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
//..
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);
//Get iterator to all the rows in current sheet
Iterator<Row> rowIterator = sheet.iterator();
//Get iterator to all cells of current row
Iterator<Cell> cellIterator = row.cellIterator();
It is from this site: http://viralpatel.net/blogs/java-read-write-excel-file-apache-poi/
Related
XSSFSheet sheet = workbook.getXSSFWorkbook().getSheetAt(0);
XSSFSheet pivot_sheet = workbook.getXSSFWorkbook().createSheet("Sheet1");
XSSFPivotTable pivotTable = pivot_sheet.createPivotTable(new AreaReference("B10:AJ24"), new CellReference("B2"),sheet);
I am getting following error at above line -
java.lang.NullPointerException at
org.apache.poi.xssf.usermodel.XSSFPivotCacheDefinition.createCacheFields(XSSFPivotCacheDefinition.java:145)
at
org.apache.poi.xssf.usermodel.XSSFSheet.createPivotTable(XSSFSheet.java:4065)
I didn't find solution for this using apache poi.
Workaround that I used is mentioned as follows :
Create SXSSFWorkbook as below
SXSSFWorkbook workbook = new SXSSFWorkbook(new XSSFWorkbook(new FileInputStream("xyz.xlsm")));
workbook.removeSheetAt(0);
xyz.xlsm is macro enabled excel which holds VBA code for generating pivot
Write in workbook as usual
Pivots will get generated in excel on opening it after download
I need to check the text format of a cell in .xlsx file (Microsoft Excel file) is strike through or not using Apache POI libraries.
Look following Image
Please Check this image !
I need to check whether B3 Cell text is strike through or not.
Finally I found a way to do this, code as follow
//Using workbook
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File("abc.xlsx")));
//Getting first sheet
XSSFSheet sheet = workbook.getSheetAt(0);
//Checking A1 cell that strikethrough or not
boolean strikeOutStatus=sheet.getRow(0).getCell(0).getCellStyle().getFont().getStrikeout();
System.out.println(strikeOutStatus);
Like this:
Get the XSSFRichTextString from the cell using XSSFCell#getRichStringCellValue()
Get the XSSFFont at a specific position using XSSFRichTextString#getFontAtIndex(int index)
Check the font using XSSFFont#getStrikeout()
Not sure how you get a reference to it but there's HSSFont.getStrikeout() and XSSFFont.getStrikeout()
Im using struts application. I want to work on reports. So going to try POI. I got this jar file poi-3.6-20091214.jar and put them in web-inf/lib folder.When i try using them in class file poi is not recognised. Im trying below...
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //..
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sample sheet"); //Create a new row in current sheet
Row row = sheet.createRow(0); //Create a new cell in current row
Cell cell = row.createCell(0); //Set value to new value
cell.setCellValue("Blahblah");
But import itself wouldnt work.Any idea?
Have you added into the eclipse lib path?
another thing: use poi 3.8, is more recent. Why start with an old release?
I think its HSSFRow & HSSFCell and not Row and Cell. These are included in the jar.
One point to consider using POI is that the same code will not be able to handle both .xls and .xlsx file formats.
missed this: it will be XSSF if you are using .xlsx files and HSSF for .xls file format.
i want to read a microsoft word document i.e. .docx format using POI but getting error:
The supplied data appears to be in the Office 2007+ XML. POI only supports OLE2 Office documents
if anyone can help me to get rid of this??
You should have a read through the Converting from HSSF to the Common SS Usermodel page to help you understand what you need to change.
As a general guide though, if your code was previously
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream("foo.xls"));
HSSFSheet s = wb.getSheetAt(0);
HSSFRow r = s.getRow(0);
System.out.println("Cell A1 is " + r.getCell(0));
It should instead become
Workbook wb = WorkbookFactory.create(new File("foo.xls")); // Or foo.xlsx
Sheet s = wb.getSheetAt(0);
Row r = s.getRow(0);
System.out.println("Cell A1 is " + r.getCell(0));
Please check http://poi.apache.org/spreadsheet/converting.html about new models XSSF and HSSF.
Not sure which API from POI you are using, but I presume that you are using the HSSF API. Instead you should use the the XSSF API (see http://poi.apache.org/spreadsheet/index.html for details). For example instead of using Workbook wb = new HSSFWorkbook(); use Workbook wb = new XSSFWorkbook();
HSSF workbook will not work for higher version of excel file instead of HSSFWorkbook use XXSFWOrkbook.You can have the complete code in the below link
http://aravind-soa.blogspot.com/2017/02/how-to-import-excel-file-into-oracle.html
I have file in type xls with xml extension, I want to read it like excel file.
I trying
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(inputFile));
HSSFSheet sheet = wb.getSheetAt(0);
but it read only with xls extension.
someone know how??
thank
You have to use the XSSF* Classes.
XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (.xlsx) file format.
https://poi.apache.org/spreadsheet/index.html
XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(inputFile));
XSSFSheet sheet = wb.getSheetAt(0);