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 Have a data stored in variables and then i want to write my data to excel file(.xlsx).
(i.e) I use automation testing tools like selenium to get data from webpage and i store it in variable which i want to wrie in xlsx file
After a lot of google search I found many of users uses list or objects to write into .xlsx file.
I created a list and add my variable to that list and using looping statements (for loop) i checked whether my data is stored in list by printing it.
Then I created XSSFWorkbook and XSSFSheet and XSSFRow and XSSFCell to write data.
I write a cell by using setCellValue method to my cell.
My code successfully creates a xlsx file and sheet in it
but after execution i could not able to find any data in it.
Source code:
ArrayList<String> head = new ArrayList<String>();
head.add("Register Number");
head.add(subject1);
head.add(subject2); //subject1 and subject2 are variable i created
System.out.println(head.get(1)); //To check if my list has value
XSSFWorkbook workbook = new XSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("/home/st.xlsx");
for (int i = 0; i < head.size(); i++)
{
XSSFRow Row = sheet1.createRow(1);
XSSFCell cell = Row.createCell(1);
cell.setCellValue(head.get(1));
sheet1.autoSizeColumn(1);
}
workbook.write(fileOut);
fileOut.close();
I expect my code add data to my file.
Main thing is During execution when i try to open my .xlsx file it has data in it.
But after the complete execution i get with the empty xlsx file.
I don't know why i'm getting this and What wrong in my code?
Thanks in advance!
ArrayList<String> head = new ArrayList<String>();
head.add("Register Number");
head.add(subject1);
head.add(subject2); // subject1 and subject2 are variable i created
System.out.println(head.get(0)); // To check if my list has value
System.out.println(head.get(1)); // To check if my list has value
System.out.println(head.get(2)); // To check if my list has value
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet1 = wb.createSheet("Sheet1");
for (int r = 0; r < head.size(); r++)
{
XSSFRow row = sheet1.createRow(r);
XSSFCell cell = row.createCell(0);
cell.setCellValue(head.get(r));
sheet1.autoSizeColumn(0);
}
// Write this workbook to a FileOutputStream.
FileOutputStream fileOut = new FileOutputStream("/home/st.xlsx");
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
More info here:
https://gist.github.com/madan712/3912272
As per the requirement, a web form needs to be filled by taking values from a excel sheet. After filling in all the values, a numeric value gets auto populated in one of the fields in the same web form.
It is numeric value from just one web field that needs to be written in to the same excel sheet that was used to read values, code is as below:
FileInputStream file = new FileInputStream(new File(".xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet2 = workbook.getSheetAt(0);
XSSFSheet sheet3 = workbook.getSheetAt(1);
DataFormat format = workbook.createDataFormat();
String NAME = sheet2.getRow(2).getCell(1).getStringCellValue();
driver.findElement(By.id("##")).sendKeys(NAME);
like wise many cells are filled.
after that to get value from webpage into excel sheet:
String value = driver.findElement(By.id("!!")).getAttribute("value");
System.out.println(value);
The value is correctly getting printed.
using the below code the value is not getting displayed
CreationHelper createHelper = workbook.getCreationHelper();
XSSFSheet sheet4 = workbook.createSheet("new sheet");
Row row1 = sheet4.createRow((short)0);
// Create a cell and put a value in it.
Cell cell = row1.createCell(0);
cell.setCellValue(value);
I need to merge two sheets into one.
Eg. I have excel file test.xls with two sheets, sheet0 and sheet1. Both sheets have some text and table.
I would like to merge them so merged sheet would look like this:
text from sheet0
table from sheet0
text from sheet1
table from sheet1
I need to do this in Java.
Is there a simple way to do this?
Something like :
HSSFWorkbook book = new HSSFWorkbook("/tmp/test.xls");
HSSFSheet sheet0 = book.getSheetAt(0);
HSSFSheet sheet1 = book.getSheetAt(1);
sheet0.merge(sheet1); //or combine or something
Didn't find such method in Poi API
Otherwise, you can manually append the content of sheet1 into sheet0 like :
int lastRowNum1 = sheet1.getLastRowNum();
int i=0;
int currentLinePos=sheet0.getLastRowNum();
while ( i <= lastRowNum1 ){
Row currentRow = sheet1.getRow(i++);
Row copiedRow = sheet0.createRow(currentLinePos++);
// code that copy the content of currentRow into copiedRow
// such as copying every cells
// or try copiedRow = currentRow; but not sure it will copy the cells
}
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);`