on 09-30-2008 4:37 AM
Dear Friends,
I'm developing a new screen which will upload excel file from client to server system. Before uploading, i need to show the excel file data to the user in a table.
I designed the view(HCFileUploadDataView) with the following UIelements :
- FileUploadUI element - for selecting file.
- Retrieve button - onClick, will populate excel data in table.
- Table - display excel data to user.
- Upload button - for uploading file.
Coding part:
1). wdDoInit method :
public void wdDoInit()
{
wdThis.wdGetFileUploadCustomControllerController().wdGetContext().currentContextElement().setRowCount(0);
IWDAttributeInfo attributeInfo = wdContext.getNodeInfo().getAttribute(IPrivateHCFileUploadDataView.IContextElement.FILE_RESOURCE);
ISimpleTypeModifiable type = attributeInfo.getModifiableSimpleType();
IWDModifiableBinaryType binaryType = (IWDModifiableBinaryType)type;
}
2). Retrieve method :
public void onActionRetriveExcelData(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
{
//@@begin onActionRetriveExcelData(ServerEvent)
Logger logger = LoggerFactory.getInstance().getLogger(HCFileUploadDataView.class);
IPrivateHCFileUploadDataView.IContextElement element = null;
String fileName = null;
FileOutputStream fos = null;
ArrayList arrList = null;
try
{
element = wdContext.currentContextElement();
if(element.getFileResource() != null)
{
fileName = wdContext.currentContextElement().getFileName();
logger.debug("input file name = " + fileName);
if (!fileName.equals("") && !fileName.equals(null) && fileName.equalsIgnoreCase("input.xls"))
{
arrList = ExcelHCFileReader.loadDataGrid(new ByteArrayInputStream(element.getFileResource()));
logger.debug("arrList size = " + arrList.size());
if(arrList == null)
{
wdComponentAPI.getMessageManager().reportWarning("oops.. file was empty..");
}
else
{
Collection hcFileUPloadTables = new ArrayList();
Iterator itrHCTable = arrList.iterator();
HCFileUploadDO hcFileUploadDO = null;
hcFileUploadDO = new HCFileUploadDO();
int i = arrList.size();
while(itrHCTable.hasNext())
{
IPrivateHCFileUploadDataView.IHCFileUploadTblElement tblElement = wdContext.createHCFileUploadTblElement();
hcFileUploadDO = (HCFileUploadDO)itrHCTable.next();
// SEQUENCE_NO
String strSequenceNo = hcFileUploadDO.getStr_SequenceNo();
tblElement.setSequenceNo(strSequenceNo);
// AS_TO_DATE
String strAsToDate = hcFileUploadDO.getStr_AsToDate();
tblElement.setAsToDate(strAsToDate);
// HDR_ENT
String strHdrEnt = hcFileUploadDO.getStr_HdrEnt();
tblElement.setHdrEnt(strHdrEnt);
hcFileUPloadTables.add(tblElement);
wdContext.nodeHCFileUploadTbl().bind(hcFileUPloadTables);
}
catch(Exception exception)
{
wdComponentAPI.getMessageManager().reportException( exception.getMessage(), true );
wdComponentAPI.getMessageManager().raisePendingException();
}
element.setFileResource(null);
onclick of retrieve button the above action method(onActionRetriveExcelData) called, and i'm able to populate excel file data into table.
3). Upload method :
public void onActionUploadFile(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
{
//@@begin onActionUploadFile(ServerEvent)
Logger logger = LoggerFactory.getInstance().getLogger(HCFileUploadDataView.class);
IPrivateHCFileUploadDataView.IContextElement element = null;
FileOutputStream fos = null;
String fileName = null;
try
{
element = wdContext.currentContextElement();
if(element.getFileResource() != null)
{
byte fileArray[] = element.getFileResource();
fos = new FileOutputStream(new File("E:/input.xls"));
fos.write(fileArray);
fos.close();
wdComponentAPI.getMessageManager().reportSuccess("File Uploaded Successfully ");
}
}
catch(Exception exception)
{
wdComponentAPI.getMessageManager().reportException( exception.getMessage(), true );
wdComponentAPI.getMessageManager().raisePendingException();
}
//@@end
}
Problem with above upload action method. I'm not able to upload the file. Because element.getFileResource() is coming as null. But element.getFileResource() is returning a value in retrieve method(onActionRetriveExcelData).
Could anyone help me how to resolve this ?
Thanks
Vijay.
Hi Mohamad,
According to me it is possible. But i am not sure. How it works.I need to be find out. I will check and if I do it I will let you know.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi all,
Thanks for the help.
Problem resolved.
Regards,
Vijay.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi
I think you have solved your problem or else you can just go through the following link which shows how create a directory on server and upload the file at specified location in server.
[https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/607e9392-8167-2b10-b9a8-e0aa297e88c6]
Regards
Raghu
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
refer this link.
step by step process with screen shots.
Regards,
Sunaina Reddy T
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi
Check the following code using POI API to read the data from Excel
try {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row;
HSSFCell cell;
int rows; // No of rows
rows = sheet.getPhysicalNumberOfRows();
int cols = 0; // No of columns
int tmp = 0;
// This trick ensures that we get the data properly even if it doesn't start from first few rows
for(int i = 0; i < 10 || i < rows; i++) {
row = sheet.getRow(i);
if(row != null) {
tmp = sheet.getRow(i).getPhysicalNumberOfCells();
if(tmp > cols) cols = tmp;
}
}
for(int r = 0; r < rows; r++) {
row = sheet.getRow(r);
if(row != null) {
for(int c = 0; c < cols; c++) {
cell = row.getCell((short)c);
if(cell != null) {
// Your code here
}
}
}
}
} catch(Exception ioe) {
ioe.printStackTrace();
}
Regards
Raghu
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Question is not answered
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
hi!
yes it is possible to show the data before uploading .you can export it in excel sheet .i have exported it in after executing the bapi . i am sending you the code .
wdThis.wdGetExportExcelComponentController().executeBapi_Flight_Getlist_Input();
//give a file name
String fileName = "vishal" + ".xls";
IWDCachedWebResource cachedExcelResource = null;
try
{
//create file
java.io.File f = new java.io.File("output.xls");
//create workbook
WritableWorkbook workbook = Workbook.createWorkbook(f);
//provide font
WritableFont red = new WritableFont(WritableFont.ARIAL,
WritableFont.DEFAULT_POINT_SIZE,
WritableFont.BOLD,
false,
UnderlineStyle.SINGLE,
Colour.DARK_BLUE);
WritableCellFormat redFormat = new WritableCellFormat(red);
WritableFont blue = new WritableFont(WritableFont.ARIAL,
WritableFont.DEFAULT_POINT_SIZE,
WritableFont.NO_BOLD,
false,
UnderlineStyle.NO_UNDERLINE,
Colour.BLACK);
WritableCellFormat blueFormat = new WritableCellFormat(blue);
WritableSheet sheet = workbook.createSheet("First Sheet", 0);
WritableSheet sheet2 = workbook.createSheet("second Sheet", 1);
jxl.write.Label label = new jxl.write.Label(0, 0, "ID", redFormat);
sheet.addCell(label);
label = new jxl.write.Label(1,0,"No",redFormat);
sheet.addCell(label);
label = new jxl.write.Label(2,0,"Arrival city",redFormat);
sheet.addCell(label);
label = new jxl.write.Label(3,0,"Depart",redFormat);
sheet.addCell(label);
label = new jxl.write.Label(4,0,"Depart. city",redFormat);
sheet.addCell(label);
label = new jxl.write.Label(5,0,"Curr.",redFormat);
sheet.addCell(label);
label = new jxl.write.Label(6, 0,"Airline",redFormat);
sheet.addCell(label);
label = new jxl.write.Label(7,0,"Airport",redFormat);
sheet.addCell(label);
label = new jxl.write.Label(8,0,"ISO",redFormat);
sheet.addCell(label);
label = new jxl.write.Label(9,0,"Apt",redFormat);
sheet.addCell(label);
label = new jxl.write.Label(10,0,"Airfare",redFormat);
sheet.addCell(label);
label = new jxl.write.Label(11,0,"Arrival Date",redFormat);
sheet.addCell(label);
label = new jxl.write.Label(12,0,"Arrival",redFormat);
sheet.addCell(label);
label = new jxl.write.Label(13,0,"Date",redFormat);
sheet.addCell(label);
for(int j=1;j<wdContext.nodeFlight_List().size();j++)
{
for(int i=0;i<13;)
{
label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getAirlineid(),blueFormat);
sheet.addCell(label);
label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getConnectid(),blueFormat);
sheet.addCell(label);
label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getCityto(),blueFormat);
sheet.addCell(label);
label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getDeptime().toString(),blueFormat);
sheet.addCell(label);
label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getCityfrom(),blueFormat);
sheet.addCell(label);
label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getCurr(),blueFormat);
sheet.addCell(label);
label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getAirline(),blueFormat);
sheet.addCell(label);
label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getAirportfr(),blueFormat);
sheet.addCell(label);
label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getAirportto().toString(),blueFormat);
sheet.addCell(label);
label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getCurr_Iso().toString(),blueFormat);
sheet.addCell(label);
label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getPrice().toString(),blueFormat);
sheet.addCell(label);
label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getArrdate().toString(),blueFormat);
sheet.addCell(label);
label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getArrtime().toString(),blueFormat);
sheet.addCell(label);
label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getFlightdate().toString(),blueFormat);
sheet.addCell(label);
}
}
workbook.setColourRGB(Colour.LIME, 0xff, 0, 0);
workbook.write();
FileInputStream excelCSVFile = new FileInputStream(f);
cachedExcelResource =
getCachedWebResource(
excelCSVFile,
fileName,
WDWebResourceType.getWebResourceType(
"xls",
"application/ms-excel"));
wdContext.currentContextElement().setFileLocation(cachedExcelResource.getURL());
workbook.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
the above code is to show the data in excel sheet.
but for uploading you need to have a context attribute say resource of type com.sap.ide.webdynpro.uielementdefinitions.Resource
hope this will help you to resolve your problem.
thanks
vishal
Edited by: Vishal Anand on Sep 30, 2008 8:37 AM
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Vishal,
This code for showing the input excel file data to the user in another excel file. am I correct ?
What do you mean by bapi ? As I am new to webdynpro i do not anything about bapi.
can u please be more specific about what you did ?
Moreover, my application used POI for reading excel data.
Regards,
Vijay.
hi!
bapi are Business Application Programming Interfaces , this is sap interface to get the data from backend.to know more you can go through this link
http://help.sap.com/saphelp_nw04/helpdata/en/5c/f3f0371bc15d73e10000009b38f8cf/frameset.htm
in my application i retrived the data from bapi and shown in the table and then exported in the excel sheet.
thanks
vishal
Hi Mohamad,
Please check the type of the context attribute that you have created.It should be binary. Also check that this attribute is mapped the FileUpLoad UI element .
Regards
Sagar Ingalwar
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
24 | |
12 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.