Quantcast
Channel: ClosedXML - The easy way to OpenXML
Viewing all articles
Browse latest Browse all 1877

New Post: can I import xml and create xlsx?

$
0
0
I suggest you define a class that represents a row in the file.
Let's say you call that class MyExcelRow.
For each row define a property to store each column value where the name of the property is the name of the column.
Make the order of the properties the same as the order you want the columns to be in the Excel file.

Use an XML reader to read the XML file and create a MyExcelRow object for each row, and add them to a generic List<MyExcelRow> collection.
Once you have read in the entire file, all you have to do is call InsertTable() with your list and voila!
ClosedXML will fill the worksheet with your data automatically.
It will also automatically add the column headings

See this the documentation here https://closedxml.codeplex.com/wikipage?title=Inserting%20Tables

Here is some sample code that assumes that GetXMLData returns a list of MyExcelRow objects that were read from the XML file.
// Read XML Data
List<MyExcelRow> myData = GetXMLData();
// Create an Excel Workbook
XLWorkbook workbook = new XLWorkbook();
// Add the worksheet
IXLWorksheet sheet = workbook.Worksheets.Add("My Data");
// Add the XML data to the Excel sheet beginning in cell A1
sheet.Cell(1, 1).InsertTable(myData);
You can then save the Excel file to disk, or return it as a memory stream to your browser.
That's all there is to it.

Viewing all articles
Browse latest Browse all 1877

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>