📄 wkstoxml.c
字号:
// TRUE/FALSE
//------------------------------------------------------------------------------------------
static BOOL getAttributeByName(Object& node, string strAttribute, string& strValue)
{
//use Xpath syntax @attributeName to search the attribute node of current node
Object attr;
string strQuery;
strQuery.Format("@%s", strAttribute);
attr = node.selectSingleNode(strQuery);
if( attr )
{
strValue = attr.nodeValue;
return TRUE;
}
return FALSE;
}
//------------------------------------------------------------------------------------------
//setColumnValue(Object& colNode, Object& datasetNode, Column& colObj)
// populate an Origin column from XML document's Column node object
//Arguments:
// colNode: the Column node
// datasetNode: the Dataset node, child node of the Column node
// colObj: Origin column object in worksheet
//------------------------------------------------------------------------------------------
static void setColumnValue(Object& colNode, Object& datasetNode, Column& colObj)
{
string strName;
if( getAttributeByName(colNode, NAME_ATTRIBUTE, strName) )
colObj.SetName(strName);
string strLabel;
if( getAttributeByName(colNode, LABEL_ATTRIBUTE, strLabel) )
colObj.SetLabel(strLabel);
string strColFormat;
if( getAttributeByName(colNode, FORMAT_ATTRIBUTE, strColFormat) )
colObj.SetFormat(atoi(strColFormat));
else
colObj.SetFormat(OKCOLTYPE_NUMERIC); //numeric as the default
string strColType;
if( getAttributeByName(colNode, TYPE_ATTRIBUTE, strColType) )
colObj.SetType(atoi(strColType));
else
colObj.SetType(OKDATAOBJ_DESIGNATION_Y); //Y as the default
string strInternalType;
if( getAttributeByName(colNode, INTERNAL_DATATYPE_ATTRIBUTE, strInternalType) )
{
colObj.SetInternalData(atoi(strInternalType));
}
else
{
colObj.SetInternalData(FSI_DOUBLE);
}
//get byte stream from XML document
vector<byte> vv;
vv = datasetNode.nodeTypedValue;
//restore dataset by calling SetBytes()
Dataset dataset(colObj);
dataset.SetBytes(vv);
}
//------------------------------------------------------------------------------------------
//WKSToXML(string strWks = "Data1", string strXMLFile ="c:\\Data1.xml")
// The function to export a worksheet into XML document
//Arguments:
// strXMLFile: the full path to the XML file
// strWks: the worksheet name, the worksheet should exist to run this sample
//------------------------------------------------------------------------------------------
void WKSToXML(string strWks = "Data1", string strXMLFile ="c:\\Data1.xml")
{
if( !getXMLParser(g_XMLParser) )
return;
Object pInstruction;
pInstruction = g_XMLParser.createProcessingInstruction(XML_INSTRUCTION_TAG, XML_INSTRACTION_CONTENTS);
if( !pInstruction)
{
printf("Fail to create process instruction node\n");
return;
}
g_XMLParser.appendChild(pInstruction);
Worksheet wks(strWks);
if(!wks)
{
printf("Worksheet %s not found\n", strWks);
return;
}
Object root = g_XMLParser.createElement(WORKSHEET_TAG);
if( !root )
{
printf("Fail to create root element\n");
return;
}
//set attributs of root element, worksheet properties
int nNumOfColumns = addWorksheetInformation(root, wks);
g_XMLParser.appendChild(root);
for( int ii=0; ii<nNumOfColumns; ii++ )
{
//first, create the node for column
Object colNode = g_XMLParser.createElement(COLUMN_TAG);
if( !colNode )
{
printf("Fail to create Column element\n");
continue;
}
//Get the column
Column colObj = wks.Columns(ii);
//retrieve information from this column and save into XML node
//if the column can not be converted, skip to next column
if( !addColumnInformation(colNode, colObj) )
continue;
//append column node to the document
root.appendChild(colNode);
}
//save document to file
g_XMLParser.save(strXMLFile);
printf("XML document is created as %s\n", strXMLFile);
}
//------------------------------------------------------------------------------------------
//addWorksheetInformation(Object& wksNode, Worksheet& wksObj)
// Add worksheet name and number of columns to root node's attribute list while setting
//other attributes of <Worksheet> node
//Arguments:
// wksNode: the Worksheet node in XML document
// wksObj: the worksheet object from Origin
//Return:
// an integer number to indicate number of columns in the worksheet
//------------------------------------------------------------------------------------------
static int addWorksheetInformation(Object& wksNode, Worksheet& wksObj)
{
string strName = wksObj.GetPage().GetName();
int nNumCols = wksObj.GetNumCols();
string strAppPath = GetAppPath();
string strSchemaLocation;
strSchemaLocation.Format("%s%s%s", strAppPath, RELATIVE_PATH_TO_SCHEMA, SCHEMA_DEFINITION);
//set attributs of root element, worksheet properties
wksNode.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema");
wksNode.setAttribute("xsi:schemaLocation", strSchemaLocation);
wksNode.setAttribute("xmlns:dt", "urn:schemas-microsoft-com:datatypes");
wksNode.setAttribute(NAME_ATTRIBUTE, strName);
wksNode.setAttribute(NUMOFCOLS_ATTRIBUTE, nNumCols);
return nNumCols;
}
//------------------------------------------------------------------------------------------
//addColumnInformation(Object& colNode, Column& colObj)
// Export an Origin column into an XML Column node including column data and information
//Arguments:
// colNode: the Column node in XML document
// colObj: the column object from Origin
//Return:
// TRUE/FALSE
//------------------------------------------------------------------------------------------
static BOOL addColumnInformation(Object& colNode, Column& colObj)
{
if( !colObj )
{
printf("Invalid column object\n");
return FALSE;
}
int nDataType = colObj.GetInternalData();
//get all information to be saved in the column node
string strColName;
colObj.GetName(strColName);
//the converting from worksheet to XML document can not handle
//the following type of columns right now
// FSI_CHAR,
// FSI_TEXT,
// FSI_MIXED
if( nDataType == FSI_CHAR || nDataType == FSI_TEXT || nDataType == FSI_MIXED )
{
printf("Fail to export coulmn \"%s\"!!! Please make sure it is Numeric type\n", strColName);
return FALSE;
}
//cloumn Name
colNode.setAttribute(NAME_ATTRIBUTE, strColName);
//column Label
string strLabel;
colObj.GetLabel(strLabel);
colNode.setAttribute(LABEL_ATTRIBUTE, strLabel);
//column Format
int nFormat = colObj.GetFormat();
colNode.setAttribute(FORMAT_ATTRIBUTE, nFormat);
//column Type
int nType = colObj.GetType();
colNode.setAttribute(TYPE_ATTRIBUTE, nType);
//construct a dataset object from column object
Dataset datSet(colObj);
//column NumRows
int nSize =datSet.GetSize();
colNode.setAttribute(NUMROWS_ATTRIBUTE, nSize);
//set internal data type
colNode.setAttribute(INTERNAL_DATATYPE_ATTRIBUTE, nDataType);
Object datasetNode;
datasetNode = g_XMLParser.createElement(DATASET_TAG);
if( !datasetNode )
{
printf("Fail to create Dataset element\n");
return FALSE;
}
else
{
//set the dataType to be bin.base64
string strType = BIN_BASE64_DATATYPE;
datasetNode.dataType = strType;
//for bin.base64 data type, XML requires data to be in
//a variant with byte array. the variant needs to
//VT_UI1 | VT_ARRAY type
//To get proper data, first, get a byte vector from the dataset,
//then, get the variant data from the byte vector
vector<byte> vv;
datSet.GetBytes(vv);
_VARIANT varData = vv.GetDataAsOneDimensionalArray();
datasetNode.nodeTypedValue = varData;
//append datasetNode to column node
colNode.appendChild(datasetNode);
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -