I have nearly 1500 rows in web table, I want to write all the rows data to excel.
I'm using Selenium-Webdriver,Java. problem is program stopped without any error after writing nearly 300 rows.
Please help me on this, How to write all the data to excel. if i give thraed.sleep(5000); its taking more time.
I have used below mentioned code:
List<WebElement> irows = a2.findElements(By.xpath("//*[#id='filter_result']/table/tbody/tr/td[2]/a[1]"));
int iRowsCount = irows.size();
System.out.println(iRowsCount);
FileOutputStream fos = new FileOutputStream("E:\\jega\\testw3.xlsx");
XSSFWorkbook wkb = new XSSFWorkbook();
XSSFSheet sheet1 = wkb.createSheet("DataStorage");
String a3=null;
for (int i=500,Row=0;i<=iRowsCount;i++) {
try
{
WebElement val= a2.findElement(By.xpath("//*[#id='filter_result']/table/tbody/tr["+i+"]/td[2]"));
// WebElement val= a2.findElement(By.xpath("//*[#id='filter_result']/table/tbody/tr["+i+"]/td["+j+"]"));
String a = val.getText();
if( a.length() != 0 )
{
a3=a;
//int length = val.length();
System.out.print(a3 + '\n');
XSSFRow excelRow = sheet1.createRow(Row++);
XSSFCell excelCell = excelRow.createCell(0);
excelCell.setCellType(XSSFCell.CELL_TYPE_STRING);
excelCell.setCellValue(a3);
for ( int j=5;j<12;j++)
{ switch (j)
{
case 5:val= a2.findElement(By.xpath("//*[#id='filter_result']/table/tbody/tr["+i+"]/td[5]"));excelCell = excelRow.createCell(1);
excelCell.setCellValue(val.getText()); break;
case 8:val= a2.findElement(By.xpath("//*[#id='filter_result']/table/tbody/tr["+i+"]/td[8]"));excelCell = excelRow.createCell(2);
excelCell.setCellValue(val.getText());break;
case 9:val= a2.findElement(By.xpath("//*[#id='filter_result']/table/tbody/tr["+i+"]/td[9]"));excelCell = excelRow.createCell(3);
excelCell.setCellValue(val.getText());break;
case 11:val= a2.findElement(By.xpath("//*[#id='filter_result']/table/tbody/tr["+i+"]/td[11]"));excelCell = excelRow.createCell(4);
excelCell.setCellValue(val.getText());break;
}
}
Thread.sleep(5000);
}
else
{
System.out.print(a3 + '\n');
XSSFRow excelRow = sheet1.createRow(Row++);
XSSFCell excelCell = excelRow.createCell(0);
excelCell.setCellType(XSSFCell.CELL_TYPE_STRING);
excelCell.setCellValue(a3);
for ( int j=5;j<12;j++)
{ switch (j)
{
case 5:val= a2.findElement(By.xpath("//*[#id='filter_result']/table/tbody/tr["+i+"]/td[5]"));excelCell = excelRow.createCell(1);
excelCell.setCellValue(val.getText()); break;
case 8:val= a2.findElement(By.xpath("//*[#id='filter_result']/table/tbody/tr["+i+"]/td[8]"));excelCell = excelRow.createCell(2);
excelCell.setCellValue(val.getText());break;
case 9:val= a2.findElement(By.xpath("//*[#id='filter_result']/table/tbody/tr["+i+"]/td[9]"));excelCell = excelRow.createCell(3);
excelCell.setCellValue(val.getText());break;
case 11:val= a2.findElement(By.xpath("//*[#id='filter_result']/table/tbody/tr["+i+"]/td[11]"));excelCell = excelRow.createCell(4);
excelCell.setCellValue(val.getText());break;
}
}
Thread.sleep(5000);
}
}//tryc
catch (NoSuchElementException e) {
}//cath
}//for
// System.out.println();
// Thread.sleep(6000);
fos.flush();
wkb.write(fos);
fos.close();
Can anyone help me on this issue?
Your help is much appreciated.
Best Regards.
I have also exported web table data to an excel sheet like this,
This works fine for me. Hope this will help you.
WebElement element = driver.findElement(By.xpath("//*[#id='entireBody']/div[3]/div/table"));
List<WebElement> tbdy = element.findElements(By.tagName("tbody"));
List<WebElement> thed = element.findElements(By.tagName("thead"));
Thread.sleep(1000);
List<WebElement> tr = tbdy.get(0).findElements(By.tagName("tr"));
List<WebElement> trh = thed.get(0).findElements(By.tagName("tr"));
Thread.sleep(1000);
try {
String filename = Path;
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("FirstSheet");
CellStyle style = workbook.createCellStyle();//Create style
Font font = workbook.createFont();//Create font
font.setBoldweight(Font.BOLDWEIGHT_BOLD);//Make font bold
style.setFont(font);//set it to bold
HSSFRow row = sheet.createRow(0);
for(int i = 0; i < trh.size(); i++){
List<WebElement> tdh =trh.get(i).findElements(By.tagName("td"));
System.out.println("loop1");
for (int j = 0; j < tdh.size(); j++) {
row.createCell(j).setCellValue(tdh.get(j).getText());
row.getCell(j).setCellStyle(style);
}
}
for (int i = 0; i < tr.size(); i++) {
HSSFRow row2 = sheet.createRow(i+1);
List<WebElement> td = tr.get(i).findElements(By.tagName("td"));
for (int j = 0; j < td.size(); j++) {
td.get(j).getText();
row2.createCell(j).setCellValue(td.get(j).getText());
}
}
FileOutputStream fileOut = new FileOutputStream(filename);
workbook.write(fileOut);
fileOut.close();
System.out.println("Your excel file has been generated!");
Related
I tried writing 2 List <WebElement> lists to Excel file using Apache POI. But my method only saves the //column 2 values. //column 1 data not saved in the excel file.
Please help me to resolve this issue.
public void FourthExcel(String classNameOne,String classNameTwo) {
WebDriverWait wait = new WebDriverWait(driver, 40);
wait.pollingEvery(2, TimeUnit.SECONDS);
String filePath = System.getProperty("user.dir");
//Book one
XSSFWorkbook workbookOne = new XSSFWorkbook();
XSSFSheet sheetOne = workbookOne.createSheet("Expenses Sheet");
CommonClass.sleepTime(3000);
WebElement expencesTble = driver.findElement(expencesTable);
wait.until(ExpectedConditions.elementToBeClickable(expencesTble));
List<WebElement> ColOneList = driver.findElements(By.className(classNameOne));
List<WebElement> ColTwoList = driver.findElements(By.className(classNameTwo));
List<Object> ColOneobjectList = Arrays.asList(ColOneList.toArray());
List<Object> ColTwoobjectList = Arrays.asList(ColTwoList.toArray());
int ColumnOneSize = ColOneList.size()-1;
for (int i = 0; i <= ColumnOneSize; i++) {
out.println("For");
out.println(ColOneList.get(i).getText());
//Column 1
XSSFRow rowOneColZero = sheetOne.createRow(i);
rowOneColZero.createCell(0).setCellValue(String.valueOf(ColOneobjectList.get(i)));
//Column 2
XSSFRow rowOneCOlOne = sheetOne.createRow(i);
rowOneCOlOne.createCell(1).setCellValue((RichTextString) ColTwoobjectList.get(i));
}
try {
out.println("try");
FileOutputStream fileOut = new FileOutputStream(filePath + "\\testExcel.xls");
workbookOne.write(fileOut);
workbookOne.close();
} catch (IOException e) {
out.println(e);
}
}
The excel file looks like this:
A1 data is not written into the excel file.
In for loop You override XSSFRow row object, please write like this:
for (int i = 0; i <= ColumnOneSize; i++) {
out.println("For");
out.println(ColOneList.get(i).getText());
//create row
XSSFRow row= sheetOne.createRow(i);
//to Column 1
row.createCell(0).setCellValue(String.valueOf( ColOneobjectList.get(i)));
//to Column 2
row.createCell(1).setCellValue((RichTextString) ColTwoobjectList.get(i));
}
My code imports the excel file then put a certain column in to a textarea
I want to know how would i modify the excel file if for example i edited something on the textarea that edit should be saved on the excel file
String excelFilePath = "sample.xlsx";
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(new File(excelFilePath));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
Workbook workbook = null;
try {
workbook = new XSSFWorkbook(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
}
Sheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = firstSheet.iterator();
while (iterator.hasNext()) {
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
Iterator<Cell> scellIterator = nextRow.cellIterator();
cellIterator.next();
scellIterator.next();
scellIterator.next();
Cell topicsCell = cellIterator.next();
Cell topicSentimentCell =scellIterator.next();
String cellContents = topicsCell.getStringCellValue();
String scellContents = topicSentimentCell.getStringCellValue();
String[] topics = cellContents.split(";");
String[] topicSentiment = scellContents.split(";");
ArrayList<String> tpc = new ArrayList<>();
ArrayList<String> topicsents = new ArrayList<>();
for(int i = 0; i < topics.length; i++) {
topics[i] = topics[i].trim();
tpc.add(topics[i]);
for (int indx = 0; indx < tpc.size(); indx++) {
textArea.append(tpc.get(indx)+"\n");
}
}
for(int si = 0; si < topicSentiment.length; si++) {
topicSentiment[si] = topicSentiment[si].trim();
topicsents.add(topicSentiment[si]);
for (int index = 0; index < topicsents.size(); index++) {
// textArea.append(topicsents.get(index)+"\n");
System.out.print(topicsents+"\n");
}
}
}
try {
inputStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
Open a file as XSSFWorkbook
Open a XSSFSheet of the workbook
Make some changes in the sheet. For example change the value of a XSSFCell
Save the changes by workbook.write()
Example
// Open workbook and sheet
final XSSFWorkbook workbook = new XSSFWorkbook(new File("filename.xlsx"));
final XSSFSheet sheet = workbook.getSheetAt(0);
// Iterate rows
final int firstRowNumber = sheet.getFirstRowNum();
final int lastRowNumber = sheet.getLastRowNum();
for (int rowNumber = firstRowNumber; rowNumber < lastRowNumber; rowNumber++ ) {
final XSSFRow row = sheet.getRow(rowNumber);
if (row == null) continue;
// Iterate columns
final int firstColumnNumber = row.getFirstCellNum();
final int lastColumnNumber = row.getLastCellNum();
for (int columnNumber = firstColumnNumber; columnNumber < lastColumnNumber; columnNumber++ ) {
final XSSFCell cell = row.getCell(firstColumnNumber);
if (cell == null) continue;
// Make some changes
cell.setCellValue("new Value");
}
}
// Save changes
workbook.write(new FileOutputStream("newFilename.xlsx"));
i m trying to merge two .xlsx files to each other in java. But i m somehow getting a NullPointerException on the line
if (cell.getSheet().getWorkbook() == mcell.getSheet()
.getWorkbook()) {
}
Any ides what could cause this error ? when i tried before merging other two files, my code worked perfectly, but now i changed the files and now getting NullPointerException. The new Files which i try to merge have 2 sheets. I just need to merge the first pages of them.
Here is my code :
public static void main(String[] args) {
try {
FileInputStream excellFile1 = new FileInputStream(new File(
"/Users/TLQ/Desktop/a.xlsx"));
FileInputStream excellFile2 = new FileInputStream(new File(
"/Users/TLQ/Desktop/b.xlsx"));
// Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook1 = new XSSFWorkbook(excellFile1);
XSSFWorkbook workbook2 = new XSSFWorkbook(excellFile2);
// Get first/desired sheet from the workbook
XSSFSheet sheet1 = workbook1.getSheetAt(0);
XSSFSheet sheet2 = workbook2.getSheetAt(0);
// add sheet2 to sheet1
addSheet(sheet1, sheet2);
excellFile1.close();
// save merged file
File mergedFile = new File(
"/Users/TLQ/Desktop/Albert.xlsx");
if (!mergedFile.exists()) {
mergedFile.createNewFile();
}
FileOutputStream out = new FileOutputStream(mergedFile);
workbook1.write(out);
out.close();
// mergeThemAll(mergedFile);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void addSheet(XSSFSheet mergedSheet, XSSFSheet sheet) {
// map for cell styles
Map<Integer, XSSFCellStyle> styleMap = new HashMap<Integer, XSSFCellStyle>();
// This parameter is for appending sheet rows to mergedSheet in the end
int len = mergedSheet.getLastRowNum();
for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
XSSFRow row = sheet.getRow(j);
XSSFRow mrow = mergedSheet.createRow(len + j + 1);
for (int k = row.getFirstCellNum(); k < row.getLastCellNum(); k++) {
XSSFCell cell = row.getCell(k);
XSSFCell mcell = mrow.createCell(k);
if (cell.getSheet().getWorkbook() == mcell.getSheet()
.getWorkbook()) {
mcell.setCellStyle(cell.getCellStyle());
} else {
int stHashCode = cell.getCellStyle().hashCode();
XSSFCellStyle newCellStyle = styleMap.get(stHashCode);
if (newCellStyle == null) {
newCellStyle = mcell.getSheet().getWorkbook()
.createCellStyle();
newCellStyle.cloneStyleFrom(cell.getCellStyle());
styleMap.put(stHashCode, newCellStyle);
}
mcell.setCellStyle(newCellStyle);
}
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
mcell.setCellFormula(cell.getCellFormula());
break;
case HSSFCell.CELL_TYPE_NUMERIC:
mcell.setCellValue(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
mcell.setCellValue(cell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
mcell.setCellType(HSSFCell.CELL_TYPE_BLANK);
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
mcell.setCellValue(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR:
mcell.setCellErrorValue(cell.getErrorCellValue());
break;
default:
mcell.setCellValue(cell.getStringCellValue());
break;
}
}
}
}
row.getCell(k) can return null if there is no Cell at that position. There are a few ways to work around the problem (e.g. asking the workbook to create cells on the fly), but for this case, checking against null Cells is the easiest and least resource-consuming option.
I have been thinking of days for the solution. Anyway, I am doing a program where the apache will copy the whole sheet to another sheet in a workbook. Currently, my code can copy the contents but not the colour and format of the sheet. Please assist as I really unsure on how to proceed. Thanks.
int rowReadIndent = 0;
int columnReadIndent = 0;
int rowWriteIndent = 0;
int columnWriteIndent = 0;
ArrayList<ArrayList<ArrayList<Object>>> lists = new ArrayList<ArrayList<ArrayList<Object>>>();
ArrayList<ArrayList<ArrayList<Short>>> cellColorLists = new ArrayList<ArrayList<ArrayList<Short>>>();
//ArrayList<ArrayList<ArrayList<XSSFCellStyle>>> cellStyleLists = new ArrayList<ArrayList<ArrayList<XSSFCellStyle>>>();
ArrayList<String> sheetNameList = new ArrayList<String>();
for(int i = 0; i < fileArrayList.size(); i++) {
OPCPackage pkg = OPCPackage.open(new FileInputStream(desktop + "/test/" + fileArrayList.get(i)));
XSSFWorkbook wb = new XSSFWorkbook(pkg);
Sheet sheet1 = wb.getSheetAt(0);
lists.add(new ArrayList<>());
//cellStyleLists.add(new ArrayList<ArrayList<XSSFCellStyle>>());
cellColorLists.add(new ArrayList<>());
sheetNameList.add(sheet1.getSheetName());
for(int j = 0; j < 50; j++) {
Row row1 = sheet1.getRow(j + rowReadIndent);
lists.get(i).add(new ArrayList<>());
//cellStyleLists.get(i).add(new ArrayList<XSSFCellStyle>());
cellColorLists.get(i).add(new ArrayList<>());
if(row1 != null) {
for(int k = 0; k < 50; k++) {
Cell cell1 = row1.getCell(k + columnReadIndent, Row.RETURN_BLANK_AS_NULL);
if(cell1 != null){
Object o = null;
int type = cell1.getCellType();
if(type == Cell.CELL_TYPE_FORMULA) {
type = cell1.getCachedFormulaResultType();
}
switch (type) {
case Cell.CELL_TYPE_STRING:
o = cell1.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell1)) {
o = cell1.getDateCellValue();
} else {
o = cell1.getNumericCellValue();
}
break;
case Cell.CELL_TYPE_BOOLEAN:
o = cell1.getBooleanCellValue();
break;
}
//XSSFCellStyle cellStyle = new XSSFCellStyle(new StylesTable());
//cellStyle.cloneStyleFrom(cell1.getCellStyle());
//cellStyleLists.get(i).get(j).add(cellStyle);
XSSFCellStyle cellStyle = new XSSFCellStyle(new StylesTable());
cellStyle = (XSSFCellStyle) cell1.getCellStyle();
cellColorLists.get(i).get(j).add(cellStyle.getFillBackgroundColor());
lists.get(i).get(j).add(o);
}
else {
lists.get(i).get(j).add(null);
}
}
}
}
pkg.close();
}
OPCPackage pkg1 = OPCPackage.open(new FileInputStream(desktop + "/test/Output Graph.xlsx"));
XSSFWorkbook wb1 = new XSSFWorkbook(pkg1);
FileOutputStream stream = new FileOutputStream(desktop + "/Output Graph4.xlsx" /*+ name + ".xlsx"*/);
for(int i = 0; i < lists.size(); i++) {
Sheet sheet1 = wb1.createSheet();
wb1.setSheetName(wb1.getSheetIndex(sheet1), sheetNameList.get(i));
for(int j = 0; j < lists.get(i).size(); j++) {
Row row1 = sheet1.createRow(j + rowWriteIndent);
for(int k = 0; k < lists.get(i).get(j).size(); k++) {
Cell cell1 = row1.createCell(k + columnWriteIndent);
//cell1.setCellStyle(cellStyleLists.get(i).get(j).get(k));
//XSSFCellStyle cellStyle = new XSSFCellStyle(new StylesTable());
//cellStyle = (XSSFCellStyle) cell1.getCellStyle();
//cellStyle.setFillBackgroundColor(cellColorLists.get(i).get(j).get(k));
if(lists.get(i).get(j).get(k) != null) {
switch (lists.get(i).get(j).get(k).getClass().getSimpleName()) {
case "String":
cell1.setCellValue((String)lists.get(i).get(j).get(k));
break;
case "Date":
cell1.setCellValue((Date)lists.get(i).get(j).get(k));
break;
case "Double":
cell1.setCellValue((Double)lists.get(i).get(j).get(k));
break;
case "Boolean":
cell1.setCellValue((Boolean)lists.get(i).get(j).get(k));
break;
}
}
}
}
}
/* Sheet sheet1;
Row row1;
Row row2;
Cell cell1;
for(int j = 0; j < lists.size(); j++) {
wb1.cloneSheet(0);
sheet1 = wb1.getSheetAt(j+1);
row1 = sheet1.createRow(1);
row2 = sheet1.createRow(0);
for(int i = 0; i < lists.get(j).size(); i++) {
cell1 = row1.createCell(i);
cell1.setCellType(Cell.CELL_TYPE_NUMERIC);
//cell1.setCellValue(lists.get(j).get(i));
System.out.println("Stored: "+lists.get(j).get(i));
}
for(int i = 0; i < lists1.get(j).size(); i++) {
cell1 = row2.createCell(i);
cell1.setCellType(Cell.CELL_TYPE_NUMERIC);
cell1.setCellValue(lists1.get(j).get(i));
System.out.println("Stored: "+lists1.get(j).get(i));
}
}*/
wb1.write(stream);
stream.close();
pkg1.close();
Desktop.getDesktop().open(new File(desktop + "/Output Graph4.xlsx"));
} catch (FileNotFoundException ex) {
Logger.getLogger(status.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(status.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidFormatException ex) {
Logger.getLogger(status.class.getName()).log(Level.SEVERE, null, ex);
}
}
When i am creating multiple sheets using Apache poi and servlets. It is creating the sheet but not writing the data to file. I am trying to write the first 1000 records to sheet1 and next 1000 to sheet2 through below code, but not working
private void writeDataToExcelFile(String string,
ArrayList<ArrayList<String>> excelData, OutputStream outputStream) {
HSSFWorkbook myWorkBook = new HSSFWorkbook();
String sheetName = "";
sheetName = "Document-" + 0;
HSSFSheet mySheet = myWorkBook.createSheet();
HSSFRow myRow = null;
HSSFCell myCell = null;
for (int rowNum = 0; rowNum < excelData.size(); rowNum++) {
ArrayList<String> rowData = excelData.get(rowNum);
if(rowNum>0 && rowNum%1000 == 0)
{
sheetName = "Document-" + (rowNum/1000);
mySheet = myWorkBook.createSheet();
}
myRow = mySheet.createRow(rowNum);
for (int cellNum = 0; cellNum < rowData.size(); cellNum++) {
myCell = myRow.createCell(cellNum);
myCell.setCellValue(rowData.get(cellNum));
}
}
System.out.println("Last row:" + mySheet.getLastRowNum());
System.out.println("Row number:" + mySheet.rowIterator().next().getRowNum());
try {
myWorkBook.write(outputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
What is wrong with my logic.Please do the needful help.
Thanks
When you loop through the dataset, you are wanting to split at row 1000 to start a new sheet, which is fine, however when you start the new sheet, the next row you create is row 1001 (the outer loop index variable)
myRow = mySheet.createRow(rowNum);
To get the effect you wish, change the loop to be something like this:
int currentRow = 0;
for (int rowNum = 0; rowNum < excelData.size(); rowNum++)
{
ArrayList<String> rowData = excelData.get(rowNum);
if(currentRow == 1000)
{
sheetName = "Document-" + (rowNum/1000);
mySheet = myWorkBook.createSheet();
currentRow = 0;
}
myRow = mySheet.createRow(currentRow);
for (int cellNum = 0; cellNum < rowData.size(); cellNum++)
{
myCell = myRow.createCell(cellNum);
myCell.setCellValue(rowData.get(cellNum));
}
currentRow++;
}
I haven't compiled this, so I don't know if it'll work right away, but it should point you in the right direction.
HTH
Edit
Thinking about this further, you could get the same effect from making a 1 line change to the original application (albeit losing a little bit of clarity):
myRow = mySheet.createRow(rowNum%1000);