GilShalit
GilShalit

Reputation: 6463

How do I load an XML doc into Excel using the Open XML SDK and .Net

In a WPF desktop application, I need to create an Excel 2007/2010 on a production server without office installed. I have a bunch of XML files which I want to load into separate pages of the workbook, mimicking the 'Data>Get External Data>From XML Import' operation, and resulting in something like:

The Excel worksheet

I think my best bet would be the Open XML SDK, but am not sure this is possible.

Upvotes: 0

Views: 1784

Answers (2)

jklemmack
jklemmack

Reputation: 3636

I'd suggest looking at ClosedXML (horrible name) for doing high-level Excel file manipulation through the OpenXML standard. It abstracts out a lot of the complexity of dealing directly with the OpenXML interfaces.

Tiny example:

var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add("Sample Sheet");
worksheet.Cell("A1").Value = "Hello World!";
workbook.SaveAs("HelloWorld.xlsx");

Upvotes: 1

Rikard Pavelic
Rikard Pavelic

Reputation: 496

You could use Templater. Disclaimer: I'm the author of the library.

The code would look something like this:

var dataSet = new DataSet();
dataSet.ReadXml("test1.xml");
dataSet.ReadXml("test2.xml");
using (var doc = Configuration.Factory.Open("test.xlsx"))
    doc.Process(dataSet);
Process.Start("test.xlsx");

Upvotes: 0

Related Questions