Embed XML into Excel worksheet - c#

I do have an XML file which contains several calculated values along with a list of items such as:
<?xml version="1.0" encoding="utf-8"?>
<XmlContent>
<Elements>
<CreationDate>...</CreationDate>
<Filename>....</Filename>
</Elements>
<PersonItems>
<PersonItem>
<FirstName>...</FirstName>
<LastName>...</LastName>
<Speed>...</Speed>
</PersonItem>
<PersonItem>
<FirstName>...</FirstName>
<LastName>...</LastName>
<Speed>...</Speed>
</PersonItem>
[...]
</PersonItems>
</XmlContent>
Now the values should be presented in an Excel sheet using Excel 2007 OpenXmlFormat. The calculated values should be mapped to a specific cell within the worksheet along PersonItems should be bound to a table within the same worksheet.
Is there a way to embed the XML file into the worksheet package and bind the values to the appropriate fields by using the c# Package API as a CustomXmlPart?
I found an example on Channel9 where Matthew Scott made something similar with Word 2007 by using the Word Content Control Toolkit. However, this only works with Word.
Is there something similar for Excel?
Or is there even a better approach for solving this task?

Well, after research and playing around with XML mapping I came to the conclusion that there is not "easy" way to achieve that what I am longing to do.
When importing XML Content within the Excel application, Excel creates an XML mapping definition and stores it within the Package. The XML content itself will be splitted by using the mapping definition and merged within the contents of the Excel file.
This means, that the XML file itself vanishes right after import and can no longer be used.
However, the intentional scenario is possbible with Word 2007+ as you can see in the links above.
Maybe this information is helpful for anybody who has a similar task like me.

The easiest way is to Parse the xml file(SAX,DOM), get the corresponding attributes and values, then write those to an excel file.

Related

Importing data into Excel using XML and Excel Templates

I am generating CSV data from a C# application. This can be imported into Excel easily but I need formatting applied to the file.
One option is interop but the machine running this application will not have Office products installed so that is out.
I've been told that XML can work with Excel templates and am looking for a starter example on how to achieve this.
I have generated excel spread sheets using the excel 2003 xml format several times but you will have to consider the following features that cannot be supported using this format:
This XML Spreadsheet 2003 file format (.xml) does not retain the following features:
Auditing tracer arrows
Chart and other graphic objects
Chart sheets, macro sheets, dialog sheets
Custom views
Data consolidation references
Drawing object layers
Outlining and grouping features
Password-protected worksheet data
Scenarios
User-defined function categories
VBA projects
If that is acceptable you can use as someone suggest an open source library that allows you to generate the spreadsheet in code or as I have done you can generate the xml using either an xml transform or a using the spark template engine. Both have worked for me in the past but using the spark view engine was probably the nicest.
The best way to achieve either of these is to create a template the way you want it to look and save it as a Excel 2003 Xml format and look at the raw xml. This should make it easy for you to generate your output. You can also download the xml definition for reference.
You can use excellent OpenXML wrapper ClosedXML to generate xlsx files with formatting. Or if you want, you can use pure OpenXML. OpenXML installation is required for ClosedXML to work.

How do you create an .xslt file from an Excel spreadsheet?

I'm working on a C# application that is going to save a DataSet to an Excel file.
I have found several examples of how to do this, but they all require you to have an xslt style sheet. I already have an existing Excel spreadsheet with all the worksheets and columns created. Is there an easy way to create an .xslt file from my existing Excel spreadsheet?
An example.
First of all, your question is really confusing and hard to understand. You might want to rewrite it to be a bit more specific. If you really have two or three questions, then ask each one on a separate page.
Step 1. You said that you have an Excel file. Open it up in a text editor and look at it. If it is in XML format, then the XSL that you need should be obvious because your job is to convert the existing XML dataset into the Excel XML format.
Step 2. XSL Templates are not magic. They are actually a form of programming language that is executed by an XLST engine. Of course it is possible to write a program that can compare two XML formats and generate an XSL template that would transform one into another, but that would be a very complex program and it would probably have some rigid requirements on the type of XML files that it would work with. I doubt that anyone has created such a tool that you can leverage.
Step 3. Get a decent tool that allows you to apply an XSL template to your dataset so that you can test it without a lot of work. Personally I use Netbeans with an XML Debug plugin, but there are numerous other tools out there. In fact, you could just write a simple transform tool yourself that just runs the XSLT on a sample dataset and then opens it up in both Excel and a text editor.

Generating Excel file from HTML

I'm aware that I can generate an HTML <table> and save it as an .xls file to read into Excel, and that works fine and all, but it only gives me one sheet.
Is there a way to generate HTML so that I can have multiple sheets in a single .xls file? I've tried to simply generate more then one <table>, but they just end up getting appended to eachother.
Short Answer: No.
Longer Answer: You cannot cause an HTML generated page to split into multiple worksheets in an Excel file. Further, the HTML you generate for even a single page could cause Excel to choke on certain machines as it does the conversion when the file is loaded. We've seen a number of low powered machines take upwards of 5 minutes to show a HTML file in excel (simple table with rows/columns, nothing fancy) depending on size.
Better Answer: Use a third party product like ClosedXML or FileHelpers to generate a proper xlsx file.
there seems to be way though I didn't try it, see http://www.c-sharpcorner.com/UploadFile/kaushikborah28/79Nick08302007171404PM/79Nick.aspx and check the Worksheets attribute
check the official documentation at http://msdn.microsoft.com/en-us/library/Aa155477%28office.10%29.aspx
After installing the Help file you can find an example of a file with 3 Worksheets in XML Reference / Excel Workbook...
You can use open source ClosedXML, а wrapper around OpenXML to conveniently generate xlsx files - i.e. Office 2007+ format Excel files.

open xml to query excel cells

In the past, I have created a component to pass and retrieve values to/from excel using the excel libraries. The good thing is that once you have your workbook in memory and modify a cell (let's call it the origin cell) all the other cells with formulas that take this origin cell value are automatically refreshed.
Is this possible in OpenXml?
As far as I see, apparently this doesn't happen in OpenXml because the excel engine is not really executed in the background, OpenXml is just a group of classes to serialize, deserialize, read etc xml files right?
That's correct, Office Open XML SDK is just a set of libraries to read/write XML files. It does not have any functionality for performing calculations.
You can specify that Excel should recalculate everything upon load by setting the following attribute, but if you need to read the new values in code (prior to re-opening in Excel) this won't help.
<workbook>
<calcPr fullCalcOnLoad="1"/>
</workbook>
Or in code with the Office Open XML SDK..
using (var doc = SpreadsheetDocument.Open(path, false))
{
doc.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;
.
.
.
}

Best tools to create valid XML files from an Excel file

I need to create a script that extracts some data from a complex Excel 2003 file (with multiple sheets and different tables inside a single sheet) and produces different XML files that need to be validated against a given XSD file.
My preferred language is Python;
to create and validate XML files i would go with lxml.
What do you suggest for parsing XLS files?
Is xlrd the right tool to use for complex Excel files?
Or do i need to convert all the sheets in CSV manually, and read files line by line, splitting and getting data?
I accept C#, VB6, VBA suggestions too.
[disclaimer: I'm the author of xlrd]
xlrd is quite suited for this kind of job. Get the latest version from PyPI. Get the flavour from the tutorial found here. XLSX support is in alpha test; e-mail me if you need it. The awkwardness and lossiness of the save-as-CSV approach was one of the things that prompted me to write xlrd.
Xlrd is OK. We use it extensively to import XLS files full of references and formulas with multiple sheets and data presented in custom (not Latin-1) encoding.
I am convinced the most simple solution for this task is using Excel VBA together with MSXML parser. Look here for some links how to use the MSXML parser in VBA for reading XML files; you can adopt this easily for writing XML files, I think.
I cant answer whether xlrd/python is the right tool for the job - as I don't know python well enough.
But there are many ways to access the excel data...in the main you have VBA built directly in to Excel.
Then you have Ado.net See David Hayden's article here which allows you to access the data via any DotNet language...even IronPython

Categories