I want to compare two excel sheet data with same column name, excel sheet having same column name but position of column name will be different. So, I need logic first check column name and compare data
below code is working if both the sheet column name have same position or index.
public static void main(String[] args) throws IOException {
// Workbook1 Code
FileInputStream filepath1 = new FileInputStream(".\\DataFile\\Book1.xlsx");
XSSFWorkbook workbook1 = new XSSFWorkbook(filepath1);
XSSFSheet worksheet1 = workbook1.getSheet("sheet1");
int rowcount1 = worksheet1.getPhysicalNumberOfRows();
int CellCount=worksheet1.getRow(0).getPhysicalNumberOfCells();
System.out.println("row count of worbook1:" + rowcount1);
// Workbook2 Code
FileInputStream filepath2 = new FileInputStream(".\\DataFile\\Book2.xlsx");
XSSFWorkbook workbook2 = new XSSFWorkbook(filepath2);
XSSFSheet worksheet2 = workbook2.getSheet("sheet1");
int rowcount2 = worksheet2.getPhysicalNumberOfRows();
System.out.println("row count of worbook2:" + rowcount2);
if (rowcount1 == rowcount2) // condition if rowcount1 is equal to row count of worbook2 are same
{
for (int i = 0; i < rowcount1; i++) {
for(int j=0; j<CellCount; j++)
{
XSSFCell row1 = worksheet1.getRow(i).getCell(j);
XSSFCell row2 = worksheet2.getRow(i).getCell(j);
row1.setCellType(CellType.STRING);
String cellvalue=row1.getStringCellValue();
row2.setCellType(CellType.STRING);
String cellvalue2=row2.getStringCellValue();
if(!cellvalue.equals(cellvalue2))
{
System.out.println("Row number:"+i+ " Column number:"+j+" [ERROR] :" + "Diference in data " +"| Book1 id = " + cellvalue + " | Book2 id = " + cellvalue2);
}
}}}
I am inserting a list of numbers into a column in Excel (column A) with Java. the following column has a list of average numbers (Column B). Column C has a list of formulas like this: =IF(B1 < 1,0,((A1)-B1)/B1).
In my program, I want to have the formulas in column C re-evaluate after I've input the new data so then I can extract the new cell value into Java. I'm checking to see if any new result in column C is -1.0.
I've tried to use FormulaEvaluator before extracting column C values but I still get the old values. I've also tried writing and closing the workbook and opening it again but that also hasn't worked.
//Pull the values stored procedure
ResultSet rs = dbutils.sqlserverdbvaliation(query, db);
System.out.println("Returned the Result Set");
//Get number of rows in result row to set array size
rs.last();
arraySize = rs.getRow();
rs.beforeFirst();
//Store values from resultSet into an array
int[] dlArray = new int[arraySize];
while(rs.next()) {
//Activity Process Count is name of ResultSet Column
dlArray[count] = rs.getInt("ActivityProcessCount");
count++;
}
//Create workbook
InputStream inp = new FileInputStream("C:\\TestWorkbook.xlsx");
Workbook wb = WorkbookFactory.create(inp);
org.apache.poi.ss.usermodel.Sheet sheet = wb.getSheetAt(1);
FormulaEvaluator evaluator =
wb.getCreationHelper().createFormulaEvaluator();
evaluator.evaluateAll();
System.out.println(Arrays.toString(dlArray));
//Write the values from the array to the spreadsheet
int excelCount = 1;
for (int i = 0; i < arraySize ; i++) {
Row row = sheet.getRow(excelCount);
Cell cell = row.getCell(2);
cell.setCellValue(dlArray[i]);
evaluator.evaluateAll();
excelCount++;
}
FileOutputStream fos = new FileOutputStream("C:\\TestWorkbook.xlsx");
evaluator.evaluateAll();
wb.write(fos);
wb.close();
InputStream inp2 = new FileInputStream("C:\\TestWorkbook.xlsx");
Workbook wb2 = WorkbookFactory.create(inp2);
org.apache.poi.ss.usermodel.Sheet sheet2 = wb2.getSheetAt(1);
excelCount = 1;
for (int i = 0; i < arraySize ; i++) {
Row row = sheet2.getRow(excelCount);
Cell cell = row.getCell(4);
if (cell.getNumericCellValue() == -1.0) {
ExcelUtils.setCellStyle(excelPath, sheetName, excelCount, 5,
YELLOW);
}
else {
ExcelUtils.setCellStyle(excelPath, sheetName, excelCount, 5,
GREEN);
}
excelCount++;
}
I have the code to get data from the worksheet using JXL and Java, but at the end of the process I need to get a data from the page and write to a certain cell in the worksheet.
//reading the worksheet
Workbook workbook = Workbook.getWorkbook(new File("d:/planilha.xls"));
//Indicate the opening of the first worksheet
Sheet sheet = workbook.getSheet(0);
//Count the rows
int linhas = sheet.getRows();
//Start a for loop to scan all rows in the already selected worksheet
for(int i = 1; i < linhas; i++){
//Referencing rows that have content
Cell nome_promocao = sheet.getCell(0, i);
Cell desc_promocao = sheet.getCell(1, i);
Cell tipo_promocao = sheet.getCell(2, i);
Cell dt_inicio = sheet.getCell(3, i);
Cell dt_fim = sheet.getCell(4, i);
Cell aparelho = sheet.getCell(5, i);
Cell idpromo = sheet.getCell(6, i);
//Taking the data from the worksheet
String linhanomepromocao = nome_promocao.getContents();
String linhadescpromocao = desc_promocao.getContents();
String linhatipopromocao = tipo_promocao.getContents();
String linhadtinicio= dt_inicio.getContents();
String linhadtfim = dt_fim.getContents();
String linhaaparelho = aparelho.getContents();
String linhaidpromo = idpromo.getContents();
//Other actions
//Get the id I need on the page and saving in a variable
String numberpromotion = driver.findElement(By.className("link")).getText();
//I need to record this number of the above variable in the corresponding cell (idpromo or linhaidpromo)
Does this provide answer to your question?:
Label label = new Label(6, i, numberpromotion);
sheet.addCell(label);
1) I am trying to read the values from the webtable of an appliation and write it into the excel sheet one by one.
2) There are 4 values in each row of the webtable that need to be written to excel sheet, but there are some images in each row that i'm ignoring bu using below code.
text.length()>2
.
3) There will be 200-300 rows in webtable that need to be fetched and written to excel sheet.
This is the code i have tried. But i dont know how to write it into each row of the excel sheet one by one. Please help me in this regard.
//get the table
WebElement statusTable = browser.findElement(By.id("projectstatus"));
//Get all the rows in the table
List<WebElement> allRows = statusTable.findElements(By.tagName("tr"));
//Get the size(row no) of allRows
int rowSize = allRows.size();
System.out.println(rowSize);
for (WebElement row : allRows) {
//Get all cell values in each row
List<WebElement> allCells = row.findElements(By.tagName("td"));
//System.out.println(allCells.size());
if(allCells.size() > 1){
for (WebElement cell : allCells) {
String text = cell.getText();
if(text.length()>2){
String value = cell.getText();
}
}
}
// locate the test xl file
File file = new File("e:\\Testing_emi.xls");
// create input stream
FileInputStream fis = new FileInputStream(file);
// create workbook
HSSFWorkbook wb = new HSSFWorkbook(fis);
// get sheet
HSSFSheet sheet1 = wb.getSheet("Sheet1");
// get rows
HSSFRow row = sheet1.getRow(1);
HSSFCell cellEx = row.getCell(0);
if (cellEx == null) {
cellEx = row.createCell(0);
}
cellEx.setCellValue(value);
//get the table
WebElement statusTable = browser.findElement(By.id("projectstatus"));
//Get all the rows in the table
List<WebElement> allRows = statusTable.findElements(By.tagName("tr"));
//Get the size(row no) of allRows
int rowSize = allRows.size();
System.out.println(rowSize);
// locate the test xls file
File file = new File("e:\\Testing_emi.xls");
// create input stream
FileInputStream fis = new FileInputStream(file);
// create workbook
HSSFWorkbook wb = new HSSFWorkbook(fis);
// get sheet
HSSFSheet sheet1 = wb.getSheet("Sheet1");
// get rows
HSSFRow row;
for (int i=0; i<rowSize; i++)
{
WebElement webRow = allRows.get(i);
//Get all cell values in each row
List<WebElement> allCells = webRow.findElements(By.tagName("td"));
//System.out.println(allCells.size());
if(allCells.size() > 1)
{
HSSFRow excelRow = sheet1.createRow(i);
for (int j=0; j<allCells.size(); j++)
{
WebElement webCell = allCells.get(j);
String text = webCell.getText();
if(text.length()>2)
{
Cell excelCell = excelRow.createCell();
excelCell.setValue(webCell.getText());
}
}
}
}
sheet1.close();
Read the values from the Web Table of an application and write it in to the excel sheet one by one.There are 5 values in each row of the Web Table and that need to written in Excel sheet. Below is the working code.
System.setProperty("webdriver.chrome.driver","C:\\Users\\Documents\\Selenium jars\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://cosmocode.io/automation-practice-webtable/");
WebElement table = driver.findElement(By.xpath("//*[#id='countries']/tbody"));
List<WebElement> rows = table.findElements(By.tagName("tr"));
int rowcount = rows.size();
FileOutputStream fis = new FileOutputStream(new File("C:\\Users\\Documents\\Selenium\\output.xlsx"));
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sh = wb.createSheet("First Sheet");
for (int row = 0; row < rowcount; row++) {
List<WebElement> columns_inrow = rows.get(row).findElements(By.tagName("td"));
int columns_count = columns_inrow.size();
System.out.println("Number of cells in Row " + row + " are " + columns_count);
Row ro = sh.createRow(row);
for (int column = 0; column < columns_count; column++) {
String celltext = columns_inrow.get(column).getText();
System.out.println(
"Cell Value of row number " + row + " and column number " + column + " Is " + celltext);
ro.createCell(column).setCellValue(celltext);
}
System.out.println("===========================");
}
wb.write(fis);
wb.close();
}
I have a List<String> that has few elements and I want to write these elements into an Excel sheet using Apache POI. Also I want to put the data into separate rows after each 10 items. For example, my list has following elements :
["aaa","bbb","ccc","ddd","eee","fff","ggg","hhh","iii","jjj","kkk","lll","mmm","nnn","ooo","ppp","qqq","rrr","sss","ttt",....]
I want this in excel sheet in following format
col1, col2, col3, col4, col5, col6, col7, col8, col9, col10
row1 : aaa, bbb, ccc, ddd, eee, fff, ggg, hhh, iii, jjj
row2 : kkk, lll, mmm, nnn, ooo, ppp, qqq, rrr, sss, ttt
Can this be achieved through Apache POI?
I have tried the following (I am really bad in loops)
public void csvUpdateWorksheet1(String fileName, String sheetName, List<String> data) {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(sheetName);
logger.info(data.size());
int rownum = 0;
for (int i = 0; i < data.size(); i++) {
Row row = sheet.createRow(i);
Cell cell = row.createCell(i);
cell.setCellValue(data.get(i));
}
csvWriteToFile(workbook, fileName);
}
You will need to create the next row once you have created 10 cells in the current row.
int r = 0;
int c = 0;
int maxCellsPerRow = 10;
Row row = sheet.createRow(r);
for (String str : data)
{
Cell cell = row.createCell(c);
cell.setCellValue(str);
c++;
// Create and advance to next row if needed.
if (c >= maxCellsPerRow)
{
c = 0;
r++;
row = sheet.createRow(r);
}
}
int rownum = 0;
int colnum = 0;
int itemsPerRow = 20;
String outfilename = "test.xls";
List<String> values = new ArrayList<String>();
// add some values ...
HSSFWorkbook book = new HSSFWorkbook();
HSSFSheet sheet = book.createSheet(
HSSFRow row = sheet.getRow(rownuw);
for(String s : values) {
HSSFCell cell = row.createCell(colnum);
cell.setCellValue(Integer.parseInt(id));
colnum = (column+1)%itemsPerRow;
if(colnum == 0) {
rownum++;
row = sheet.getRow(rownuw);
}
}
File f = new File("src/main/resources/films.xls");
FileOutputStream fos = new FileOutputStream(f);
book.write(fos);
fos.flush();
fos.close();