cancel
Showing results for 
Search instead for 
Did you mean: 

Import data from excel/csv file in web dynpro

Former Member
0 Kudos

Hi All,

I need to populate a WD table by first importing a excel/CSV file thru web dynpro screen and then reading thru the file.Am using FileUpload element from NW04s.

How can I read/import data from excel / csv file in web dynpro table context?

Any help is appreciated.

Thanks a lot

Aakash

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi,

Here are the basic steps needed to read data from excel spreadsheet using the Java Excel API(jExcel API).

jExcel API can read a spreadsheet from a file stored on the local file system or from some input stream, ideally the following should be the steps while reading:

Create a workbook from a file on the local file system, as illustrated in the following code fragment:

import java.io.File;

import java.util.Date;

import jxl.*;

...

Workbook workbook = Workbook.getWorkbook(new File("test.xls"));

On getting access to the worksheet, once can use the following code piece to access individual sheets. These are zero indexed - the first sheet being 0, the second sheet being 1, and so on. (You can also use the API to retrieve a sheet by name).

Sheet sheet = workbook.getSheet(0);

After getting the sheet, you can retrieve the cell's contents as a string by using the convenience method getContents(). In the example code below, A1 is a text cell, B2 is numerical value and C2 is a date. The contents of these cells may be accessed as follows

Cell a1 = sheet.getCell(0,0);

Cell b2 = sheet.getCell(1,1);

Cell c2 = sheet.getCell(2,1);

String a1 = a1.getContents();

String b2 = b2.getContents();

String c2 = c2.getContents();

// perform operations on strings

.........

However in case we need to access the cell's contents as the exact data type ie. as a numerical value or as a date, then the retrieved Cell must be cast to the correct type and the appropriate methods called. The code piece given below illustrates how JExcelApi may be used to retrieve a genuine java double and java.util.Date object from an Excel spreadsheet. For completeness the label is also cast to it's correct type. The code snippet also illustrates how to verify that cell is of the expected type - this can be useful when performing validations on the spreadsheet for presence of correct datatypes in the spreadsheet.

String a1 = null;

Double b2 = 0;

Date c2 = null;

Cell a1 = sheet.getCell(0,0);

Cell b2 = sheet.getCell(1,1);

Cell c2 = sheet.getCell(2,1);

if (a1.getType() == CellType.LABEL)

{

LabelCell lc = (LabelCell) a1;

stringa1 = lc.getString();

}

if (b2.getType() == CellType.NUMBER)

{

NumberCell nc = (NumberCell) b2;

numberb2 = nc.getValue();

}

if (c2.getType() == CellType.DATE)

{

DateCell dc = (DateCell) c2;

datec2 = dc.getDate();

}

// operate on dates and doubles

...

It is recommended to, use the close() method (as in the code piece below) when you are done with processing all the cells.This frees up any allocated memory used when reading spreadsheets and is particularly important when reading large spreadsheets.

// Finished - close the workbook and free up memory

workbook.close();

The API class files are availble in the 'jxl.jar', which is available for download.

Regards

Raghu

Former Member
0 Kudos

Thanks for your replies.

Well i have this question of using which APIs for reading thru the excel file, JExcelAPI/POI ...Any one of these prefered over the other???

Also, when I select the file and hit some button, the file browser field gets reset. How do I prevent that??

Thanks a lot.

Aakash

Former Member
0 Kudos

Hi Akash,

it can be done using JEXCel have a glance anthe following blog.

https://www.sdn.sap.com/irj/sdn/index?rid=/library/uuid/b030e7fb-2662-2b10-0dab-c4aa52c3550b

and can refer this thread also

Former Member
0 Kudos

You can use the open source Apache's POI excel API to read excel file and populate the table data.

Here is the link to download source and go through the docs

http://poi.apache.org/

Hope that helps!

Former Member
0 Kudos

Thanks Anu for your reply.

Can I use JExcelAPI to read the file instead of Apaches POI? Which one is normally prefered??

Also I saw one strange behaviour of the Web Dynpro FileUpload Element, once you select the file and hit some action button to process the file, the file path & the name goes off from the file browser element?

is there a way to prevent that?

Thanks again.