How to write DataRow into Excel

Hi,

How can I write DataRow variable to new excel sheet or in text file? Please suggest the solution.

Thanks,
Saili

@saili

  1. First create one DataTable using Build DataTable activity and then pass that DataRow to that DataTable using Add DataRow activity.

  2. And then use Write Range activity to write into Excel file.

@saili,

Create a datatable and add the datarow to that datatable, then using write range activity write that data table to the excel.

For text file, you can add the datarow into a datatable, then using output datatable activity you can get the output string of the datatable, then write that string into a textfile using write text file activity.

Hi @lakshman,

Actually I loop through datarow array and I need seperate row from it.

1 Like

@saili

Do you mean you want to filter some rows from original input file and want to write into new excel file ?

Yes. I have original excel file which I converted into datatable. I apply some filter on it and store filtered data in dataRow array. Then I loop through that array using for each row activity. Now I need that, the single row in foreach loop write to excel / text file.

Fine
hope these steps would help you resolve this
–use a FOR EACH activity and pass the array of datarow as input and change the type argument as system.data.datarow in the property panel
–inside the loop use ADD DATAROW activity
hope we have a datatable created with build datatable activity or already existing activity with a variable named Finaldt
–now in add datarow activty mentionn the datarow property mention as item and in the datatabble mention as Finaldt

Cheers @saili

@saili

You can convert that Array of DataRows into DataTable directly using CopyToDataTable method.

@lakshman

I already did it. While I am writing i to excel getting this error :
Assign: The source contains a detached DataRow that cannot be copied to the DataTable.

1 Like

@saili

Could you please show me screenshot of that expression how you are writing it here. So that i can check and help you.

@Palaniyappan

I am getting error that this row is already belongs to another table.

1 Like

Fine
i hope you must have use select method and thus it gives us array of datarow
in that case this expression would help you
yourdatatablename = yourdatatablename.Select(“yourcondition”).Asenumerable().Take(1).CopyToDatable()

Cheers @saili

1 Like

fine in that case mention the variable item in ARRAYROW propertty like this
item.ItemArray

Cheers @saili

okk. thanks @Palaniyappan. What is Take(1) here. Can you explain?

1 Like

sure
it wiill take the first row alone and copy that to the datatable
Cheers @saili

But I want all selected rows from DataRow array.

@saili

Could you please show me screenshot of that expression how you are writing it here. So that i can check and help you.

it was mentioned like single row buddy
fine no worries, these steps would help you resolve this

–use a FOR EACH activity and pass the array of datarow as input and change the type argument as system.data.datarow in the property panel
–inside the loop use ADD DATAROW activity
hope we have a datatable created with build datatable activity or already existing activity with a variable named Finaldt
–now in add datarow activty mention the arrayrow property mention as item.ItemArray and in the datatabble mention as Finaldt

Cheers @saili

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.