This is actually not a question but solution proposal: I found workaround to read set of data from excel. in this case there is no need for multiple users or data variation but read parameters to create a validation environment.
ok, solution is to save excel file to html format and then let the Selenium IDE to read parameters from that. Users needs only to agree the same filename to be used.
1) you should add "Apache POI" jar files in order to read your excel through java.
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelReadExample {
#SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) throws Exception {
String filename = "E:\\data.xls";
List sheetData = new ArrayList();
FileInputStream fis = null;
try {
fis = new FileInputStream(filename);
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
XSSFRow row = (XSSFRow) rows.next();
Iterator cells = row.cellIterator();
List data = new ArrayList();
while (cells.hasNext()) {
XSSFCell cell = (XSSFCell) cells.next();
data.add(cell);
}
sheetData.add(data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
}
showExelData(sheetData);
}
private static void showExelData(List sheetData) {
int sum = 0;
for (int i = 0; i < sheetData.size(); i++) {
List<XSSFCell> list = (List) sheetData.get(i);
for (int j = 0; j < list.size(); j++) {
XSSFCell cell = (XSSFCell) list.get(j);
if(cell.getCellType()==0)
{
sum += cell.getNumericCellValue();
}
}
System.out.println("");
System.out.println("Sum Value is:" +sum);
}
}
}
Change the file path.
i hve mentioned my sheet name as "input" change it as per yours
Happy excelling :D
Related
I've a simple method to read csv and convert it to Excel:
public static void main(String[] args) throws Exception {
CSVReader csvReader = new CSVReader(new FileReader("P:\\employees.csv"));
SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
SXSSFSheet sxssfSheet = sxssfWorkbook.createSheet("Sheet");
String[] dataRow = null;
int rowNum = 0;
while ((dataRow = csvReader.readNext()) != null) {
Row currentRow = sxssfSheet.createRow(rowNum);
for (int i = 0; i < dataRow.length; i++) {
String cellValue = dataRow[i];
currentRow.createCell(i).setCellValue(cellValue);
}
rowNum++;
}
sxssfWorkbook.write(new FileOutputStream("P:\\employees.xlsx"));
}
But there's a problem with cell data type. All my data now represents as text. I want to find columns by their name (for example age, paid_total), not by index, and set numeric (float) data type for these columns. Something like this (sorry for sql-like style, for me it's a simplier to describe): WHEN columnName IN ('age', 'paid_total') SET allColumnType AS NUMERIC. How can I do this? Or it's only possible with indexes?
CSV files always are plain text files without data types. But if you exactly know which column should be which data type, then a type safe Excel sheet can be created. This can be achieved by column indes as well as by column header. To detect types by column header, those headers wolud must be into a separate data structure. But this will always be benefical.
Let's take the example employees.csv from here: https://gist.github.com/kevin336/acbb2271e66c10a5b73aacf82ca82784.
Then following should work:
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.xssf.streaming.*;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
import com.opencsv.CSVReader;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeFormatter;
import java.time.LocalDate;
class CreateExcelFromCSVDifferentDataTypes {
public static void main(String[] args) throws Exception {
try (
SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(); FileOutputStream fileout = new FileOutputStream("./employees.xlsx");
CSVReader csvReader = new CSVReader(new FileReader("./employees.csv"));
) {
sxssfWorkbook.setCompressTempFiles(true);
CellStyle dateStyle = sxssfWorkbook.createCellStyle();
dateStyle.setDataFormat(sxssfWorkbook.getCreationHelper().createDataFormat().getFormat("dd-MMM-yy"));
SXSSFSheet sxssfSheet = sxssfWorkbook.createSheet("Sheet");
sxssfSheet.setRandomAccessWindowSize(100);
String[] strHeaders = null;
String[] dataRow = null;
int rowNum = 0;
while ((dataRow = csvReader.readNext()) != null) {
if (rowNum == 0) strHeaders = dataRow;
Row currentRow = sxssfSheet.createRow(rowNum);
for (int i = 0; i < dataRow.length; i++) {
String cellValue = dataRow[i];
if (rowNum > 0 && "HIRE_DATE".equals(strHeaders[i])) {
DateTimeFormatter formatter= new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("dd-MMM-yy").toFormatter(java.util.Locale.ENGLISH);
LocalDate localDate = LocalDate.parse(cellValue, formatter);
currentRow.createCell(i).setCellValue(localDate);
currentRow.getCell(i).setCellStyle(dateStyle);
} else if (rowNum > 0 && "SALARY".equals(strHeaders[i])) {
double d = Double.valueOf(cellValue);
currentRow.createCell(i).setCellValue(d);
} else {
currentRow.createCell(i).setCellValue(cellValue);
}
}
rowNum++;
}
sxssfWorkbook.write(fileout);
sxssfWorkbook.dispose();
}
}
}
Can custom properties on worksheet level (in VBA accessed as Worksheet.CustomProperties) also be accessed via POI?
Jason - I've been struggling with the same issue and found a way to make it work, but it's far from optimal. Here it is anyway and hopefully you or someone else can come up with a better method.
package temp.temp;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCustomProperties;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCustomProperty;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
public class Temp2 {
public static void main(String[] args) {
File inputFile = new File("C:\\myspreadsheet.xlsx");
try (BufferedInputStream fis = new BufferedInputStream(new FileInputStream(inputFile))) {
XSSFWorkbook wb = new XSSFWorkbook(fis);
for (int i = 0; i < wb.getNumberOfSheets(); i++) {
XSSFSheet sheet = wb.getSheetAt(i);
System.out.println("\nSheetName=" + sheet.getSheetName());
CTWorksheet ctSheet = sheet.getCTWorksheet();
CTCustomProperties props = ctSheet.getCustomProperties();
if (props != null) {
List<CTCustomProperty> propList = props.getCustomPrList();
propList.stream().forEach((prop) -> {
POIXMLDocumentPart rel = sheet.getRelationById(prop.getId());
if (rel != null) {
try (InputStream inp = rel.getPackagePart().getInputStream()) {
byte[] inBytes = inp.readAllBytes();
// By experimentation, byte array has two bytes per character with least
// significant in first byte which is UTF-16LE encoding. Don't know why!
String value = new String(inBytes, "UTF-16LE");
System.out.println(" " + prop.getName() + "=" + value);
} catch (IOException ioe) {
//Error
}
}
});
}
}
wb.close();
} catch (Exception e) {
System.out.println(e);
}
System.out.println("End");
}
}
Note that CTWorksheet comes from poi-ooxml-schemas-xx.jar and CustomProperties from ooxml-schemas-yy.jar, so both have to be on the classpath. If you're using modules (as I am), this gives big problems! Good Luck
I am trying to extract data from excel and write it to a web page . As I click on a drop down"I am " provided in the link in the code below the remaining contents of the page changes .I have made certain cells as blank.I wrote the code but getting some exceptions .Please help
import java.io.*;
import java.util.concurrent.TimeUnit;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Copyprogram {
public static WebDriver driver=new FirefoxDriver();
public static void main(String[] args)
{
driver.get("http://www.deal4loans.com/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("html/body/div[4]/div/div/div[1]/a[1]")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
try {
FileInputStream fis = new FileInputStream("C:\\Users\\user\\Documents\\Copy of Booklet1.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheet("testdata");
// Loop through all rows in the sheet
// Start at row 1 as row 0 is our header row
int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum();
for(int i = 1;i<=rowCount;i++)
{
Row row1 = sheet.getRow(i);
try {
Thread.sleep(2000);
} catch (Exception e)
{
// TODO: handle exception
}
runTest(row1.getCell(1).toString(),row1.getCell(2).toString(),row1.getCell(3).toString(),row1.getCell(4).toString(),row1.getCell(5).toString(),row1.getCell(6).toString(),row1.getCell(7).toString(),row1.getCell(8).toString(),row1.getCell(9).toString(),row1.getCell(10).toString(),row1.getCell(11).toString(),row1.getCell(12).toString(),row1.getCell(13).toString() );
System.out.println(row1.getCell(1).toString());
System.out.println(row1.getCell(2).toString());
System.out.println(row1.getCell(3).toString());
System.out.println(row1.getCell(4).toString());
System.out.println(row1.getCell(5).toString());
System.out.println(row1.getCell(6).toString());
System.out.println(row1.getCell(7).toString());
System.out.println(row1.getCell(8).toString());
System.out.println(row1.getCell(9).toString());
System.out.println(row1.getCell(10).toString());
if (i<rowCount)
{
driver.navigate().back();
}
System.out.println(i);
}
fis.close();
} catch (IOException e) {
System.out.println("Test data file not found");
}
driver.close();
}
public static void runTest(String name,String mailid,String query,String city,String mob,String pdt,String PL,String HL,String CL,String LA,String BL,String CC,String ass)
{
driver.findElement(By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[1]/td[2]/input")).sendKeys(name);
driver.findElement(By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[2]/td[2]/input")).sendKeys(mailid);
Select listbox1 = new Select(driver.findElement(By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[3]/td[2]/select")));
listbox1.selectByValue(query);
String selectedValue =listbox1.getFirstSelectedOption().getText();
Select listbox2 = new Select(driver.findElement(By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[4]/td[2]/select")));
listbox2.selectByValue(city);
driver.findElement(By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[6]/td[2]/input")).sendKeys(mob);
if (selectedValue=="1")
{
Select listbox3 = new Select(driver.findElement(By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[7]/td/div[2]/table/tbody/tr/td[2]/select")));
listbox3.selectByValue(pdt);
}
else
{
if (PL == null && PL.length() == 0)
{
driver.findElement(By.cssSelector("input[value=PL]")).click();
System.out.println(PL);
}
if(HL == null && HL.length() == 0)
{
driver.findElement(By.cssSelector("input[value=HL]")).click();
}
if(CL == null && CL.length() == 0)
{
driver.findElement(By.cssSelector("input[value=CL]")).click();
}
if(LA == null && LA.length() == 0)
{
driver.findElement(By.cssSelector("input[value=LA]")).click();
}
if(BL == null && BL.length() == 0)
{
driver.findElement(By.cssSelector("input[value=BL]")).click();
}
if(CC== null && CC.length() == 0)
{
driver.findElement(By.cssSelector("input[value=CC]")).click();
}
driver.findElement(By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[7]/td/div[3]/table/tbody/tr[2]/td[2]/input")).sendKeys(ass);
}
driver.findElement(By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[9]/td/input")).click();
}
}
I have an excel file of 111 rows. I need to omit first two rows of the sheet and then read the file using java and POI.
You have to skip first two rows using rownum().Here is the sample code
HSSFWorkbook workBook = new HSSFWorkbook (fileSystem);
HSSFSheet sheet = workBook.getSheetAt (0);
Iterator<HSSFRow> rows = sheet.rowIterator ();
while (rows.hasNext ())
{
HSSFRow row = rows.next ();
// display row number in the console.
System.out.println ("Row No.: " + row.getRowNum ());
if(row.getRowNum()==0 || row.getRowNum()==1){
continue; //just skip the rows if row number is 0 or 1
}
}
Here is the complete example
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
public class POIExcelReader
{
/** Creates a new instance of POIExcelReader */
public POIExcelReader ()
{}
#SuppressWarnings ("unchecked")
public void displayFromExcel (String xlsPath)
{
InputStream inputStream = null;
try
{
inputStream = new FileInputStream (xlsPath);
}
catch (FileNotFoundException e)
{
System.out.println ("File not found in the specified path.");
e.printStackTrace ();
}
POIFSFileSystem fileSystem = null;
try
{
fileSystem = new POIFSFileSystem (inputStream);
HSSFWorkbook workBook = new HSSFWorkbook (fileSystem);
HSSFSheet sheet = workBook.getSheetAt (0);
Iterator<HSSFRow> rows = sheet.rowIterator ();
while (rows.hasNext ())
{
HSSFRow row = rows.next ();
if(row.getRowNum()==0 || row.getRowNum()==1){
continue; //just skip the rows if row number is 0 or 1
}
// once get a row its time to iterate through cells.
Iterator<HSSFCell> cells = row.cellIterator ();
while (cells.hasNext ())
{
HSSFCell cell = cells.next ();
System.out.println ("Cell No.: " + cell.getCellNum ());
/*
* Now we will get the cell type and display the values
* accordingly.
*/
switch (cell.getCellType ())
{
case HSSFCell.CELL_TYPE_NUMERIC :
{
// cell type numeric.
System.out.println ("Numeric value: " + cell.getNumericCellValue ());
break;
}
case HSSFCell.CELL_TYPE_STRING :
{
// cell type string.
HSSFRichTextString richTextString = cell.getRichStringCellValue ();
System.out.println ("String value: " + richTextString.getString ());
break;
}
default :
{
// types other than String and Numeric.
System.out.println ("Type not supported.");
break;
}
}
}
}
}
catch (IOException e)
{
e.printStackTrace ();
}
}
public static void main (String[] args)
{
POIExcelReader poiExample = new POIExcelReader ();
String xlsPath = "c://test//test.xls";
poiExample.displayFromExcel (xlsPath);
}
}
Apache POI provides two ways to access the rows and cells in an Excel file. One is an iterator that gives you all the entries, the other is to loop up by index. (POI will also tell you the start/end rows/columns). The iterator is often simpler to use, but both are equally as fast.
If you have specific requirements on rows to fetch, I'd suggest you use the latter. Your code would want to be something like:
int FIRST_ROW_TO_GET = 2; // 0 based
Sheet s = wb.getSheetAt(0);
for (int i = FIRST_ROW_TO_GET; i < s.getLastRowNum(); i++) {
Row row = s.getRow(i);
if (row == null) {
// The whole row is blank
}
else {
for (int cn=row.getFirstCellNum(); cn<row.getLastCellNum(); cn++) {
Cell c = row.getCell(cn, Row.RETURN_BLANK_AS_NULL);
if (c == null) {
// The cell is empty
} else {
// Process the cell
}
}
}
}
you can improve Murali N s answer. If you want to skip 40 rows for example, use:
if (currentRow.getRowNum() <= 40) {
continue;
}
Does anybody know how to copy a existing column data into a new sheet in a excel file or a new workbook by Apache poi? The data type is double and date.
I attached the link below. They also provide mutiple different ways of doing it.
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.util.CellRangeAddress;
/**
*
* #author jk
* getted from http://jxls.cvs.sourceforge.net/jxls/jxls/src/java/org/jxls/util/Util.java?revision=1.8&view=markup
* by Leonid Vysochyn
* and modified (adding styles copying)
* modified by Philipp Löpmeier (replacing deprecated classes and methods, using generic types)
*/
public class Util {
public static void copySheets(HSSFSheet newSheet, HSSFSheet sheet){
copySheets(newSheet, sheet, true);
}
public static void copySheets(HSSFSheet newSheet, HSSFSheet sheet, boolean copyStyle){
int maxColumnNum = 0;
Map<Integer, HSSFCellStyle> styleMap = (copyStyle) ? new HashMap<Integer, HSSFCellStyle>() : null;
for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
HSSFRow srcRow = sheet.getRow(i);
HSSFRow destRow = newSheet.createRow(i);
if (srcRow != null) {
Util.copyRow(sheet, newSheet, srcRow, destRow, styleMap);
if (srcRow.getLastCellNum() > maxColumnNum) {
maxColumnNum = srcRow.getLastCellNum();
}
}
}
for (int i = 0; i <= maxColumnNum; i++) {
newSheet.setColumnWidth(i, sheet.getColumnWidth(i));
}
}
public static void copyRow(HSSFSheet srcSheet, HSSFSheet destSheet, HSSFRow srcRow, HSSFRow destRow, Map<Integer, HSSFCellStyle> styleMap) {
Set<CellRangeAddress> mergedRegions = new TreeSet<CellRangeAddress>();
destRow.setHeight(srcRow.getHeight());
for (int j = srcRow.getFirstCellNum(); j <= srcRow.getLastCellNum(); j++) {
HSSFCell oldCell = srcRow.getCell(j);
HSSFCell newCell = destRow.getCell(j);
if (oldCell != null) {
if (newCell == null) {
newCell = destRow.createCell(j);
}
copyCell(oldCell, newCell, styleMap);
CellRangeAddress mergedRegion = getMergedRegion(srcSheet, srcRow.getRowNum(), (short)oldCell.getColumnIndex());
if (mergedRegion != null) {
CellRangeAddress newMergedRegion = new CellRangeAddress(mergedRegion.getFirstRow(), mergedRegion.getFirstColumn(), mergedRegion.getLastRow(), mergedRegion.getLastColumn());
if (isNewMergedRegion(newMergedRegion, mergedRegions)) {
mergedRegions.add(newMergedRegion);
destSheet.addMergedRegion(newMergedRegion);
}
}
}
}
}
public static void copyCell(HSSFCell oldCell, HSSFCell newCell, Map<Integer, HSSFCellStyle> styleMap) {
if(styleMap != null) {
if(oldCell.getSheet().getWorkbook() == newCell.getSheet().getWorkbook()){
newCell.setCellStyle(oldCell.getCellStyle());
} else{
int stHashCode = oldCell.getCellStyle().hashCode();
HSSFCellStyle newCellStyle = styleMap.get(stHashCode);
if(newCellStyle == null){
newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();
newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
styleMap.put(stHashCode, newCellStyle);
}
newCell.setCellStyle(newCellStyle);
}
}
switch(oldCell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
newCell.setCellValue(oldCell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_NUMERIC:
newCell.setCellValue(oldCell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
newCell.setCellType(HSSFCell.CELL_TYPE_BLANK);
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
newCell.setCellValue(oldCell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR:
newCell.setCellErrorValue(oldCell.getErrorCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA:
newCell.setCellFormula(oldCell.getCellFormula());
break;
default:
break;
}
}
public static CellRangeAddress getMergedRegion(HSSFSheet sheet, int rowNum, short cellNum) {
for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
CellRangeAddress merged = sheet.getMergedRegion(i);
if (merged.isInRange(rowNum, cellNum)) {
return merged;
}
}
return null;
}
private static boolean isNewMergedRegion(CellRangeAddress newMergedRegion, Collection<CellRangeAddress> mergedRegions) {
return !mergedRegions.contains(newMergedRegion);
}
}
http://www.coderanch.com/t/420958/open-source/Copying-sheet-excel-file-another