Read Column values from excel file using selenium java - java

I am trying to read column values from excel file which looks like this
This is my code for reading excel file
Xls_Reader d = new Xls_Reader("C:\\TestData.xlsx");
System.out.println(d.getRowCount("sheetname"));
String s1 = String.valueOf(d.getCellData("TC03", "Contract_ID",3));
s1 =(int)Double.parseDouble(s1) + "";
System.out.println(s1);
in this code i am able to get record of one row. But i need to print all the Row value.
Please suggest.

What Jars you are using for this approach?

I have following code to read data from excel using apache poi -
public static void main(String args[]) throws IOException
{
FileInputStream fis = new FileInputStream(new File("your_file.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(fis); // XSSFWorkbook for .xlsx file
XSSFSheet sheet = workbook.getSheetAt(0); // open sheet 1
Iterator<Row> rowIterator = sheet.iterator();
// Traversing over each row of XLSX file
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
if(row.getRowNum()!=0) // skip title row
{
Iterator cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
Cell cell = (Cell) cellIterator.next();
System.out.print(cell.getStringCellValue() + "\t");
}
}
}
}
Note :- In my case cell in excel sheet formatted as text you can change cell.getStringCellValue() method as per your data.

This method will be print all sheet of your excel document:
public static void GetEmail() throws IOException{
InputStream in = new FileInputStream("C:/path to your doc");
HSSFWorkbook wb = new HSSFWorkbook(in);
ExcelExtractor extractor = new ExcelExtractor(wb);
extractor.setFormulasNotResults(false); // Read formulas
extractor.setIncludeSheetNames(false);
String text = extractor.getText();
System.out.println(text);
}

Related

How to add columns to an existing large excel file using SXSSF Apache POI?

I am working with a large excel file ( larger than 40 Mb , more than 100k rows and 50 columns ). I am successfully reading it using POI ( 3.10.1 version ) event stream and then doing some calculation and storing result into a List.
Now I have to append this List as a column in the same file. In this part I am facing issue.
I have tried to achieve this by using the below code
FileInputStream excelFile = new FileInputStream(new File(pathToFile));
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0); // Get first sheet
Iterator<Row> iterator = datatypeSheet.iterator();
int i=0;
while (iterator.hasNext()) { // Loop over each row
Row currentRow = iterator.next();
Cell cell = currentRow.createCell(currentRow.getLastCellNum());
cell.setCellType(Cell.CELL_TYPE_STRING);
if(currentRow.getRowNum() == 0)
cell.setCellValue("OUTPUT-COLUMN"); // set column header for the new column
else {
cell.setCellValue(list.get(i)); // list contains the output to populate in new column
i++;
}
}
FileOutputStream fos = new FileOutputStream(new File(pathToOutput));
workbook.write(fos);
fos.close();
It is working fine with smaller files But the issue is that I am getting Out of memory for the larger files. Now I tried to modify this and use SXSSF in place of XSFF to get over the memory issue (See below code). But while testing even for smaller files I am getting output file same as the input file.
FileInputStream excelFile = new FileInputStream(new File(pathToFile));
XSSFWorkbook xwb = new XSSFWorkbook(inputStream);
inputStream.close();
SXSSFWorkbook wb = new SXSSFWorkbook(xwb,100);
wb.setCompressTempFiles(true);
SXSSFSheet sh = (SXSSFSheet) wb.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
int i=0;
while (iterator.hasNext()) { // Loop over each row
Row currentRow = iterator.next();
Cell cell = currentRow.createCell(currentRow.getLastCellNum());
cell.setCellType(Cell.CELL_TYPE_STRING);
if(currentRow.getRowNum() == 0)
cell.setCellValue("OUTPUT-COLUMN"); // set column header for the new column
else {
cell.setCellValue(list.get(i)); // list contains the output to populate in new column
i++;
}
}
FileOutputStream fos = new FileOutputStream(new File(pathToOutput));
wb.write(fos);
fos.close();
Using a db is not suitable in my use case and i want to avoid using a temporary data structure to hold data for writing due to memory constraint.
Is there a way to write in output workbook while streaming ? Here is the code that I am using to read using POI Streaming API
private class ExcelData implements SheetContentsHandler {
LinkedHashMap<Strin, String> rowMap;
public void startRow(int rowNum) {
}
public void endRow(int rowNum) {
// Process the row
// Handle write to output workbook ??
}
public void cell(String cellReference, String formattedValue,
XSSFComment comment) {
// Save current row in rowMap ( column name => cell value )
}
public void headerFooter(String text, boolean isHeader, String tagName)
{
}
}
It is not possible to add column to existing workbook using POI SXSSF. It only allows addition of new rows.
The only solution is to read the existing workbook and write to a new workbook with the added column.
To achieve this we can store the rows in a data structure or database in the endrow() method and then use the persisted data to write a new workbook.

how to get all values with poi excel and java

Hello with this code i just get the first line in my database
i need to gett all line please what a should to do
I'm using Apache POI to read an Excel document. and i need to stock all data in my database but with this code i just save the first line please what i should to do !
how to do read all line in my excel thanks
#PostMapping("/upload")
public ResponseEntity<?> addRfp (#RequestParam("file") MultipartFile file) throws IOException, InvalidFormatException {
ZipSecureFile.setMinInflateRatio(0);
FileInputStream fis = (FileInputStream) file.getInputStream();
XSSFWorkbook myWorkBook = new XSSFWorkbook (fis);
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
Iterator<Row> rowIterator = mySheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
final DataFormatter df = new DataFormatter();
Rfp rfp = new Rfp();
for(int i =0 ; i < mySheet.getPhysicalNumberOfRows() ; i++)
{
Row nextRow = mySheet.getRow(row.getRowNum() + 1);
if (nextRow != null)
{
System.out.println(df.formatCellValue(row.getCell(3)));
rfp.setIde(df.formatCellValue(row.getCell(0)));
rfp.setUnit(df.formatCellValue(row.getCell(1)));
rfp.setRFPID(df.formatCellValue(row.getCell(2)));
rfp.setREQUESTID(df.formatCellValue(row.getCell(3)));
rfp.setINCOMING(df.formatCellValue(row.getCell(4)));
rfp.setPROJECTTITLE(df.formatCellValue(row.getCell(5)));
rfp.setSERVICELINE(df.formatCellValue(row.getCell(6)));
rfp.setBUSINESSUNIT(df.formatCellValue(row.getCell(7)));
rfp.setREQUSITIONBUSINESSUNIT(df.formatCellValue(row.getCell(8)));
rfp.setCOUNTRY(df.formatCellValue(row.getCell(9)));
rfp.setTARGETPROPOSE(df.formatCellValue(row.getCell(10)));
rfp.setRFX(df.formatCellValue(row.getCell(11)));
rfp.setWORKFLOWTRACKING(df.formatCellValue(row.getCell(12)));
rfp.setPROTOCOL(df.formatCellValue(row.getCell(13)));
rfp.setJOURFIXKUNDE(df.formatCellValue(row.getCell(14)));
rfp.setREJECTEDSTATUS(df.formatCellValue(row.getCell(15)));
rfp.setCLOSEDREASON(df.formatCellValue(row.getCell(16)));
rfp.setPRACTICE(df.formatCellValue(row.getCell(17)));
rfp.setSTARTDATE(df.formatCellValue(row.getCell(18)));
rfp.setENDDATE(df.formatCellValue(row.getCell(19)));
rfp.setVERTRAGSART(df.formatCellValue(row.getCell(20)));
rfp.setRFPCOMMENT(df.formatCellValue(row.getCell(21)));
rfp.setPERSONDAYS(df.formatCellValue(row.getCell(22)));
rfp.setACCOUNTEXECUTIVE(df.formatCellValue(row.getCell(23)));
rfp.setACCOUNTSALESMANAGER(df.formatCellValue(row.getCell(24)));
rfp.setEXPERTSALES(df.formatCellValue(row.getCell(25)));
rfp.setDELIVERYUNIT(df.formatCellValue(row.getCell(26)));
rfp.setDELIVERYRESPONSIBLE(df.formatCellValue(row.getCell(27)));
rfp.setREQUISITIONER(df.formatCellValue(row.getCell(28)));
rfp.setPROCUREMENT(df.formatCellValue(row.getCell(29)));
rfp.setREFERENCENUMBER(df.formatCellValue(row.getCell(30)));
rfp.setATOSCONTRACTNO(df.formatCellValue(row.getCell(31)));
rfp.setCANDIDATES(df.formatCellValue(row.getCell(32)));
rfp.setITEC(df.formatCellValue(row.getCell(33)));
rfp.setTCV(df.formatCellValue(row.getCell(34)));
rfp.setUPDATED(df.formatCellValue(row.getCell(35)));
rfp.setUPDATEDBY(df.formatCellValue(row.getCell(36)));
rfp.setPORTFOLIO(df.formatCellValue(row.getCell(37)));
rfp.setPATH(df.formatCellValue(row.getCell(38)));
rfp.setRMPROTOCOL(df.formatCellValue(row.getCell(39)));
rfp.setRM(df.formatCellValue(row.getCell(40)));
rfp.setWFM_ID(df.formatCellValue(row.getCell(41)));
rfp.setNEEDCONFOPART(df.formatCellValue(row.getCell(42)));
rfp.setCONFIRMATIONOFPARTICIPATION(df.formatCellValue(row.getCell(43)));
rfp.setPROPOSE(df.formatCellValue(row.getCell(44)));
rfp.setLASTCALL(df.formatCellValue(row.getCell(45)));
rfp.setLASTCALLDATE(df.formatCellValue(row.getCell(46)));
rfp.setSKILLS(df.formatCellValue(row.getCell(47)));
rfp.setLEISTUNGSERBRINGUNG(df.formatCellValue(row.getCell(48)));
rfp.setROLLEFUNKTION(df.formatCellValue(row.getCell(49)));
rfp.setINHALTSCOPE(df.formatCellValue(row.getCell(50)));
rfp.setLAUFZEITPERSPEKTIVE(df.formatCellValue(row.getCell(51)));
rfp.setLAUFZEITANGEFRAGT(df.formatCellValue(row.getCell(52)));
rfp.setPHASE(df.formatCellValue(row.getCell(53)));
rfp.setFRISTVERLAENGERUNG(df.formatCellValue(row.getCell(54)));
rfp.setCLOSEDSTATUS(df.formatCellValue(row.getCell(55)));
rfp.setDEFERRED_TILL(df.formatCellValue(row.getCell(56)));
rfp.setSIPA_YN(df.formatCellValue(row.getCell(57)));
rfp.setPO_NO(df.formatCellValue(row.getCell(58)));
rfp.setNESSIE_NO(df.formatCellValue(row.getCell(59)));
rfp.setNUMBER_OF_PERSONS(df.formatCellValue(row.getCell(60)));
rfp.setATT_JOB_SITE(df.formatCellValue(row.getCell(61)));
rfp.setATT_LANGUAGE(df.formatCellValue(row.getCell(62)));
rfp.setATT_MAIN_TOPICS(df.formatCellValue(row.getCell(63)));
rfp.setREF_RFPID(df.formatCellValue(row.getCell(64)));
rfp.setREF_HISTORY(df.formatCellValue(row.getCell(65)));
rfp.setREF_REJECTIONS(df.formatCellValue(row.getCell(66)));
rfp.setRV_REJ_STATUS_DETAILS(df.formatCellValue(row.getCell(67)));
rfp.setRV_ALLIANCE_MANAGER(df.formatCellValue(row.getCell(68)));
rfp.setRV_FINAL_STATUS(df.formatCellValue(row.getCell(69)));
rfp.setRV_CUST_LIST_DATE(df.formatCellValue(row.getCell(70)));
rfp.setRV_FINAL_NOTE(df.formatCellValue(row.getCell(71)));
rfp.setRV_ATOS_ROOT_CAUSES(df.formatCellValue(row.getCell(72)));
rfp.setCURRENCY(df.formatCellValue(row.getCell(73)));
rfp.setEXTERNALID(df.formatCellValue(row.getCell(74)));
rfp.setRESULTSTATUS(df.formatCellValue(row.getCell(75)));
rfp.setREASONCODE(df.formatCellValue(row.getCell(76)));
rfp.setSYSID(df.formatCellValue(row.getCell(77)));
rfp.setNCRM_COMMENT(df.formatCellValue(row.getCell(78)));
rfp.setIDEMAND_COMMENT(df.formatCellValue(row.getCell(79)));
rfp.setOFFSHORE_COMMENT(df.formatCellValue(row.getCell(80)));
rfp.setRV_RC_UPDBY(df.formatCellValue(row.getCell(81)));
rfp.setRV_RC_DATE(df.formatCellValue(row.getCell(82)));
rfp.setRV_FINALCOMMENT(df.formatCellValue(row.getCell(83)));
rfp.setRV_FINALDATE(df.formatCellValue(row.getCell(84)));
rfp.setTCV_EURO(df.formatCellValue(row.getCell(85)));
rfp.setREM_ACTIVE(df.formatCellValue(row.getCell(86)));
rfp.setREM_SEND(df.formatCellValue(row.getCell(87)));
rfp.setREMCOP_ACTIVE(df.formatCellValue(row.getCell(88)));
rfp.setREMCOP_SEND(df.formatCellValue(row.getCell(89)));
rfp.setIDEMAND(df.formatCellValue(row.getCell(90)));
rfp.setIDEMAND_URL(df.formatCellValue(row.getCell(91)));
rfp.setLABOUR_LEASING_YN(df.formatCellValue(row.getCell(92)));
rfp.setCAS_NO(df.formatCellValue(row.getCell(93)));
rfp.setPROLONGATION(df.formatCellValue(row.getCell(94)));
rfp.setRV_SKILLS_MISS_1(df.formatCellValue(row.getCell(95)));
rfp.setRV_SKILLS_MISS_2(df.formatCellValue(row.getCell(96)));
rfp.setRV_SKILLS_MISS_3(df.formatCellValue(row.getCell(97)));
rfp.setREF_RENEWAL_DATE(df.formatCellValue(row.getCell(98)));
rfprepository.save(rfp);
}
}}
}
return null;
}}
I use to get all rows this way.
FileInputStream fis = (FileInputStream) file.getInputStream();
XSSFWorkbook myWorkBook = new XSSFWorkbook (fis);
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
for(Row row : mySheet){
final DataFormatter df = new DataFormatter();
System.out.println(df.formatCellValue(row.getCell(0)));
}

How to iterate over current row in an excel sheet using column name?

I need to parse over an excel sheet and retrieve values from each row to store it in database. Currently I am doing it based on the type of values that each cell holds. This is ok in the current case as I have to deal with only 2 columns. But I have a new requirement to parse an excel sheet that holds more than 12 columns. How can it be done in this case? Is there a way I could iterate each row based on column if I am using a structured table with table headers?
My current code is as follows.
File file = new File(UPLOAD_LOCATION + fileUpload.getFile().getOriginalFilename());
FileInputStream excelFile = new FileInputStream(file);
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
// getCellTypeEnum shown as deprecated for version 3.15
// getCellTypeEnum ill be renamed to getCellType starting
// from version 4.0
if (currentCell.getCellTypeEnum() == CellType.STRING) {
System.out.print(currentCell.getStringCellValue() + "--");
} else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
System.out.print(currentCell.getNumericCellValue() + "--");
}
}
I am using the following external apache API imports:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
Is there a way I can do the same passing in the name of column headers?
Please help.
Thanks in advance.
based on the comments
InputStream excelFile = new FileInputStream(file);
Workbook workbook = new XSSFWorkbook(excelFile);
ArrayList colsList=new ArrayList();
colsList.add("Col1");
colsList.add("Col2");
colsList.add("Col3");
colsList.add("Col4");
Sheet datatypeSheet = workbook.getSheetAt(0);
int numOfRows=datatypeSheet.getLastRowNum();
for(int rowNum=0;rowNum<numOfRows;rowNum++){
Row row=datatypeSheet.getRow(rowNum);
int numOfCellPerRow=row.getLastCellNum();
for(int cellNum=0;cellNum<numOfCellPerRow;cellNum++){
if(colsList.contains(row.getCell(rowNum).getStringCellValue())){
Cell cell=row.getCell(cellNum)
System.out.println("Cell No:"+cellNum+" value is:
"+cell.getStringCellValue())
}
}
System.out.println("This is a new Row");
}

Updating value of cells in Excel with Java Apache POI

I am trying to update an existing Excel with Apache POI.
The thing is, I have an excel having in its 3rd column telephone numbers, and I want to correct them to the specific format using if-statements.
But I cannot write to the file.
Here is the code I am using:
public void readingExcel(String fileName) throws Exception {
try (FileInputStream file = new FileInputStream(new File(fileName))) {
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
int counter = 0;
for (Row row : sheet) {
int columnIndex = 2;
row = CellUtil.getRow(counter, sheet);
Cell cell = CellUtil.getCell(row, columnIndex);
if (formatter.formatCellValue(cell).length() == 8)
cell.setCellValue("+216" + formatter.formatCellValue(cell));
if (formatter.formatCellValue(cell).length() == 11 && formatter.formatCellValue(cell).startsWith("216"))
cell.setCellValue("+" + formatter.formatCellValue(cell));
counter++;
}
FileOutputStream outFile =new FileOutputStream(new File(fileName));
workbook.write(outFile);
outFile.close();
}
}
And I am getting this error
org.apache.poi.poifs.filesystem.NotOLE2FileException: Invalid header signature; read 0x0000000000000000, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:181)
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:140)
at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:302)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:398)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:379)
at excelapp.myClass.readingExcel(myClass.java:50)
at excelapp.myClass.<init>(myClass.java:41)
at excelapp.ExcelApp.main(ExcelApp.java:26)
The thing is that it is really important for today, and I am getting really stuck on updating the values of that column :-/ anyone has an idea? I think I'm writing the wrong way..

The process cannot access the file because it is being used by another process

I am trying to read data from one sheet of excel file and write to other sheet of same excel file.I have tried this :
FileInputStream file = new FileInputStream(new File("E:\\excel\\input.xlsx"));
//Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);
//Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
//Check the cell type and format accordingly
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
System.out.print((int)cell.getNumericCellValue()+ " ");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue());
break;
}
}
System.out.println("");
}
file.close();
CreationHelper createHelper = workbook.getCreationHelper();
XSSFSheet newSheet = workbook.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = newSheet.createRow((short)0);
// Create a cell and put a value in it.
Cell cell = row.createCell(0);
cell.setCellValue(1);
// Or do it on one line.
row.createCell(1).setCellValue(1.2);
row.createCell(2).setCellValue(
createHelper.createRichTextString("This is a string"));
row.createCell(3).setCellValue(true);
System.out.println("writing to file");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream(new File("E:\\excel\\input.xlsx"));
workbook.write(fileOut);
fileOut.close();
I have successfully read data from excel sheet But I am getting exception while writing in new sheet given below
java.io.FileNotFoundException: E:\excel\input.xlsx (The process cannot access the file because it is being used by another process)
This will happen when you are trying to execute this program while the "input.xlsx" file is already opened by another application.
Please close any instance of MS Excel and try to run it again
Just a guess but XSSFWorkbook should be implementing Closeable thus it has a .close() that should be called.
Maybe the file result still in use because of that?
You are probably going to need a middle step there: write to a new file, close the XSSFWorkbook and then copy the new file on the old one that you want to write on

Categories

Resources