I'm trying to change value of cell in .xls document.
In .xls file i have got only 1 cell - A1 with abc value inside.
My code:
File fo = new File("D:\\TMP\\Zeszyt1.xls");
HSSFWorkbook a = new HSSFWorkbook(new FileInputStream(fo));
HSSFSheet my_sheet = a.getSheetAt(0);
HSSFRow my_row = my_sheet.getRow(0);
HSSFCell myCell;
myCell = my_row.getCell(0);
myCell.setCellValue("NEW VALUE");
How to commit this changes? When i open .xls file i still have got abc value inside A1.
You have to write to the file.
FileOutputStream outputStream = new FileOutputStream(new File("abc.xls"));
workbook.write(outputStream);
outputStream.close();//Close in finally if possible
Related
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
I want to clear contents of 3 columns in excel sheet(.xlsx file) using java before execution of my selenium script. i am using XSSFWorkbook(Apache poi) for read and write data to excel sheet.There are 5 columns in excel sheet.I want to clear 3 column contents and remaining column content should keep like before.
please help me how can i achieve that?
FileInputStream objFileInputStream = new FileInputStream(new File(
Xlsx_File_path.xlsx));
XSSFWorkbook objWorkbook = new XSSFWorkbook(objFileInputStream);
XSSFSheet objSheet = objWorkbook.getSheetAt(0);
Cell cell = null;
int row=[start of row]
int cell=[your cell]
//iterate the below code as you want
//code
cell = objSheet.getRow(rows).getCell(cell);
cell.setCellValue("")
//code
objFileInputStream.close();
FileOutputStream output_file = new FileOutputStream(new File(
finalXlsx.xlsx));
objWorkbook.write(output_file);
output_file.close();
I simply want to put the result of a Java program into an Excel cell. Let's assume the result is 5, then my code in main() would look like this:
XSSFWorkbook wb = C:\Users\Username\Desktop\java-programs\results.xlsm;
XSSFSheet ExcelSheet = wb.getSheet("numbers");
XSSFRow row = ExcelSheet.getRow(0);
XSSFCell cell = row.getCell(0);
cell.setCellValue(5);
However, this code doesn't compile.
I read some examples on this topic but the code given simply refers to the Excel sheet as (e.g.) "sheet" and then goes on with sheet.getRow(some number). The examples don't explain how I tell Java which workbook to write to.
Your first line is incorrect.. try this:
String fileName = "C:/Users/Username/Desktop/java-programs/results.xlsm";
FileInputStream fileIn = new FileInputStream(fileName);
Workbook wb = WorkbookFactory.create(fileIn);
// update the workbook
wb.getSheet("numbers").getRow(0).getCell(0).setCellValue(5);
fileIn.close();
FileOutputStream fileOut = new FileOutputStream(fileName);
wb.write(fileOut);
fileOut.close();
I have xlsx document with one sheet.
In cell 'A2' i have a reference to cell 'A1'.
The following code writes data to cell 'A1'.
When I open the document, cell 'A2' is not updated with value of cell 'A1' automatically.
I am required to click on cell 'A2' and press enter, only then I see the new value.
What can be done so when document is opened the value will be updated?
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
XSSFWorkbook book = null;
String path = "c://temp//doc.xlsx";
inputStream = new FileInputStream(path);
book = new XSSFWorkbook(inputStream);
XSSFSheet sheet = book.getSheetAt(0);
XSSFRow row = sheet.getRow(0);
row.createCell(0).setCellValue("VALUE");
outputStream = new FileOutputStream(path);
book.write(outputStream);
outputStream.close();
I'm reading the xls file as per below:
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileName));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow rowTPNB;
HSSFCell cellTPNB;
rowTPNB = sheet.getRow(5);
cellTPNB = rowTPNB.getCell(1);
With that code i'm getting the value from excel which like this 76653764 to 7.6653764E7. How do I do to remain value to 76653764. Please advice
You can do it this way
Double.valueOf("7.6653764E7").longValue();
This will return a long value and remove the scientific notation :)