cancel
Showing results for 
Search instead for 
Did you mean: 

How can I populate a table control with a text file?

Former Member
0 Kudos

Hi,

I would like to know how can I put the content of a csv file in a table control? This file is on my computer.

I use a upload file control for upload the csv file in a context variable. How can I use this context variable for populate the table control?

Thank you

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

What exactly is the problem?

You need to populate a context node "Rows" of cardinality 0:N with the rows of the text file.

If the max. number of columns is bound, let's say at most 10, and all columns have type "string", you can define a context node with 10 attributes A_1, ..., A_10 of type "string" at design time in the IDE.

After having read the CSV file, for each row create a node element e and set attribute e.A_i to the value in column i.

Define an IWDTable with 10 columns, bind the table's data source to the context node "Rows", create 10 IWDTableColumns, use an IWDTextView as table cell editor and bind it's text-property to attribute Rows.A_i.

If the number of columns is less than 10, set the visibility of all unnecessary columns to "Visibility.NONE".

A more sophisticated solution could create the context attributes dynamically, depending on the real number of columns, or could create context attributes of suitable type, if you have this type info.

Armin

Former Member
0 Kudos

Thank you,

It works but I want to know how can I read the csv file from my computer. I upload the file in the contect variable with a upload file control. It's correct?

Regards,

Patrick

Former Member
0 Kudos

Hi

Yes you can do it that way.

ravi

Former Member
0 Kudos

Hi,

I can upload the file in a context variable but I don't know how read the content of this file in this variable.

Thank you

Patrick

Former Member
0 Kudos

Hi

Read the file using this

	wdComponentAPI.getComponent().getMessageManager().reportSuccess("Inside File Read...");
FileInputStream fin = new FileInputStream("C:/<foldername>/testexcel.xls");
byte b[] = new byte[fin.available()];
fin.read(b,0,b.length);
fin.close();
wdComponentAPI.getComponent().getMessageManager().reportSuccess("File closed after reading...");
element.setDocumentContent(b);

Let me know if that helped you.

regards

ravi

Former Member
0 Kudos

The file-upload UI element stores the file content in a context attribute of DDIC type "binary" which corresponds to byte[] at runtime. There is a constructor in class String which takes a byte array and a charset like UTF-8 which may be used to get a string representation of the file content.

Armin

Former Member
0 Kudos

Hi,

It's exactly what I want to do. Do you have a code example for this constructor for read the content of the file in the context?

Thank you,

Patrick

Former Member
0 Kudos

hi

Say you have value node called "DocumentSourceNode" containing a context attribute called "DocumentContent"

//Then you can read the content

wdContext.currentDocumentSourceNode().getDocumentContent();

This returns a byte array.Well assuming you already have the context filled with data. This will fetch data only then. If its a different scenario let me know.

regards

ravi

Former Member
0 Kudos

Suppose you have a root attribute "FileContent" of type "binary" and bound the "data" property of the FileUpload element to this attribute.

byte[] content = wdContext.currentContextElement().getFileContent();
String contentAsString = new String(content, "UTF-8");

Armin

Former Member
0 Kudos

Hi,

It's work with the constructor String(byte[] content)

Thank you very much for your help

Patrick

Former Member
0 Kudos

Hi

You would like to do something similar to what you do in ABAP. Well you can read the csv file , but for mapping the respective columns you need to know the attributes of the column fields coming from the file meaning you need to know what is the first column field length and so on. Based on this you need to write a logic that would split the columns based on the offset and then accordingly place it in the Table UI.

By the way are the number of columns always fixed or how is it.

If they are fixed then its enough if you set the text of the column fields at runtime.

Hope that was helpful.

regards

ravi

Former Member
0 Kudos

Thank you,

I know the lenght of the column fields but when I want binding the table control to the context variable, the path and the name is displayed in the first line on each column. How can I format the context variable for populate the control?

regard,

Patrick

Former Member
0 Kudos

Hi

Say your value node is "TableNode". It has 3 columns namely col1, col2 and col3. Now this is bound to the Table ui element right.

Read the data from the csv file and split them based on the offset information you have. ( I guess that logic should be straightforward).

Ensure you store all the columns from the file in a separate context so that it is easy to loop through it .

Now once that is done

IPrivate<ViewName>.ITableNodeElement elt = wdContext.nodeTableNode().createTableNodeElement();

wdContext.nodeTableNode().addElement(elt);

wdContext.currentTableNode().setCol1("col1 from csv");

wdContext.currentTableNode().setCol2("col2 from csv");

wdContext.currentTableNode().setCol3("col3 from csv");

wdContext.nodeTableNode().moveNext();

Hope that helps. This will add the columns to the table.

I guess i have just written it in a hurry.Hope it gives you an idea on what to do. In case you require more info do let me know.

regards

ravi