I want to read data ( cell values ) from a certain xlsx file using apachi poi.
The code below creates the SXSSFWorkBook instance successfully and assigns db.xlsx ( my dummy xlsx). I have tried changing sheet numbers and double checking it with the getSheetNumber method to make sure the workbook is correctly assigned.
Next I want to assign a specific sheet (index 0 with name main) to SXSSFSheet instance, but currently it returns null. (I have both tried getSheetAt and getSheet methods).
SXSSFRow DummyRow;
SXSSFCell DummyCell;
int RowCount;
OPCPackage pkg = OPCPackage.open(blabla string adress);
XSSFWorkbook wb = new XSSFWorkbook(pkg);
Workbook MainBook = new SXSSFWorkbook(wb,100);
int a = MainBook.getNumberOfSheets();
SXSSFSheet MainSheet = (SXSSFSheet) MainBook.getSheetAt(0);
RowCount = MainSheet.getLastRowNum();
What am I doing wrong?
Edit:
I have tried getSheetName method and had a positive result. So the problem is with reaching rows in Worksheet. so, the last line getLastRowNum() is not working.
You can't. SXSSFWorkBook is write only, it doesn't support reading
For low memory reading of .xlsx files, you should look at the XSSF and SAX EventModel documentation
Related
I use Apache POI to write data to a predefined XLSM file. I use this code to open existing file:
Cell cell;
File file = new File(XLSMPath);
FileInputStream inputStream = new FileInputStream(file);
XSSFWorkbook workbook = XSSFWorkbookFactory.createWorkbook(inputStream);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row = sheet.getRow(recordcount+4);
Data is written in first iteration in the 5th row and so on. Code to set value of a given cell:
cell = row.getCell(CellReference.convertColStringToIndex("A"));
cell.setCellValue(myvalue);
It worked fine for the first 400 iterations, but after that I get following error message:
Cannot invoke "org.apache.poi.ss.usermodel.Cell.setCellValue(String)"
because "cell" is null
You need to create the worksheet cells yourself. Just check if your Cell is null and create new Cell using createCell(int):
cell = row.getCell(CellReference.convertColStringToIndex("A"));
if (cell == null) {
// maybe in your case index should be taken in other way
cell = row.createCell(CellReference.convertColStringToIndex("A"));
}
cell.setCellValue(myvalue);
I know i am missing the main function here. I want to copy/ write only particular row from my source excel to destination excel using apache POI.
eg. My source excel has 10 rows. I need only 5th row to be copied to my destination excel.
My class,
public class Test1 {
public static void main(String[] args) throws Exception{
File srcFile=new File("C:\\Test\\Read.xlsx");
FileInputStream fis=new FileInputStream(srcFile);
XSSFWorkbook wb=new XSSFWorkbook(fis);
XSSFSheet sheet1=wb.getSheetAt(0);
File desFile=new File("C:\\Test\\Write.xlsx");
FileOutputStream fout=new FileOutputStream(desFile);
wb.write(fout);
wb.close();
}
}
As per the Apache POI docs, you can use the XSSFSheet.getRow(int) method to get a row at a specific index.
// 5th row
Row row = sheet.getRow(4);
To add this row to a new workbook, you'll have to iterate through each cell in the row object and set the value of cells in the new workbook to these values. An example can be found here.
I want to read and write large excel files. Therefore, I used SXSSFWorkbook to write the excel file and XSSF and SAX EVENT API to read the files.
However, the cell content is empty when the excel file is read, and if the excel file is written using SXSSFWOrkbook. If I open the written excel file and save it again, the content is shown correctly.
The following is the code I used to write the excel file.
SXSSFWorkbook wb = new SXSSFWorkbook();
wb.setCompressTempFiles(true);
SXSSFSheet sh = (SXSSFSheet) wb.createSheet();
// sh.setRandomAccessWindowSize(100);// keep 100 rows in memory,
// exceeding rows will be flushed to disk
for (int rownum = 0; rownum < 100; rownum++) {
Row row = sh.createRow(rownum);
for (int cellnum = 0; cellnum < 10; cellnum++) {
Cell cell = row.createCell(cellnum);
String address = new CellReference(cell).formatAsString();
cell.setCellValue(address);
}
}
FileOutputStream out = new FileOutputStream("D:\\tempsxssf.xlsx");
wb.write(out);
out.flush();
out.close();
wb.dispose();
I am in a big trouble, can someone help me to figure out the issue?
I used another constructor according POI documentation
SXSSFWorkbook(workbook, rowAccessWindowSize, compressTmpFiles, useSharedStringsTable)
Like this:
SXSSFWorkbook workbook = new SXSSFWorkbook(null, 1000, true, true);
where you can enable shared strings table
I have the following problem using Apache POI v3.12:
I need to use a XLSX file with 49 rows [0..48] as a template, fill it's cells with data and write it out as a different file, so I can reuse the template again. What I am doing is approximately this:
XSSFWorkbook wbk_template = new XSSFWorkbook(new FileInputStream (f_wbk_template));
SXSSFWorkbook wbk = new SXSSFWorkbook(wbk_template, 50, true);
Sheet sheet = wbk.getSheet(STR_SHEET_NAME);
/ later on/
Row row = sheet.getRow(rownum);
if (null == row) {
row = sheet.createRow(rownum);
}
Upon debugging it turns out that getRow() returns null, but the attempt to .createRow() fails with:
java.lang.IllegalArgumentException: Attempting to write a row[2] in the range [0,48] that is already written to disk.
at org.apache.poi.xssf.streaming.SXSSFSheet.createRow(SXSSFSheet.java:122)
...
am I missing something here? As far as I have read in the Apache docs and forums, I need to createRow() if getRow() returns null. The sheet does not contain any rows according to .getPhysicalRows(), .getFirstRowNum() and .getLastRowNum()
Thanks.
See the documentation for the SXSSFWorkbook constructor that takes the XSSFWorkbook as param. You cannot override or access the initial rows in the template file. You are trying to overwrite an existing row and the API does not support this. Your exception message reflects this.
https://poi.apache.org/apidocs/org/apache/poi/xssf/streaming/SXSSFWorkbook.html#SXSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook)
For your use case, you may want to try http://jxls.sourceforge.net.
If you want to read or edit an exist row, you can firstly do it in xssf type, and then create the sxssf file base on the xssf file.
The code is something like below...
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(file));
//do the read and edit operation with xssf......
......
......
SXSSFWorkbook sXSSFbook = new SXSSFWorkbook(xssfWorkbook);
//do the write operation with sxssf......
......
......
Please have a look at this constructor arguments
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(exportExcelTo));
SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(workbook, -1, Boolean.FALSE, Boolean.TRUE);
I have generated an excel (.xls) document using Apache POI. I use HSSF classes.
Could someone advise me on the following question:
How can we keep the column headers in the diaplay part constant and have scroll only on the data part?
That is, I want the header columns to be always visible when the excel file is scrolled down.
Thanks,
David
One option may be the repeating rows suggestion from timboo. Not sure on your exact needs, but it's possible you might want a Freeze Pane instead.
To have the first row always on screen, never scrolling off, you'd do:
Workbook wb = new HSSFWorkbook();
Sheet sheet1 = wb.createSheet("No Scroll");
sheet1.createFreezepane(0,1);
You can freeze rows, columns or both, see this javadoc or this for details
Check out the Apache POI Busy Developers Guide:
It's possible to set up repeating rows and columns in your printouts
by using the setRepeatingRowsAndColumns() function in the HSSFWorkbook
class.
This function Contains 5 parameters. The first parameter is the index
to the sheet (0 = first sheet). The second and third parameters
specify the range for the columns to repreat. To stop the columns from
repeating pass in -1 as the start and end column. The fourth and fifth
parameters specify the range for the rows to repeat. To stop the
columns from repeating pass in -1 as the start and end rows.
Workbook wb = new HSSFWorkbook();
Sheet sheet1 = wb.createSheet("new sheet");
Sheet sheet2 = wb.createSheet("second sheet");
// Set the columns to repeat from column 0 to 2 on the first sheet
wb.setRepeatingRowsAndColumns(0,0,2,-1,-1);
// Set the the repeating rows and columns on the second sheet.
wb.setRepeatingRowsAndColumns(1,4,5,1,2);
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
This should help you. I've searched for days and found this. This solved my problem.
`Workbook wb = new XSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet1 = wb.createSheet("new sheet");
Row row = sheet1.createRow((short) 0);
// Create a cell and put a value in it.
row.createCell(0).setCellValue(
createHelper.createRichTextString("Freeze"));
for (int i = 1; i < 20; i++) {
row = sheet1.createRow((short) i);
row
.createCell(0)
.setCellValue(
createHelper
.createRichTextString("This is the Moving Row"));
}
// Freeze just one row
sheet1.createFreezePane(0, 1, 0, 1);`