First for context, I am a placement student on the automation team of a software development company, so I help develop the automated test programs on our daily builds.
My line manager has produced an excel document which will be used to present the results from our test environments. Our tests run on virtual PC's on a hyper-V server.
I want to be able to update this excel document with the results of tests from each of the test environments, but I'm not sure how I should go about doing this.
Each of the virtual PC's runs an automation program (C# .NET 4.0), and currently copies the test logs and result files to a shared folder on our build machine, which is accessible by everyone.
So I want the automation program to run its tests, then when it has finished, I want it to write to the table in the excel document with the test results. Has anyone any experience with modifying excel documents programmatically? I should also note that the excel document will be stored in a shared folder on a remote virtual PC (accessible from the test environment)
The table will contain the build name/date, environment details, type of test and the test result etc.
I already collect this information and present it in HTML files - but there is a separate HTML file for each test environment. The goal of this is so that the results will all be in one place (The excel document will take care of presenting the data in the tables in the form of graphs and charts).
So really, all I need to know is how to write to a table in an excel document from a .NET program. There seems to be different ways of doing this (as I found when I researched it online), so I want to ask stackoverflow to see who knows the best way to do this).
Have a look at this.
Hope this help.
There are a lot of methods, indeed. You can use OLEDB, or you can use interop (look at
get_Range()
andListObjects["table_name"].DataBodyRange
).When I was forced to create Excel files based on a template, I used XML maps -- it's very handy. After you bind an xsd to your workbook, you can just load an xml into it and Excel will place the data to the right cells. (If you need an example, just leave a comment and I'll update the answer with it.)
But personally I agree with mj82. If you can avoid writing to an Excel file -- avoid it. You can bind a table ('list' in 2003) to some external datasource and set the workbook to autoupdate on open. If it's possible in your environment that would be the best solution.
UPDATE: To use xml maps for pushing data into an Excel workbook, follow the next steps
Create an xml schema (xsd file). For example:
Open the workbook, add your xsd as a map and map cells to elements as described in ms help.
Then in you code: Create a string with xml data. (I used typed DataSets, so I just called GetXml.) Open the workbook and push the data to Excel:
And that's all. Hope this helps.
OfficeWriter is another 3rd party library for working programmatically with XLS/XLSX (and Word). It's mature, at version 8.0 now.
http://www.officewriter.com
I use and am happy with EPPlus http://epplus.codeplex.com/ for parsing, editing and creating xlsx files.
This question had helped me out: Create Excel (.XLS and .XLSX) file from C#