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.
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
Recently, I met a question:
I need to export an excel(xlsx) with java, which must contain this kind of cell style:
I made a excel file with this vertical text, and exported as a xml file. Then I found that the style has an attribute named 'VerticalText':
By experience, I chose Apache POI. But I couldn't find any way to generate the cell style with POI. I could only find rotate method, which could't meet the requirement.
So I read more code of POI, and found that the cellstyles are build from some xsb file, which do not contain vertical text either.
Any help much appreciated.
The XML in your picture is Excel 2003 SpreadsheetML. But an *.xlsx file is a ZIP archive containing Office Open XML files. In that ZIP archive the styles.xml contains:
...
<cellXfs count="2">
...
<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0">
<alignment textRotation="255"/>
</xf>
</cellXfs>
...
There <alignment textRotation="255"/> is for vertical text.
This can be set using apache poi like so:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CreateXSSFVerticalText {
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook();
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setRotation((short)255);
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("test");
cell.setCellStyle(cellStyle);
FileOutputStream fileOut = new FileOutputStream("CreateXSSFVerticalText.xlsx");
workbook.write(fileOut);
fileOut.close();
workbook.close();
}
}
Since the Office Open XML formats (like *.xlsx) are ZIP archives containing XML files it is pretty easy to determine the necessary XML attributes. Simply create a simple *.xlsx file having the needed formatting using the Excel GUI. Then unzip the *.xlsx file and have a look at xl/styles.xml.
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()
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/
I have a .xls file which is auto generated through some process. There is only 1 sheet(with a name) in the workbook. When i use the following code, it is not getting hold of the sheet
HSSFSheet sheet = workbook.getSheetAt(0);
But when i copy the contents of that sheet into a new .xls file in sheet 1, and rerun the code, its working fine. My assumption is, it has to catch hold of any sheet that exists at index 0, then why its not working in the former case and working fine in the latter case? Any ideas?