Excel Automation: read a csv file and update an Excel file - c#

I have an automated test which produces a csv with two columns of data.
I have an excel file which I use to gather the results of all the runs in a worksheet.
I want to fully automate the process of updating the Excel file after each test run.
This is probably what I want to do:
1. Read the two columns from the CSV file
2. Paste the two columns in a worksheet in the excel file, in the first
empty column to the right of the existing block of columns.
3. Save the Excel file
EDIT:
Now i understand that i can do steps 1-3 using a macro.
All that is left for me to figure out is how to launch the macro.

You can read the csv and write to the Excel sheet with a single data provider, the OleDb provider. Here is an article on how to write to Excel, and here is one on how to read csv.

Once you write the macro, it should be in the macro list in Excel for that Excel document. You can bind the macro to a keystroke (like Alt-i, Alt-whatever) so that it runs when your press that key combination.
I've done this on a couple of similar projects where I need to import CSV files from other sources and put the data into an Excel sheet (or sheets)

Related

Update Stylesheet of an existing Excel file using openXML

I am trying to copy an Excel workbook with 4 worksheets (tabs) where 1 worksheet is macro enabled with many formulas using the OpenXML SAX approach as I have lot of data for each sheet.
If I copy the file and and start writing into the worksheets, it starts writing from the 1st line and the headers are being deleted. So I tried copying just the macro-enabled sheet and add other sheets later and add styles to headers.
When I try doing it, the system give an error saying only one style sheet is allowed. Only one instance of the type is allowed for this parent.
How can I access the existing stylesheet and add more styling into it for other tabs?

Create NI TestStand Sequence file from Excel file

I have a Microsoft Excel file which have two columns and sixteen rows of Data. As image attached.
Excel Reference Image to import sequence data to NI TestStand
Please I need help to Import excel data to NI TestStand sequence file. This excel file for just reference only.
Please let me know if there is any confusion. Thanks
Is the data you are referring to in the Excel file properties? Or did you want to actually create Test Steps from Excel file?
If it's properties to load, then use the Property Loader Step. To make it easier, you can do an export which will create the XML layout which you can modify the data with the one in the Excel.
As far as creating a Test Step based on Excel file, that's not how TestStand works.
You can do this with Import Properties.
I would first create a test sequence as a model and export the properties from it. Then you can use that as a template to import the properties you have in your excel file.
From what I see from your example though. It may be easier to just manually create the sequences and steps, if that's all there is. If you have to create 300 steps or sequences it may be a better idea to utilize loops or create a module.

Excel formula calculation using OpenXML

I have scenario where I have to prepare two batch of excel files. I have two templates. I am automating a process. The 1st Excel file has formulas in it. I populate data using C# winform and OpenXML library in the first excel file. I want to have the formulas calculate results and then grab them to populate the 2nd excel file. I have tried various methods but was unsuccessful. I did use the following code hoping it will make the excel file calculate the formulas in it.
SpreadSheetDocument.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
SpreadSheetDocument.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;
So basically the process should be
Make copy of the 1st excel file and populate it with data [This part is working]
Some how make the 1st excel file data calculate the formulas [This is the step I have problem with]
Copy the formula results values to the 2nd excel file [This part is working but as formula is not calculated the values are not being copied over]
I am reluctant to use excel interlop library as closing the excel does not work as well.

Approach for exporting the .csv data to excel workbook

I was trying to import data set to excel via creating a temporary .csv file in between to speed up the process.
Approach used:
Create a .csv file with the required data.
Open this .csv file using OpenText() function and save it as exel file.
Delete the temp. created .csv file.
Now the problem is when user opts for export to workbook option in that case i had to keep the newly created workbook open to the user as per my application requirement. But since as per my approach file is still opened in the excel workbook. So it's not allowing me the delete the temporary .csv file for obvious reason.
Can anyone suggest me another approach to do this. For exporting i had to use .csv option only, using library closedXML is not in the picture here.
Thanks in advance

How to read a row in an Excel sheet and cut it?

I wanna read rows in a single column and put that in to a string variable. I take one row at a time. When i read a one row, i wanna delete that row from the sheet and save the excel file. Second time, it skips the empty row and takes the next row. Deletes it and saves the file. This goes on..How can i achieve that using C#??
If you just wanted to read from Excel, I would suggest Linq to Excel
However, since you also want to update the Excel document, the best places to get started for free are either Excel Interop (link to follow shortly) or OLEDB connection to Excel.
If you have a little budget, Spreadsheet Gear is well worth investigating.

Categories