cancel
Showing results for 
Search instead for 
Did you mean: 

Read excel file available in desk top

Former Member
0 Kudos

Hi,

 I had created excel file in following way but i want to read that excel file ike cell by cell how can it possible.

 public void exporttoexcel( com.sap.tc.webdynpro.progmodel.api.IWDNode datanode, java.util.Map ColumnInfo )
  {
    //@@begin exporttoexcel()
	byte[] excelXMLFile;
		IWDCachedWebResource cachedExcelResource = null;
		String fileName = datanode.getNodeInfo().getName() + ".xls";
		try {
//		   create Excel 2003 XML data as a byte array for the given context node,
//		   attributes and headers
		excelXMLFile = toExcel(datanode, ColumnInfo).getBytes("UTF-8");
		wdComponentAPI.getMessageManager().reportSuccess("datanode size in Export to excel"+datanode.size());
//		   create a cached Web Dynpro XLS resource for the given byte array
//		   and filename
		cachedExcelResource = getCachedWebResource(
		excelXMLFile, fileName, WDWebResourceType.XLS);
//		   Store URL and file name of cached Excel resource in context.
		if (cachedExcelResource != null) {
		wdContext.currentContextElement().setExcelFileURL(
		cachedExcelResource.getURL());
		wdContext.currentContextElement().setExcelFileName(
		cachedExcelResource.getResourceName());
//		   Open popup window with a link to the cached Excel file Web resource.
		//openExcelLinkPopup();
		} else {
		wdComponentAPI.getMessageManager().reportException(
		"Failed to create Excel file from table!", true);
		}
		} catch (UnsupportedEncodingException e) {
		wdComponentAPI.getMessageManager().reportException(
		e.getLocalizedMessage(), true);
		} catch (WDURLException e) {
		wdComponentAPI.getMessageManager().reportException(
		e.getLocalizedMessage(), true);
		}  

Accepted Solutions (0)

Answers (3)

Answers (3)

susmita_panigrahi
Active Participant
0 Kudos

Hi Surya,

Please refer the below forums which is related to reading data from an excel sheet:

1.

2.

Thanks

Susmita

former_member185086
Active Contributor
0 Kudos

Hi Surya

Please refer this thread for

[HELP|;

Best Regards

Satish Kumar

Former Member
0 Kudos

Hi Satish,

Here i didn't have any download UI element, here first i'm creating excel file using watever data entered by enduser and in parallel i want to avoid exsting of multiple users in excel sheet for this whenever user press submit button after entering user data,read excel file data and check with persent entered data of user. so here i can able to give path of excel from that path i want to read excel file. how can it possible please tell otherwise u can provide me any better solution.

Best regards,

surya.

Former Member
0 Kudos

Hi Surya,

Create a value node User data with no value attributes in it..

Create a File download UI element and write this method to get the data from the excel to a node.

Write this code ont the event handler of that button.Let the file is stored in Value attribute 'ExcelFile' which is binded to the resource and data property of the UI element.

InputStream InpStream = null;

if (wdContext.currentContextElement().getExcelFile()!=null)

{

if (wdContext.currentContextElement().getExcelFile().getResourceType().getFileExtension().equalsIgnoreCase("xls"))

{

try {

InpStream = wdContext.currentContextElement().getExcelFile().read(true);

} catch (IOException e) {

wdComponentAPI.getMessageManager().reportException(e.getMessage(),true); }

}else {

wdComponentAPI.getMessageManager().raiseInvalidContextAttributeException((IWDNodeElement)wdContext.currentContextElement(),wdContext.getNodeInfo().getAttribute("ExcelFile"),"Please choose an excel file",true);

}}

else {wdComponentAPI.getMessageManager().reportException("Please select an file",true); }

if (InpStream!=null)

{ try {

Workbook workbook = Workbook.getWorkbook(InpStream);

Sheet sheet = workbook.getSheet(0);

int columnCount = sheet.getColumns();

int rowCount = sheet.getRows();

for (int i = 0; i <rowCount; i++){

if(i!=0) {

IPublicUserCreateApp.IUserDataElement UserDataEle = wdContext.createUserDataElement();

if (wdContext.nodeUserData().size()==0)

{ wdContext.nodeUserData().bind(UserDataEle); }

else { wdContext.nodeUserData().addElement(UserDataEle);

}wdContext.nodeUserData().moveLast();

}

for (int j = 0; j<columnCount; j++)

{ if (i==0)

{wdContext.nodeUserData().getNodeInfo().addAttribute(sheet.getCell(j,0).getContents(),"com.sap.dictionary.string");}

else{ // Setting the values..

wdContext.nodeUserData().currentUserDataElement().setAttributeValue(sheet.getCell(j,0).getContents(),sheet.getCell(j,i).getContents());

}}}

catch (FileNotFoundException e) {

wdComponentAPI.getMessageManager().reportException("FileNotFound EXception occured"+e.getMessage(),true);

} catch (IOException e) {

wdComponentAPI.getMessageManager().reportException("IO Exception occured"+e.getMessage(),true);

}catch (BiffException e){

wdComponentAPI.getMessageManager().reportException("Biff Exception occured"+e.getMessage(),true);

}catch (Exception e){

wdComponentAPI.getMessageManager().reportException("Exception occured"+e.getMessage(),true);

} }

//Method to display values..

// wdContext.nodeUserData().moveFirst();

//

// for (int i = 0; i<wdContext.nodeUserData().size();i++)

// {

// for (java.util.Iterator j= wdContext.nodeUserData().getNodeInfo().iterateAttributes(); j.hasNext();)

// {

// IWDAttributeInfo AttInfo = (IWDAttributeInfo)j.next();

//

// wdComponentAPI.getMessageManager().reportSuccess("For the element"i" for the attribute "AttInfo.getName()" the value is "+ wdContext.nodeUserData().currentUserDataElement().getAttributeAsText(AttInfo.getName()));

//

// }

//

// wdContext.nodeUserData().moveNext();

// }

//

Now the value attributes will be created dynamically and the values are populated.

Best Wishes

Idhaya R