4

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).

Ciaran Gallagher
  • 3,895
  • 9
  • 53
  • 97
  • If it's in shared location, what if the file will be opened by somebody and you try do insert something in it? Excel is not good as a database, if there are better solution... I would recommend you to use MS Access, create a table for results, C# work with MS Access database via ADO is easy and well-known over the internet. As for results - you can design with few clicks a query in Excel to connect to Access and get all the data you need into it. – mj82 May 07 '11 at 20:01
  • 1
    @user: please don't put " (C# .NET)" in your titles. Just leave that information in the tags. – John Saunders May 07 '11 at 20:37
  • @mj82, the file only gives write permissions to those who require it, including the user where the automation program is written. Excel files can still be written to if it is already being accessed in read-only mode. – Ciaran Gallagher Sep 07 '13 at 10:08

3 Answers3

6

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#

Community
  • 1
  • 1
Matt
  • 902
  • 7
  • 11
  • 1
    I ended up using the EPPPlus library in my automation program and it has worked out quite well. It's very easy to use and set up - it's exactly what I needed. Only problem I experienced was that formatting cells didn't seem to work for me, but I solved this by setting conditional formatting on cells in my spreadsheet. Thanks for this I'd recommend this library to anyone else as it's the easiest to implement. – Ciaran Gallagher May 18 '11 at 13:26
2

There are a lot of methods, indeed. You can use OLEDB, or you can use interop (look at get_Range() and ListObjects["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:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="BILLSTRINGS" targetNamespace="http://tempuri.org/BILLSTRINGS.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/BILLSTRINGS.xsd" xmlns:mstns="http://tempuri.org/BILLSTRINGS.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="BILLSTRINGS">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="STRINGS" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="ID_GOOD" type="xs:string" />
                            <xs:element name="NAME_GOOD" type="xs:string" />
                            <xs:element name="Quantity" type="xs:string" />
                            <xs:element name="Price" type="xs:string" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

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:

using OExcel = Microsoft.Office.Interop.Excel;
//...
OExcel.Application app = new OExcel.ApplicationClass(); 
OExcel.Workbook wb = app.Workbooks.Open(filepath, false, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
wb.XmlMaps["MAPNAME"].ImportXml(string_with_xml);

And that's all. Hope this helps.

Dmitry
  • 3,069
  • 1
  • 17
  • 26
  • This sounds like it might be the easiest option! If you could provide me with an example that would be fantastic. Thanks for the help it's very much appreciated. – Ciaran Gallagher May 07 '11 at 22:23
  • I've updated the answer with an example of using xml maps, enjoy. – Dmitry May 08 '11 at 09:00
0

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

Eisbaer
  • 189
  • 1
  • 6