Round-tripping XML -> Excel -> XML - c#

I'd like to use Excel's XML Map feature from server-side C# in a web app. XML maps enable you to associate an XML schema with a workbook and specify which cells map to which parts of the schema. From there you can import XML files to update cell values, and export XML containing the latest values (i.e. if they have been manually changed). You can do this manually on the Developer tab in Excel.
We already have a licence for Aspose Cells, but (despite some recent work in this area) it doesn't seem to fully support XML maps. Has anyone had any success with this?
I've created a test using Excel Interop, which works (i.e. can set up an XML Map programatically and use it to both import and export values). It boils down to this:
XmlMap map = workbook.XmlMaps.Add(xmlSchema, Missing.Value);
// map cell C3 to the question text
var worksheet = (Worksheet)workbook.Worksheets[1];
Range cell = (Range)worksheet.Cells[3, 3];
cell.XPath.Clear(); // just in case
cell.XPath.SetValue(map, "/Root/Question/Text", Missing.Value, false);
// import the XML to populate the mapped cells
map.ImportXml(xmlData, true);
However, that's not really suitable for use on a server because it relies on running the Excel process in the background.
Is it possible to use Microsoft's Open XML SDK to import or export XML via an XML Map? I've found the Map class, but I think that just represents the metadata for a map that already exists. Does the SDK contain this sort of transformation logic or does it just represent a worksheet's content as classes?

To further providing you details about XMLMaps feature, currently, Aspose.Cells only supports the following options regarding XML Maps:
It can preserve XmlMaps in the template file and re-save the file with it.
It supports to Import XML Mapping from external sources.
It can remove or check the XML mappings.
Currently it does not support the following at the moment:
It cannot add XML Maps to the Excel file.
It does not export XML Maps to save an external file (We will try to support exporting xml Maps in the second quarter of 2013)
My name is Nayyer and I am developer evangelist at Aspose.

Related

How to paste a specific range from one excel sheet to another as Paste Special - Linked Picture using C#

I am trying to including a table or specific range from one sheet into another sheet as an image/picture.
In Excel it is done by selecting the entire table from sheet2 and then on sheet1: Paste Special - Linked Picture.
Is there a way to get this done using C#? I am developing a project which requires this task.
I'm not sure about converting the contents to a picture, but you can read and write to/from a spreadsheet using the Open XML SDK (link). I've used it to manipulate Word documents, but I imagine it functions similarly for spreadsheets.

Embed XML into Excel worksheet

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.

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.

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;
.
.
.
}

Import XML file to excel with existing map using ExcelPackage in C#

How can I import xml file to existing excel template. I have a map (xsd).
It has to be done on server. I used excelPackage , but couldnt find any documentation for the classes.
More specific question: how do I write in C# the following code from VB.NET
Dim xmlSchema As String = "c:\Schema1.xsd"
Map = XL.ActiveWorkbook.XmlMaps.Add(xmlSchema)
Wb.XmlImportXml(xmlStr, oMap)
There is extensive documentation and tutorials and everything on the Codeplex site for ExcelPackage:
http://excelpackage.codeplex.com/
As for your question:
if you have a XSD file, you can easily deserialize your XML file into an object graph
parse the objects as needed and insert the relevant information into a new Excel sheet based on your template
Your question is way too broad and not specific enough to be able to help with it - you'll need to make your question more specific and provide more information!

Categories