Write one cell from datatable to Excel

I have a SQL query that outputs to a datatable . One column repeats the name of the contract number down every row (called 123456, for example). I then write the datatable to Excel. I want to take one occurrence of the 123456 contract number and in cell A5 of the Excel spreadsheet write the following “Contract Number : 123456”
Can anyone assist this struggling UIPath rookie?
Thanks in advance,

Hi @mark_newman

I guess we can help you…we just need a bit more information.
So…contract number is a fixed value, right?..and you already wrote the dt to an Excel sheet…?
Do you want to write just in that one specified cell “A5”?

you would want to use something like this: dtSQLQuery.Rows(0)(“column_name”).ToString where 0 is the index number of the first row ( so row 2 would be 1) and the Column Name to be replaced with the column name or you can use the index as well i.e. dtSQLQuery.Rows(0)(0).ToString the first row and first column.

Since it is the same value in your query dt column, then you can take the first occurrence ie. the value in the first row and corresponding column then set equal to a variable which you then write to cell a5.

This is kinda what everyone is saying in one way or another.

Thanks for the fast replies! To clarify for @RoboHeart,
So…contract number is a fixed value, right? Yes that is correct
And you already wrote the dt to an Excel sheet? Yes
Do you want to write just in that one specified cell “A5”? Yes
To explain a little further - the SQL pulls back 30 columns of data. Columns 1-26 is data that is used to populate columns 1-26 in the Excel spreadsheet. Columns 27-30 are header values that need to placed in single cells above the detail from columns 1-26. So, 27 could be contract number that needs to go to cell A5, 28 could be contract name that goes to A6, etc.

I that case I would use the “Write Range” and “Write Cell” activities from the UiPath.Excel.Activities package as well as the “Filter Data Table” activity to reduce the table by the column you want to use for the headers.
With the first one you can easily write your dt into the sheet and with the second the headers in the related cells based on the example of cwedl.

dt_SqlQuery.rows(0).items(26).ToString --> "A5"
or short
dt_SqlQuery.rows(0)(26).ToString --> "A5"

I got it working! Thank you all very much for the help.

1 Like