Reading data from ods(Open office spreadsheet) for selenium in java - java

Is there any possibility of using ods(Open office spreadsheet) files for read and write data in selenium using java. I searched internet and i am not find any way. Pls anyone help me.

File file = new File("file location");
Sheet sheet = SpreadSheet.createFromFile(file).getSheet(0)MutableCell domain = null;
sheet.getCellAt(row index,column index).setValue(value you want to write in the cell) / getvalue()-fetches the value in the cell;
sheet.getSpreadSheet().saveAs(file);
Note:
For using this, you need openjdocument jar file added to your project.

Related

Aspose : set custom properties to a sheet using aspose jar

How to Write custom properties to a sheet in workbook using aspose - cells -7
CustomPropertyCollection custom = customProp.getCustomProperties();
Here's a code snippet to begin with
See the following sample code using Aspose.Cells for Java to accomplish the task for your reference:
e.g.
Sample code:
// Open an Excel file
Workbook workbook = new Workbook("Book1.xlsx");
// Retrieve a list of all custom document properties of the Excel file
CustomDocumentPropertyCollection customProperties = workbook.getWorksheets().getCustomDocumentProperties();
// Adding a custom document property to the Excel file
DocumentProperty publisher = customProperties.add("Publisher", "Aspose");
// Save the file
workbook.save("out1.xlsx", SaveFormat.XLSX);
PS. I am working as Support developer/ Evangelist at Aspose.

Read excel file in java without using any jars

I have a requirement to read excel file in java without using any third party library jar like POI,JEXCEL .I don't know exactly can spring support for same? Please suggest if you know something.
Thanks in advance
Using POI I have done but read without using any jar
public static void readFromExcel(String file) throws IOException{
HSSFWorkbook myExcelBook = new HSSFWorkbook(new FileInputStream(file));
HSSFSheet myExcelSheet = myExcelBook.getSheet("Birthdays");
HSSFRow row = myExcelSheet.getRow(0);
if(row.getCell(0).getCellType() == HSSFCell.CELL_TYPE_STRING){
String name = row.getCell(0).getStringCellValue();
System.out.println("name : " + name);
}
If you are not allowed to use a 3rd party JAR or library you will need to write a parser to read the document into your data classes.
I would advise you to take a look at the file format specification for Microsoft Office. You will need to understand this to build a reliable parser.
It would be much easier to just use Apache POI and see if the requirements can be changed to allow it.

How to automatically load the excel workbook with multiple worksheets in R

I am very new to R, and am reading an excel file (size 27,964 KB) into R using read.xlsx from library open.xlsx.
filename = "myfile.xlsx"
df1 = read.xlsx(filename, sheet="df1",colNames= TRUE)
df2 = read.xlsx(filename, sheet="df2",colNames= TRUE)
There are multiple sheets in the excel file. Right now I am reading one sheet at a time, but I want to automate the process and be able to create data frames according to sheet names.
I read we can use XLConnect for this purpose, but when I tried XLConnect in my case, I got the an error
wb = loadWorkbook("myfile.xlsx")
Error: OutOfMemoryError (Java): GC overhead limit exceeded
In order to fix this error I used
options(java.parameters = "-Xmx1024m")/"Xmx2g"
but both the options did not help. I tried reading about exce.link but could not really figure it out.
Can some one please guide how to use another method to read excel file with multiple sheets.

How to Edit/Modify an existing Excel file in Java with Jexcel API

I want to edit an existing Excel file with Java, to add some more data to an existing template excel file. So i used Jexcel for this purpose.
As suggested everywhere, I tried the following,
Workbook existingWorkbook = Workbook.getWorkbook(new File("H://"+file_name));
WritableWorkbook copy = Workbook.createWorkbook(new File("H://"+file_name+"_temp1.xls"));
But it shows an exception in the second line.
jxl.common.AssertionFailed
at jxl.common.Assert.verify(Assert.java:37)
at jxl.read.biff.SheetReader.handleObjectRecord(SheetReader.java:1811)
at jxl.read.biff.SheetReader.read(SheetReader.java:1059)
at jxl.read.biff.SheetImpl.readSheet(SheetImpl.java:716)
at jxl.read.biff.WorkbookParser.getSheet(WorkbookParser.java:257)
at jxl.write.biff.WritableWorkbookImpl.copyWorkbook(WritableWorkbookImpl.java:969)
at jxl.write.biff.WritableWorkbookImpl.<init>(WritableWorkbookImpl.java:343)
at jxl.Workbook.createWorkbook(Workbook.java:339)
at jxl.Workbook.createWorkbook(Workbook.java:320)
at run_book.process_input.<init>(process_input.java:83) <--create workbook stt.
.........<stack trace goes on>
So how could one edit an already existing jexcel file.
I did get another warning
Warning: Text Object on sheet "sheet2" not supported - omitting
Thanks in advance :)
Figured out the problem.
We have to close the input file before writing back (editing) the same file.
so to edit an existing Excel file with Jexcel
File inp = new File("H://"+file_name);
File out = new File("H://"+file_name);
Workbook existingWorkbook = Workbook.getWorkbook(inp);// This opens up a read-only copy of the workbook
WritableWorkbook copy = Workbook.createWorkbook(out,existingWorkbook); // This opens up a writable workbook so that we can edit the copy
//..........Some writes to excel workbook...........
// Now before writing & closing the copy, first close the existing one
existingWorkbook.close(); // Important: Close it before writing the copy with copy.write();
inp.close();
copy.write();
copy.close();
I ran into this same issue and to solve the problem I updated to the latest version of jxl.jar via maven. After doing this there was a very long delay when it ran destinationWorkbook = Workbook.createWorkbook(outputFile, sourceWorkbook); but it completed successfully without errors.

how to solve JXL error : jxl.read.biff.BiffException: Unable to recognize OLE stream

i am trying to get cell data from my .csv file but it gets error :
jxl.read.biff.BiffException: Unable to recognize OLE stream
I don't understand how to solve this,please give me some solution
this code is for jxl api & is that api support to .csv?
Code for reference:
public void read() throws IOException, BiffException {
File inputWorkbook = new File(inputFile);
try
{
w = Workbook.getWorkbook(inputWorkbook.getAbsoluteFile());
// Get the first sheet
Sheet sheet = w.getSheet(0);
// Loop over first 10 column and lines
for (row = 1; row < sheet.getRows(); row++)
{
ReadExcelLotSizeEntity readExcelLotSizeEntity =new ReadExcelLotSizeEntity();
cell = sheet.getCell(1,row);
type= cell.getType();
if (cell.getType() == CellType.LABEL)
{
symbol=cell.getContents();
System.out.println(":::::::::::::::::"+symbol);
readExcelLotSizeEntity.setSymbol(symbol);
}
int col=2;
cell = sheet.getCell(col,row);
while(!cell.getContents().equals("")||cell.getContents()!=null)
{
System.out.println("||||||||||||||||"+cell.getContents());
cell=sheet.getCell(col,row);
col++;
}
lotSize= new Double(cell.getContents());
readExcelLotSizeEntity.setLotSize(lotSize);
readExcelLotSizeEntity.setCreateUserId(1L);
readExcelLotSizeEntity.setCreateDtTm(new Date());
readExcelLotSizeHome.persist(readExcelLotSizeEntity);
}
} catch (BiffException e) {
e.printStackTrace();
}
}
I was also facing this problem earlier. I googled and read this post and many other posts that were asking for solution to this BiffException. I don't have the exact solution but as I fixed my problem you can do it too, perhaps.
I was trying to read data from the Excel file saved in MS Office 2010 and I was getting this error. I saved the file as an Excel 2003-7 and then read it without any problem. It may the case that this problem occurs in Office 10 but not in Office 2003-7.
I hope this will work in your case.
Saving File as "Excel 97-2003 Workbook" type solved my issue.
JXL library doesnot support .csv and .xslx formats, which is the format used by Excel-2010. hence, use Excel 97-2003 which is .xls foramatted and is supported by JXL library.
or else if you want to use excel-2010, use APACHE POI(XSSFWorkbooks) instead of JXL.
For using .csv format, google for CSVReader libraries.
JXL is a simple (and hence limited) API. If it says
Unable to recognize OLE stream
it is what it is. It doesn't quite understand your Excel XLS file. Have confidence that the error is legitimate. This API only supports *.xls files; it doesn't support, for example, *.csv or *.xlsx files. Obviously, having the file renamed to *.xls alone is not sufficient. It must be in Excel 97-2003 format too.
Copy all the cells from your *.csv or *.xlsx file.
Open MS Excel and paste the copied cells.
Save the file as MS Excel 97-2003 (*.xls) file.
This error will surely not appear again.
On the other hand, if you want to process other formats (xlsx, csv) directly, look for other tools like Apache POI.
Save the Excel file type as Excel 97-2003 Worksheet and extension type as xls
Actually you are using different version of csv file .Please save it in the exact version.
For ex: we should save the excel sheet in word as 9
save the file as Excel 97-2003 and also change the file format from xlsx to xlx , in the code(in the file name)
I was trying to read data from the Excel file saved in MS Office 2010 and I was getting this error. I saved the file as an Excel 2003-7 and then read it without any problem. It may the case that this problem occurs in Office 10 but not in Office 2003-7

Categories