cancel
Showing results for 
Search instead for 
Did you mean: 

Problems with Using OfficeControl

Former Member
0 Kudos

Hello -

I have finally been successful in loading and viewing an eXcel (XLS) file in a Web Dynpro App using the OfficeControl. I followed the instructions in "Example for the Use of an Office Document"

http://help.sap.com/saphelp_nw04/helpdata/en/ef/3483789514b748b6fe1f145e9685ab/frameset.htm

... of course changing documentType from 'ms_word' to ms_excel'

First, I want to document how I got this to work. The method showDocument() in the tutorial has been deprecated. There is a note in the tutorial about this but, incredibly, there are no details as to how to accomplish this the new way.

1. Create a Context Value Attribute

name = Visibility

type = com.sap.ide.webdynpro.uielementdefinitions.Visibility

2. Bind this attribute to the OfficeControl visible property

3. Set the visibility in code in the fillNode() method


wdContext.currentContextElement().setVisibility(WDVisibility.VISIBLE)

where:

WDVisibility.NONE = control is invisible and takes no space

WDVisibility.BLANK = control is invisible and takes up space

WDVisibility.VISIBLE = control is visible

Second, I want to see if anyone knows how to load more than one file i.e. close the first XLS file and open a new. I have tried and tried and the control will only display the first file loaded in fillNode().

Some explanation: a supplyFunction must be defined when you create the Context SourceNode attribute. This method, fillNode() in the tutorial, is automatically executed when the web page loads. The file specified in fillNode() is the one that displays when you click the button.

I have added an InputField that lets the user specify a different XLS file. I have verified that getBytesFromFile() is opening and reading a byte array from the file I specify, but again the original file is displayed.

Note: setting Visibility = WDVisibility.BLANK is supposed to close the file. I have tried this and it has no effect. I still cannot view a different worksheet.

Has anyone been able to do this with a Word document or eXcel spreadsheet?

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

Try the following

byte[] bytes =null;

try

{

int length = 0;

String path = wdContext.currentContextElement().getDocPath();

FileInputStream is=null;

byte[] bytes = null;

try

{

is = new FileInputStream(new File(path));

long length = file.length();

bytes = new byte[(int)length];

long bytesRead = is.read(bytes);

is.close();

}

catch(IOException ioe)

{

is.close();

}

wdContext.currentContextElement().setDocData(bytes);

WDOfficeControlMethods.showDocument(wdThis.wdGetAPI(),"myOfficeControl");

Attribute "DocData" should be set the datasource property of the OfficeControl.

Regards, Anilkumar

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Terry,

I m using office control in my WebDynpro application.I m having problem reading data from file .I have refered the tutorial that u have mentioned above.What exactly u have done to read data from file?

Former Member
0 Kudos

Anilkumar -

You solved the problem. Your solution -- which is much simpler than what is shown in the tutorial -- allows me to open any eXcel (XLS) file. To be safe, you should 'close' one file before opening another.

Here is a recap of what I did in hopes it will help others who have struggled with the OfficeControl:

1. Delete the Context variables WorksheetSourceNode and WorksheetContent

In the tutorial these are named DocumentSourceNode and DocumentContent

Also delete all references to variables 'node' and 'element' which refer to WorksheetSourceNode and WorksheetContent

2. Create a simple Context Value Attribute to replace the above

name = ExcelData

type = binary

3. Delete method fillNode() required by WorksheetSourceNode

4. The method getBytesFromFile() in the tutorial is retained

5. Here is the code for wdDoInit()


  public void wdDoInit()
  {
    //@@begin wdDoInit()
    // Turn visibility off before loading any file
    wdContext.currentContextElement().setVisibility(WDVisibility.BLANK);
    //@@end
  }

6. And the code for my [Load] and [Close] buttons


  public void onActionLoad(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
  {
    //@@begin onActionLoad(ServerEvent)

    // Get the file to load; should verify file exists
    String excelFile = wdContext.currentContextElement().getExcelFile();

    try {
        byte[] bytes = getBytesFromFile(new File(excelFile));
        wdContext.currentContextElement().setExcelData(bytes);
    } catch (IOException iox) {
        //e.printStackTrace(); //not seen
    }

    //@@end
  }

  public void onActionClose(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
  {
    //@@begin onActionClose(ServerEvent)
    wdContext.currentContextElement().setVisibility(WDVisibility.NONE);
    //@@end
  }