Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

How to create/update a csv file in repository from a data table thing

Duartefe
12-Amethyst

How to create/update a csv file in repository from a data table thing

Hi Everyone,

 

I have a thing which is a data table and this data table has a service which update its lines from some mashup inputs every time a click a button. It is working fine.

Now I need to write the same information that is in the data table to a csv file in a repository. I can either export a csv file after update the data table or update the csv.

Which one is the better approach and how can I do that?

 

Thanks in advance!

Felipe Duarte

ACCEPTED SOLUTION

Accepted Solutions

Hello @Duartefe,

 

For me the best approach is to append a new line(s) to the CSV file when you add a record to the Table. It would benefit from a better performance. When you update a DataTable and then query the DataTable and export to CSV, then it's overloading the memory and the DataTable itself for a longer perion and in a bigger scope.

However, it also brings additional effort to provide consistency between DataTable and CSV file also when removing the record and updating it.

 

To simply append a new row to the existing file in SystemRepository, you can simply invoke a service AppendToTextFile:

 

 Things["SystemRepository"].AppendToTextFile({
	path: "export.csv", // file has to exist in the repository!
	data: field1 + "," + field2 + "\n" // and so on...
});

 

Remember about handling new lines and about commas.

Hope that this helps, if you have a following questions, don't hesitate to ask.

 

BR,

JK.

 

View solution in original post

3 REPLIES 3

Hello @Duartefe,

 

For me the best approach is to append a new line(s) to the CSV file when you add a record to the Table. It would benefit from a better performance. When you update a DataTable and then query the DataTable and export to CSV, then it's overloading the memory and the DataTable itself for a longer perion and in a bigger scope.

However, it also brings additional effort to provide consistency between DataTable and CSV file also when removing the record and updating it.

 

To simply append a new row to the existing file in SystemRepository, you can simply invoke a service AppendToTextFile:

 

 Things["SystemRepository"].AppendToTextFile({
	path: "export.csv", // file has to exist in the repository!
	data: field1 + "," + field2 + "\n" // and so on...
});

 

Remember about handling new lines and about commas.

Hope that this helps, if you have a following questions, don't hesitate to ask.

 

BR,

JK.

 

Hi JK,

 

Thanks for the reply first of all.

 

So, we got a good start now. At least the file is being populated but this way to write in data is confuse and it always write at the same line and randomly. How to fix the values for each column in csv and also how to change for the next line to write in a new line.

 

Thanks,

Felipe Duarte

Duartefe
12-Amethyst
(To:Duartefe)

Hi JK,

 

It works! I was forgetting the "\n".

 

Thank you very much!

 

Best Regards,

Felipe Duarte

Announcements


Top Tags