Reading from login.xls file and append results in same file? - selenium

Workbook workbook = Workbook.getWorkbook(new File("C:\\Users\\Tsss-Pc1\\Desktop\\login.xls"));
Sheet sheet = workbook.getSheet(0);
String uname = sheet.getCell(0, 0).getContents();
d.findElement(By.id("inputEmail")).sendKeys(uname);
String pwd = sheet.getCell(1, 0).getContents();
d.findElement(By.id("inputPassword")).sendKeys(pwd);
d.findElement(By.xpath("//div[3]/div/button")).click();
This is my code for reading user name and password from login.xls, i want to append the results in the third column in same file.
WritableCell label = new Label(2,0, "pass");
WritableSheet sheet1 = null;
sheet1.addCell(label);
I tryed with this code, but it's not working for me..
can any one tell me, how to do this...

WritableWorkbook workbook = Workbook.createWorkbook(new File("d://output6.xls"));
WritableSheet sheet = workbook.createSheet("First Sheet", 0);
Label label = new Label(0, 0, "pass");
sheet.addCell(label);
workbook.write();
workbook.close();
}

Related

Syntax error when passing variable args in macro

I have a macro for libreoffice that takes in 2 arguments.
A file path
Variable args that are sheet names to look for.
I iterate over the document pertaining to the first arg and get the sheet that matches the first variable arg. I then convert that sheet to CSV.
Sub ExportToCsv(URL as String, ParamArray sheetNames() As Variant)
Dim saveParams(1) as New com.sun.star.beans.PropertyValue
saveParams(0).Name = "FilterName"
saveParams(0).Value = "Text - txt - csv (StarCalc)"
saveParams(1).Name = "FilterOptions"
saveParams(1).Value = "44,34,0,1,1" ' 44=comma, 34=double-quote
GlobalScope.BasicLibraries.loadLibrary("Tools")
URL = ConvertToURL(URL)
document = StarDesktop.loadComponentFromUrl(URL, "_blank", 0, Array())
baseName = Tools.Strings.GetFileNameWithoutExtension(document.GetURL(), "/")
directory = Tools.Strings.DirectoryNameoutofPath(document.GetURL(), "/")
sheets = document.Sheets
sheetCount = sheets.Count
Dim x as Integer
Dim requiredSheetIndex as Integer
For x = 0 to sheetCount -1
sheet = sheets.getByIndex(x)
sheet.isVisible = True
For i = LBound(sheetNames) To UBound(sheetNames)
If StrComp(sheet.Name, sheetNames(i), vbTextCompare) = 0 Then
requiredSheetIndex = x
End If
Next
Next
currentSheet = document.GetCurrentController.GetActiveSheet()
sheet = sheets(requiredSheetIndex)
document.GetCurrentController.SetActiveSheet(sheet)
filename = directory + "/" + baseName + ".csv"
fileURL = convertToURL(Filename)
document.StoreToURL(fileURL, saveParams())
document.close(True)
End Sub
Eg. ExportToCsv(<path>, 'Data'). Suppose the document has 4 sheets with the 3rd sheet as DATA, this sheet should be converted to CSV.
Previously I used to give the sheet idnex directly into the macro and it worked perfectly. But requirements changed and I have to pass in an array of possible names to match on. Hence the variable args.
But now I am getting a syntax error(Screenshot attached). I cant figure out what is wrong here.
I believe it is complaining about the ParamArray keyword. After a little digging, I discovered you need to include:
option compatible
at the top of your module. For more information, you can refer to this link.

EPPLUS generated xlsx files needs recover on open

I'm using EPPLUS to generate reports and that works fine.
In case I assign valid excel formula in some cells formula property, formulas are rejected, I'm getting warnings: "We found a problem with some content in..." and "Removed Records: Formula from /xl/worksheets/sheet2.xml part".
Formula looks like:"=IF(Worksheet1!C9-Calculation!$B$1=0;24;IF(Worksheet1!C9-Calculation!$B$1<0;Worksheet1!C9-Calculation!$B$1+24;Worksheet1!C9-Calculation!$B$1))" and it works if I paste it directly to excel.
My formula related code looks like:
ExcelPackage excel = new ExcelPackage();
excel.Workbook.Worksheets.Add("Worksheet1");
excel.Workbook.Worksheets.Add("Calculation");
ExcelWorksheet worksheet = excel.Workbook.Worksheets["Worksheet1"];
ExcelWorksheet worksheet2 = excel.Workbook.Worksheets["Calculation"];
for (int c = 1; c <= monthcnt; c++)
{
NumColumns cellAddr = (NumColumns)c;
string addr1 = string.Format("Worksheet1!{0}{1}", cellAddr.ToString(), 8);
worksheet2.Cells[addr1].Formula = string.Format("={0}-Calculation!$B$1", addr1);
string addr2 = string.Format("Worksheet1!{0}{1}", cellAddr.ToString(), 9);
worksheet2.Cells[addr2].Formula = string.Format("=IF({0}-Calculation!$B$1=0;24;IF({1}-Calculation!$B$1<0;{2}-Calculation!$B$1+24;{3}-Calculation!$B$1))", addr2, addr2, addr2, addr2);
}
Does anybody knows what causes this?
Thanks in advance,
SiniĊĦa

How to get numeric value from excel file cell, using POI

I am pretty new to Selenium and learning through self study. Any help will be appreciable.
Currently I am using
String data = wb.getSheetAt(sheetnum).getRow(row).getCell(col).getStringCellValue();
It is working fine for String but not for numeric. I have a cell value of '500' and I want to fetch it. Please help.
Try out following code :
public void readfile(String filepath, String filename, String sheetname) throws IOException {
File file = new File(filepath+"\\"+filename);
FileInputStream fis = new FileInputStream(file);
// for creating .xlsx workbook
Workbook wb = new XSSFWorkbook(fis);
// for reading the sheet by its name
Sheet sh = wb.getSheet(sheetname);
// find the total rows in sheet
int rowcount = sh.getLastRowNum() - sh.getFirstRowNum();
// create a loop to create
for (int i = 0; i < rowcount + 1; i++) {
Row row = sh.getRow(i);
// create a loop to print cell values
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(row.getCell(j).getStringCellValue() + " ");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print((int)row.getCell(j).getNumericCellValue() + " ");
break;
}
}
System.out.println();
}
}
Hope it will help you.
Double data = wb.getSheetAt(sheetnum).getRow(row).getCell(col).getNumericCellValue();

How can I add formatting within a cell that only applies to part of the cell value using EPPlus?

I know how to format cell values in EPPlus but only if the format applies to the whole value. How can I add formatting that only applies to part of the cell value?
for example...
Use the RichText collection to build it:
using (var pck = new ExcelPackage())
{
var wb = pck.Workbook;
var ws = wb.Worksheets.First();
var cell = ws.Cells["B2"];
cell.RichText.Add("This is some ");
cell.RichText.Add("formatting");
cell.RichText.Add(" withing a value");
cell.RichText[1].Color = Color.Red;
cell.RichText[1].Size = 14;
pck.Save();
}

DateTimeStamp in Excel (POI)

I need some help in Apache poi
I am generating excel using Apache POI
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFRow row = sheet.createRow(2);
HSSFCell cell = row.createCell(1);
cell.setCellValue(someService.getDate());
I am getting this Date in DateFormat with TimeStamp MM/dd/yyyy 00:00:00 AM
but after generating Excel, it's not showing its proper value in DateFormat.
Any solution?
You need to tell Excel what format to apply to the cell, so it knows how to format it
You probably want something like
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
CreationHelper createHelper = wb.getCreationHelper();
// Cell styles can be re-used, create only once
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(
createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
....
// In your loop
Row row = sheet.createRow(i);
Cell cell = row.createCell(1);
Date value = service.getDate();
cell.setCellValue(value);
cell.setCellStyle(cellStyle);
Simplest solution is to set date value as formatted string, I think.
...
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
cell.setCellValue(simpleDateFormat.format(someService.getDate()));