Home > Java, Web Dynpro > Web Dynpro Java – generate the excel file

Web Dynpro Java – generate the excel file

October 25th, 2010 Leave a comment Go to comments

Download business data as a excel file is critical for business users to view,modify and manipulate the data freely. In this example, I use Apache POI to generate the excel file in web dynpro application.

The Apache POI Project’s mission is to create and maintain Java APIs for manipulating various file formats based upon the Office Open XML standards (OOXML) and Microsoft’s OLE 2 Compound Document format (OLE2). In short, you can read and write MS Excel files using Java. In addition, you can read and write MS Word and MS PowerPoint files using Java. For more information, please visit the official home page – http://poi.apache.org/.

Current stable version of POI is 3.6, but it requires JDK 1.5 and higher version to use it. Because NW7.0 is based on JDK 1.4.2, I have to choose POI version 3.2.

For more information on how to use external library, please refer to my another post – Web Dynpro Java – use external library.

Step 1. Create a value attribute named ‘FileResource’ with type type    com.sap.ide.webdynpro.uielementdefinitions.Resource.

file resource

Step 2. In the view layout designer, add a UI element with type FileDownload and bind the value attribute to property resource which is created in the previous step.

filedownload

Step 3. The initialization logic of attribute FileResource can be implemented in the method wdDoInit or the other method which is triggered by the action.

Below codesnip is used to create an excel object with POI and initialize the attribute fileReource.

  try {
    //create excel workbook object
    HSSFWorkbook wb = new HSSFWorkbook();
    wb.createSheet(";Days Outstanding";);
    //retrieve sheet object
    HSSFSheet sheet = wb.getSheetAt(0);
    if (sheet == null) {
      sheet = wb.createSheet(";Days Outstanding";);
    }
    HSSFRow headerRow = sheet.getRow(0);
    if (headerRow == null) {
      headerRow = sheet.createRow(0);
    }
    //set cell style
    HSSFCellStyle cellStyle = wb.createCellStyle();
    HSSFFont font = wb.createFont();
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    font.setFontHeightInPoints((short) 10);
    cellStyle.setFont(font);
    //Header Object Type
    HSSFRichTextString str_1=new HSSFRichTextString(";Opportunity Type";);
    if (cell == null) {
      cell = headerRow.createCell((short) 0);
    }
    cell.setCellValue(str_1);
    cell.setCellStyle(cellStyle);
 
    //Header Opportunity Name
    HSSFRichTextString str_2=new HSSFRichTextString(";Opportunity Title";);
    cell = headerRow.getCell(1);
    if (cell == null) {
      cell = headerRow.createCell((short) 1);
    }
    cell.setCellValue(str_2);
    cell.setCellStyle(cellStyle);
 
    //Initilize content
    for(int i = 0; i <; wdContext.nodeDaysOutstandingTable().size(); i++){
      HSSFRow contentRow = sheet.getRow(i + 1);
      if (contentRow == null) {
        contentRow = sheet.createRow(i + 1);
      }
      HSSFCell contentCell = contentRow.getCell(0);
      if (contentCell == null) {
        contentCell = contentRow.createCell((short) 0);
      }
      HSSFRichTextString content_1=new HSSFRichTextString(wdContext.nodeDaysOutstandingTable()
        .getDaysOutstandingTableElementAt(i).getObjectType());
      contentCell.setCellValue(content_1);
 
      contentCell = contentRow.getCell(1);
      if (contentCell == null) {
        contentCell = contentRow.createCell((short) 1);
      }
      HSSFRichTextString content_2=new HSSFRichTextString(wdContext.nodeDaysOutstandingTable()
        .getDaysOutstandingTableElementAt(i).getOppName());
      contentCell.setCellValue(content_2);
    }
 
    //Initilize the value attribute FileResource
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    wb.write(baos); 
 
    byte[] mybytes = baos.toByteArray(); 
    ByteArrayInputStream bais = new ByteArrayInputStream(mybytes); 
 
    wdContext.currentContextElement().setFileResource(WDResourceFactory
        .createResource(bais, ";Summary";, WDWebResourceType.XLS, false )); 
    baos.close(); 
    bais.close(); 
  } catch (IOException ex) {
    //Handle the IO exception
  }

At the web dynpro runtime, the UI element Filedownload will be displayed as a link. User can click the link to download the resource which is bind to it. In this example, it is an excel file that contains the business data.

  1. No comments yet.
  1. No trackbacks yet.