JOpenDocument reading from .ods file - java

I'm trying to read a .ods file from jopendocument. But when I am trying to access/print the value of a particular cell a blank output is displayed.My code is :
for(int nRowIndex = 0; nRowIndex < r; nRowIndex++)
{
int nColIndex = 0;
for( ;nColIndex < t; nColIndex++)
{
System.out.println(sheet.getCellAt(nColIndex, nRowIndex).toString());
}
}
This gets printed on console:
<table:table-cell xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" />
Any help will be appreciated.

The method getCellAt returns an Object of type Cell, to get the contained value as text use
sheet.getCellAt(nColIndex, nRowIndex).getTextValue()

Related

Content overwritten on top of another when formula is applied to excel cell using Apache poi

I am getting an issue of content being overwritten on top of another i.e old value and new values overlap after applying formula. And this issue only occurs Microsoft Excel 2010. How do I handle this?
I have tried refreshing the formulas for entire workbook using formula evaluator but still the issue persists.
private String applyTtlAvlFormula(final int colId, final int hrsColId)
{
String ttlFormulaString = "";
for( int i = ttlAvlContentRow; i < tableHeadStartRow+1; i++) {
String colName1 = CellReference.convertNumToColString(hrsColId);
String colName2 = CellReference.convertNumToColString(colId);
ttlFormulaString += "("+colName1+String.valueOf(i)+ "*$"+colName2 +"$"+String.valueOf(i)+")";
if (i != tableHeadStartRow){
ttlFormulaString += "+";
}
}
return ttlFormulaString;
}
cell.setCellFormula(applyTtlAvlFormula(j,hrsCol));
Following are pre and post shots of the issue.
From the images it can be seen that 40 and 32 gets overlapped after applying the formula. How can this issue be resolved.

Selenium looping sendKeys

So I have gotten far enough to locate the textbox on facebook, and send a person a message. Now I want to see if I can create a loop to send multiple messages at once. My current problem is I have a for loop:
for (int i = 0; i < 20; i++) {
messageBox.sendKeys("My friend's name " + i + " ");
messageBox.sendKeys(Keys.ENTER);
}
However, I can only do this once. So It will print "My friend's name0" and then stop. Java says: element is not attached to the page document.
I am assuming that because i sent the message, some part of the Inspect Element has changed? Is there a way to solve this?
Get Friend's name from Excel sheet
Make a Method for get data from excel file:-
public static void fn_FetchExcelData(String FilePath,String ColumnName) throws IOException{
String WbookPath=FilePath;
Workbook WBookObj=fn_GetWorkbook(FilePath);
Sheet SheetObj=WBookObj.getSheetAt(0);
Row FstRowObj=SheetObj.getRow(0);
int cellCount=FstRowObj.getLastCellNum();
int columnNumber=0;
ArrayList<String> AL=new ArrayList<String>();
for(int j=0; j<cellCount;j++){
if(FstRowObj.getCell(j, Row.CREATE_NULL_AS_BLANK).getStringCellValue().trim().equalsIgnoreCase(ColumnName)){
columnNumber=j;
break;
}
}
int rowcnt=SheetObj.getLastRowNum();
for(int i=1;i<=rowcnt;i++){
Cell fstcellObj=SheetObj.getRow(i).getCell(columnNumber, Row.CREATE_NULL_AS_BLANK);
String ColumnVal=fstcellObj.getStringCellValue();
AL.add(ColumnVal);
call excel method & use it
fn_FetchExcelData("Your excel data path");
int j;
int size = AL.size();
for(j=0;j<size;j++)
{
System.out.println(AL.get(j));
messageBox.sendKeys(AL.get(j));
Nevermind i figured it out, I just added
WebElement messageBox = driver.findElement(By.xpath("//textarea[#class='uiTextareaAutogrow _552m']"));
wait.until(ExpectedConditions.visibilityOf(messageBox));
Again to the loop and it works. However it is VERY slow.

Export CLOB database field that exceeds 32767 char limit

I try to export content of table from a database to Excel.
In the table I have a column that holds imageURI data (that exceeds 32767 characters limit) or may hold line "Not Available" if the image is absent.
I do not need to export that image URI to Excel, just check if the image is present or not and export one of two lines for this particular column: "Present" (instead of the value of the cell) or "Not Available" (the value of the cell).
Here is the code that checks if the image is present or not (I'm using Apache POI to export data):
for (int i=1; i<=columnCount; i++) {
Cell dataCell = dataRow.createCell(i);
if(columnName.get(i).equals("IMAGE") && !result.getString(columnName.get(i)).equals("Not Available")){
dataCell.setCellValue("Present");
}
else{
dataCell.setCellValue(result.getString(columnName.get(i)));
}
}
The above construction gives me java.sql.SQLException.
How do I solve this?
Thanks.
UPD:
The following construction:
for (int i=1; i<=columnCount; i++) {
Cell dataCell = dataRow.createCell(i);
if(columnName.get(i).equals("IMAGE")){
dataCell.setCellValue("Present");
}
else{
dataCell.setCellValue(result.getString(columnName.get(i)));
}
}
works fine.

sheet.getLastRowNum() function gives invalid count with Apache POI 3.9 [duplicate]

This question already has answers here:
How to get row count in an Excel file using POI library?
(7 answers)
Closed 6 years ago.
I am using Apache POI 3.9 for xls and xlsx file processing.
As per the logic, I want to iterate over each row and want to collect data for further processing. For that I am using sheet.getLastRowNum() to retrieve number of rows in the xls sheet.
But it seems that sheet.getLastRowNum() gives wrong count if number of records are more than 10.
It works fine if total number of rows are 10. Otherwise it gives result deducted by one.
i.e If there are 15 row in the sheet then It gives 14 as Last Row number.
Can Anyone suggest me,How to solve this problem ??
Here is the code that i am using to collect the data ...
public static List<LinkedList<String>> populateExcelSheetContentWithHeader(Sheet sheet,int columnsCount,int rowsCount) throws Exception{
Row row = null;
List<LinkedList<String>> excelFileRecordsList = new LinkedList<LinkedList<String>>();
int emptyColCount = 0;
String freeTextData = null;
LinkedList<String> excelFileDataList =null;
int actualRows = sheet.getLastRowNum();
if (actualRows < rowsCount) {
rowsCount = actualRows;
}
///ITERATE OVER EACH ROW AND POPULATE THE DATA
for(int index=0;index<rowsCount;index++){
row = sheet.getRow(index);
if(row!=null){
emptyColCount = 0;
excelFileDataList =new LinkedList<String>();
for(int colIndex=0;colIndex<columnsCount;colIndex++){
freeTextData = "";
if(isEmptyCell(row.getCell(colIndex))){
emptyColCount++;
}else{
freeTextData = getCellValue(row.getCell(colIndex),false);
}
//ADD TEXT DETILS TO THE LIST
excelFileDataList.add(freeTextData);
}
//CHECK FOR END OF FILE
if(emptyColCount != columnsCount){
excelFileRecordsList.add(excelFileDataList);
}else{
break;
}
}
}
return excelFileRecordsList;
}
Any suggestion is appreciated .
it is possible that you are converting from CSV or you did any kind of modifications (copy or paste) inside your xls? I had the same exact behaviour when I did some changes like I mentioned. To fix it I had to copy the original again. Sorry for not providing a real solution, I just say what happened.
Maybe that helped

Error while reading text from Document in jdom?

I am here to ask you a basic question about jdom. I am trying to read a Document object but I got an error all the time.
The Document that I am trying to read is,
<message>
<header>
<messageType>snmp</messageType>
<sendFrom>192.168.0.16</sendFrom>
<hostName>oghmasysMehmet</hostName>
<sendTo>192.168.0.12</sendTo>
<receiverName>Mehmet</receiverName>
<date>03/10/2011</date>
</header>
<body>
<snmpType>getbulk</snmpType>
<ip>127.0.0.1</ip>
<port>161</port>
<oids>
<oid>1.3.6.1.2.1.1</oid>
</oids>
<community>public</community>
<nR>0</nR>
<mR>5</mR>
</body>
</message>
And I am trying to value. To do it, I have written a function,
public Vector<String> getOIDs(Document document){
Vector<String> oids = new Vector<String>();
Element root = document.getRootElement();
Element body = root.getChild("body");
//Element element = body.getChild("oids");
List rows = body.getChildren("oid");
for (int i = 0; i < rows.size(); i++) {
Element row = (Element) rows.get(i);
String s = row.getText();
oids.add(s);
}
return oids;
}
but When I debug it, I can always see that there is nothing read by the function.
Can you please help me about that ?
Thank you all
EDIT :Ok sorry for asking such a noob question, I just made a mistake in getchildren (); I should have written oids instead of oid
EDIT 2: Actually ı have changed the code as I commented on my question but now, the only thing I read is "\n \n" not "1.3.6.1.2.1.1". What do you think the problem might be ?
At first glance, seems to me that doesn't have a "oid" child, it has a "oids" child. The element you're trying to read is inside the "oids" element.
You can try and debug it step by step, and see what is the element that isn't being read. That would be my best guess without trying it out.
Your commented out line was correct, the line below it just needs to be updated to match. Your listing should be:
Vector<String> oids = new Vector<String>();
Element root = document.getRootElement();
Element body = root.getChild("body");
Element element = body.getChild("oids");
List rows = element.getChildren("oid");
for (int i = 0; i < rows.size(); i++) {
Element row = (Element) rows.get(i);
String s = row.getText();
oids.add(s);
}
return oids;

Categories