Import XML data in .NET, Java and other programming languages
EasyXLS™ library allows you to import data from XML file or from XML string.
The methods that import XML data accept any XML schema, but for the best result, a schema similar with EasyXLS.xsd should be used. It is preferable that the schema contain data in a table format simulating the rows and columns, such as the mentioned schema or such as an HTML table and CDATA tag. The schema should also mention EasyXLS word. The tags like table, row and data can be named differently, but the table alike format is important.
// Create an instance of the class that exports Excel files, having one sheet
ExcelDocument workbook = new ExcelDocument(1);
// Build XML stringString xml = "<?xml version='1.0'?><Table><![CDATA[Generated by EasyXLS]]>" +
"<Row><Data>Value 1</Data><Data>Value 2</Data></Row>" +
"<Row><Data>Value 3</Data><Data>Value 4</Data></Row>" +
"</Table>";
// Import data from an XML string into worksheet starting with A1 cell
ExcelWorksheet xlsWorksheet = (ExcelWorksheet) workbook.easy_getSheetAt(0);
xlsWorksheet.easy_insertXMLFromString(xml, "A1");
// Optionally, export Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Excel.xlsx");
' Create an instance of the class that exports Excel files, having one sheetDim workbook As New ExcelDocument(1)
' Build XML stringDim Xml = "<?xml version='1.0'?><Table><![CDATA[Generated by EasyXLS]]>" & vbCrLf & _
"<Row><Data>Value 1</Data><Data>Value 2</Data></Row>" & vbCrLf & _
"<Row><Data>Value 3</Data><Data>Value 4</Data></Row>" & vbCrLf & _
"</Table>"' Import data from an XML string into worksheet starting with A1 cell
Dim xlsWorksheet As ExcelWorksheet = workbook.easy_getSheetAt(0)
xlsWorksheet.easy_insertXMLFromString(Xml, "A1")
' Optionally, export Excel file
workbook.easy_WriteXLSXFile("C:\Samples\Excel.xlsx")
C++// Create an instance of the class that exports Excel files
EasyXLS::IExcelDocumentPtr workbook;
hr = CoCreateInstance(__uuidof(EasyXLS::ExcelDocument),
NULL,
CLSCTX_ALL,
__uuidof(EasyXLS::IExcelDocument),
(void**) &workbook) ;
// Create one sheet
workbook->easy_addWorksheet_2("My tab");
// Build XML stringchar* xml = (char*)malloc(536*sizeof(char));
strcpy(xml, "<?xml version='1.0'?><Table><![CDATA[Generated by EasyXLS]]>");
strcat(xml, "<Row><Data>Value 1</Data><Data>Value 2</Data></Row>");
strcat(xml, "<Row><Data>Value 3</Data><Data>Value 4</Data></Row>");
strcat(xml, "</Table>");
// Import data from an XML string into worksheet starting with A1 cell
EasyXLS::IExcelWorksheetPtr xlsWorksheet =
(EasyXLS::IExcelWorksheetPtr)workbook->easy_getSheetAt(0);
xlsWorksheet->easy_insertXMLFromString_3(xml, "A1");
// Optionally, export Excel file
workbook->easy_WriteXLSXFile("C:\\Samples\\Excel.xlsx");
C++.NET// Create an instance of the class that exports Excel files, having one sheet
ExcelDocument ^workbook = gcnew ExcelDocument(1);
// Build XML stringString ^xml = "<?xml version='1.0'?><Table><![CDATA[Generated by EasyXLS]]>"
"<Row><Data>Value 1</Data><Data>Value 2</Data></Row>"
"<Row><Data>Value 3</Data><Data>Value 4</Data></Row>"
"</Table>";
// Import data from an XML string into worksheet starting with A1 cell
ExcelWorksheet ^xlsWorksheet = safe_cast<ExcelWorksheet^>(workbook->easy_getSheetAt(0));
xlsWorksheet->easy_insertXMLFromString(xml, "A1");
// Optionally, export Excel file
workbook->easy_WriteXLSXFile("C:\\Samples\\Excel.xlsx");
// Create an instance of the class that exports Excel files, having one sheet
ExcelDocument workbook = new ExcelDocument(1);
// Build XML stringString xml = "<?xml version='1.0'?><Table><![CDATA[Generated by EasyXLS]]>" +
"<Row><Data>Value 1</Data><Data>Value 2</Data></Row>" +
"<Row><Data>Value 3</Data><Data>Value 4</Data></Row>" +
"</Table>";
// Import data from an XML string into worksheet starting with A1 cell
ExcelWorksheet xlsWorksheet = (ExcelWorksheet) workbook.easy_getSheetAt(0);
xlsWorksheet.easy_insertXMLFromString(xml, "A1");
// Optionally, export Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Excel.xlsx");
.NET:// Create an instance of the class that exports Excel files
$workbook = new COM("EasyXLS.ExcelDocument");
// Create one sheet
$workbook->easy_addWorksheet_2("My tab");
// Build XML string
$xml = "<?xml version='1.0'?><Table><![CDATA[Generated by EasyXLS]]>" .
"<Row><Data>Value 1</Data><Data>Value 2</Data></Row>" .
"<Row><Data>Value 3</Data><Data>Value 4</Data></Row>" .
"</Table>";
// Import data from an XML string into worksheet starting with A1 cell
$xlsWorksheet = $workbook->easy_getSheetAt(0);
$xlsWorksheet->easy_insertXMLFromString_3($xml, "A1");
// Optionally, export Excel file
$workbook->easy_WriteXLSXFile("C:\Samples\Excel.xlsx");
Java:// Create an instance of the class that exports Excel files
$workbook = new java("EasyXLS.ExcelDocument");
// Create one sheet
$workbook->easy_addWorksheet("My tab");
// Build XML string
$xml = "<?xml version='1.0'?><Table><![CDATA[Generated by EasyXLS]]>" .
"<Row><Data>Value 1</Data><Data>Value 2</Data></Row>" .
"<Row><Data>Value 3</Data><Data>Value 4</Data></Row>" .
"</Table>";
// Import data from an XML string into worksheet starting with A1 cell
$xlsWorksheet = $workbook->easy_getSheetAt(0);
$xlsWorksheet->easy_insertXMLFromString($xml, "A1");
// Optionally, export Excel file
$workbook->easy_WriteXLSXFile("C:\Samples\Excel.xlsx");
' Create an instance of the class that exports Excel filesset workbook = Server.CreateObject("EasyXLS.ExcelDocument")
' Create one sheet
workbook.easy_addWorksheet_2("My tab")
' Build XML stringDim xml: xml = "<?xml version='1.0'?><Table><![CDATA[Generated by EasyXLS]]>" & _
vbNewline & "<Row><Data>Value 1</Data><Data>Value 2</Data></Row>" & _
vbNewline & "<Row><Data>Value 3</Data><Data>Value 4</Data></Row>" & _
vbNewline & "</Table>"' Import data from an XML string into worksheet starting with A1 cell
set xlsWorksheet = workbook.easy_getSheetAt(0)
xlsWorksheet.easy_insertXMLFromString_3 xml, "A1"' Optionally, export Excel file
workbook.easy_WriteXLSXFile("C:\Samples\Excel.xlsx")
' Create an instance of the class that exports Excel filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Create one sheet
workbook.easy_addWorksheet_2 ("My tab")
' Build XML stringDim xml As String
xml = "<?xml version='1.0'?><Table><![CDATA[Generated by EasyXLS]]>" & vbCrLf _
& "<Row><Data>Value 1</Data><Data>Value 2</Data></Row>" & vbCrLf _
& "<Row><Data>Value 3</Data><Data>Value 4</Data></Row>" & vbCrLf _
& "</Table>"' Import data from an XML string into worksheet starting with A1 cellSet xlsWorksheet = workbook.easy_getSheetAt(0)
xlsWorksheet.easy_insertXMLFromString_3 xml, "A1"' Optionally, export Excel file
workbook.easy_WriteXLSXFile ("C:\Samples\Excel.xlsx")
' Create an instance of the class that exports Excel filesset workbook = CreateObject("EasyXLS.ExcelDocument")
' Create one sheet
workbook.easy_addWorksheet_2 ("My tab")
' Build XML stringDim xml
xml = "<?xml version='1.0'?><Table><![CDATA[Generated by EasyXLS]]>" & _
"<Row><Data>Value 1</Data><Data>Value 2</Data></Row>" & _
"<Row><Data>Value 3</Data><Data>Value 4</Data></Row>" & _
"</Table>"' Import data from an XML string into worksheet starting with A1 cellset xlsWorksheet = workbook.easy_getSheetAt(0)
xlsWorksheet.easy_insertXMLFromString_3 xml, "A1"' Optionally, export Excel file
workbook.easy_WriteXLSXFile("C:\Samples\Excel.xlsx")
<!-- Create an instance of the class that exports Excel files --><cfobject type="java"class="EasyXLS.ExcelDocument"name="workbook"action="CREATE">
<!-- Create one sheet --><cfset ret = workbook.easy_addWorksheet("My tab")><!-- Build XML string --><cfset xml = "<?xml version='1.0'?><Table><![CDATA[Generated by EasyXLS]]>" &
"<Row><Data>Value 1</Data><Data>Value 2</Data></Row>" &
"<Row><Data>Value 3</Data><Data>Value 4</Data></Row>" &
"</Table>"><!-- Import data from an XML string into worksheet starting with A1 cell --><cfset xlsWorksheet = workbook.easy_getSheetAt(0)><cfset xlsWorksheet.easy_insertXMLFromString(xml, "A1")><!-- Optionally, export Excel file --><cfset ret = workbook.easy_WriteXLSXFile("C:\Samples\Excel.xlsx")>
.NET:# Create an instance of the class that exports Excel files, having one sheet
workbook = ExcelDocument(1)
# Build XML string
xml = """<?xml version='1.0'?><Table><![CDATA[Generated by EasyXLS]]>
<Row><Data>Value 1</Data><Data>Value 2</Data></Row>
<Row><Data>Value 3</Data><Data>Value 4</Data></Row>
</Table>"""# Import data from an XML string into worksheet starting with A1 cell
xlsWorksheet = workbook.easy_getSheetAt(0)
xlsWorksheet.easy_insertXMLFromString(xml, "A1")
# Optionally, export Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Excel.xlsx")
Java:# Create an instance of the class that exports Excel files, having one sheet
workbook = gateway.jvm.ExcelDocument(1)
# Build XML string
xml = """<?xml version='1.0'?><Table><![CDATA[Generated by EasyXLS]]>
<Row><Data>Value 1</Data><Data>Value 2</Data></Row>
<Row><Data>Value 3</Data><Data>Value 4</Data></Row>
</Table>"""# Import data from an XML string into worksheet starting with A1 cell
xlsWorksheet = workbook.easy_getSheetAt(0)
xlsWorksheet.easy_insertXMLFromString(xml, "A1")
# Optionally, export Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Excel.xlsx")
Import data from XML string
EasyXLS library allows importing data from XML strings using ExcelWorksheet.easy_insertXMLFromString method. The above code sample shows how to achieve this goal.
Import data from XML file
EasyXLS library allows importing data from XML file using ExcelWorksheet.easy_insertXMLFromFile method similarly, as presented in the above code sample.
Import XML Spreadsheet file
EasyXLS allows you to import XML Spreadsheet Excel file.