office open xml

  • Thread starter Thread starter DG
  • Start date Start date
D

DG

Is there a documentation on how to build Word documents with just writing
XML?
Footer, header, tables, insert images, fonts, ect....
 
When I looked about 2 years ago, I couldn't find anything.

I did this with excel one time, and I basically just saved off a basic file,
opened it, and started coding it up to match.

Maybe this will get you started:


private XmlDocument m_resultDoc ;//= null;
private string m_xmlDocDefaultNamespace = string.Empty;
private string m_xmlDocExcelSpreadSheetNameSpace = string.Empty;

private readonly string EXCEL_XPATH_TABLE_DEFAULT =
"//ss:Workbook/ss:Worksheet/ss:Table";

private readonly string EXCEL_XPATH_ELEMENT_NAME_ROW = "Row";
private readonly string EXCEL_XPATH_ELEMENT_NAME_CELL = "Cell";
private readonly string EXCEL_XPATH_ELEMENT_NAME_DATA = "Data";
private readonly string EXCEL_XPATH_ELEMENT_NAME_TYPE = "Type";

private readonly string EXCEL_WORKSHEET_GENERICNAME_PREFIX = "WorkSheet";

private readonly string EXCEL_XPATH_ELEMENT_TYPE_VALUE_STRING = "String";
private readonly string EXCEL_XPATH_ELEMENT_TYPE_VALUE_NUMBER = "Number";

private readonly string EXCEL_XPATH_ATTRIBUTE_NAME_COLUMN = "Column";
private readonly string EXCEL_XPATH_ATTRIBUTE_NAME_STYLEID = "StyleID";
private readonly string EXCEL_XPATH_ATTRIBUTE_NAME_INDEX = "Index";
private readonly string EXCEL_XPATH_ATTRIBUTE_NAME_FORMULA = "Formula";
private readonly string EXCEL_XPATH_ATTRIBUTE_NAME_SS_NAME = "ss:Name";
private readonly string EXCEL_XPATH_ATTRIBUTE_NAME_SS_COLUMN =
"ss:Column";
private readonly string EXCEL_XPATH_ATTRIBUTE_NAME_SS_EXPCOLUMNCOUNT =
"ss:ExpandedColumnCount";
private readonly string EXCEL_XPATH_ATTRIBUTE_NAME_AUTOFITWIDTH =
"AutoFitWidth";
private readonly string EXCEL_XPATH_ATTRIBUTE_NAME_WIDTH = "Width";


private readonly string EXCEL_XPATH_ELEMENT_STYLE_VALUE_HEADER =
"sHeader";
private readonly string EXCEL_XPATH_ELEMENT_STYLE_VALUE_DETAILS =
"sDetails";
private readonly string EXCEL_XPATH_ELEMENT_STYLE_VALUE_SUMMARY =
"sSummary";
private readonly string EXCEL_XPATH_ELEMENT_STYLE_VALUE_SUMMARY_ALT =
"sSummaryAlt";

private readonly string EXCEL_NAMESPACE_PREFIX_OFFICE = "o";
private readonly string EXCEL_NAMESPACE_PREFIX_EXCEL = "x";
private readonly string EXCEL_NAMESPACE_PREFIX_SPREADSHEET = "ss";
//
private readonly string EXCEL_NAMESPACE_FULLNAME_OFFICE =
"urn:schemas-microsoft-com:office:office";
private readonly string EXCEL_NAMESPACE_FULLNAME_EXCEL =
"urn:schemas-microsoft-com:office:excel";
private readonly string EXCEL_NAMESPACE_FULLNAME_SPREADSHEET =
"urn:schemas-microsoft-com:office:spreadsheet";


public void
ExecuteViewer(MeasInc.ReportFrameworkCS.EventArgs.ReportEventArgs rargs)//,
object[] objs)
{

/*
m_multipleCallsErrorCheck+=1;
if (m_multipleCallsErrorCheck > 1 )
{
//this check is put in because there could possibly be
//multiple calls to this procedure by accident
throw new ArgumentException("The ExecuteViewer method was called more
than once. Adjust code so it is only called once");
}
*/


m_resultDoc = GetVirginExcelXmlDocument();

//Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager nsmgr = new
XmlNamespaceManager(m_resultDoc.NameTable);
nsmgr.AddNamespace (EXCEL_NAMESPACE_PREFIX_OFFICE,
EXCEL_NAMESPACE_FULLNAME_OFFICE);
nsmgr.AddNamespace(EXCEL_NAMESPACE_PREFIX_EXCEL,
EXCEL_NAMESPACE_FULLNAME_EXCEL);
nsmgr.AddNamespace(EXCEL_NAMESPACE_PREFIX_SPREADSHEET,
EXCEL_NAMESPACE_FULLNAME_SPREADSHEET);


XmlElement root = m_resultDoc.DocumentElement;

XmlNodeList tableLevel;
XmlNode tableNode;

tableLevel = m_resultDoc.SelectNodes(EXCEL_XPATH_TABLE_DEFAULT, nsmgr);
tableNode = tableLevel[0];

m_xmlDocExcelSpreadSheetNameSpace =
root.GetNamespaceOfPrefix(EXCEL_NAMESPACE_PREFIX_SPREADSHEET);

m_xmlDocDefaultNamespace = root.NamespaceURI;

// InsertOtherStuff(tableNode); Now you have a virgin excel document (as
xml )... do something with it here


}

private XmlDocument GetVirginExcelXmlDocument ()
{

//This procedure gets a plain/jane (no data) version of an Excel
spreadsheet, but in a xml format
//Some of the "styles" are precoded, and reflect the CONST's above

XmlDocument returnDoc = new XmlDocument();

System.Text.StringBuilder sb = new System.Text.StringBuilder();

sb.Append("<?xml version=\"1.0\"?>");
sb.Append("<Workbook
xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sb.Append(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");


sb.Append(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
sb.Append(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sb.Append(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");

sb.Append(" <Styles>");
sb.Append(" <Style ss:ID=\"Default\" ss:Name=\"Normal\">");
sb.Append(" <Alignment ss:Vertical=\"Bottom\"/>");
sb.Append(" <Borders/>");
sb.Append(" <Font/>");
sb.Append(" <Interior/>");
sb.Append(" <NumberFormat/>");
sb.Append(" <Protection/>");
sb.Append(" </Style>");


sb.Append(" <Style ss:ID=\"sHeader\">");
sb.Append(" <Alignment ss:Horizontal=\"Left\" ss:Vertical=\"Bottom\"
ss:WrapText=\"1\"/>");
sb.Append(" <Borders>");
sb.Append(" <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" </Borders>");
sb.Append(" <Interior ss:Color=\"#FFFFCC\" ss:Pattern=\"Solid\"/>");
sb.Append(" </Style>");

sb.Append(" <Style ss:ID=\"sSummary\">");
sb.Append(" <Alignment ss:Horizontal=\"Left\" ss:Vertical=\"Bottom\"
ss:WrapText=\"1\"/>");
sb.Append(" <Borders>");
sb.Append(" <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" </Borders>");
sb.Append(" <Interior ss:Color=\"#B6B6B6\" ss:Pattern=\"Solid\"/>");
sb.Append(" </Style>");

sb.Append(" <Style ss:ID=\"sSummaryAlt\">");
sb.Append(" <Alignment ss:Horizontal=\"Left\" ss:Vertical=\"Bottom\"
ss:WrapText=\"1\"/>");
sb.Append(" <Borders>");
sb.Append(" <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" </Borders>");
sb.Append(" <Interior ss:Color=\"#C0C0C0\" ss:Pattern=\"Solid\"/>");
sb.Append(" </Style>");


sb.Append(" <Style ss:ID=\"sDetails\">");
sb.Append(" <Alignment ss:Horizontal=\"Left\" ss:Vertical=\"Bottom\"
ss:WrapText=\"1\"/>");
sb.Append(" <Font ss:FontName=\"Arial Unicode MS\"
x:Family=\"Swiss\"/>");
sb.Append(" </Style>");
sb.Append(" </Styles>");
sb.Append(" <Worksheet ss:Name=\"WorkSheet1\">");
sb.Append(" <Table ss:ExpandedColumnCount=\"1\"
ss:ExpandedRowCount=\"92\" x:FullColumns=\"1\" x:FullRows=\"1\">");

sb.Append(" </Table>");
sb.Append(" </Worksheet>");
sb.Append(" </Workbook>");

returnDoc.LoadXml(sb.ToString());
return returnDoc;
}
 
You might find some hints at:
http://www.sqlservercentral.com/col...eragingxpexcelxmlandopenxmlfordataimports.asp

where I do the "reverse" of generation, I read the excel (as xml).




sloan said:
When I looked about 2 years ago, I couldn't find anything.

I did this with excel one time, and I basically just saved off a basic file,
opened it, and started coding it up to match.

Maybe this will get you started:


private XmlDocument m_resultDoc ;//= null;
private string m_xmlDocDefaultNamespace = string.Empty;
private string m_xmlDocExcelSpreadSheetNameSpace = string.Empty;

private readonly string EXCEL_XPATH_TABLE_DEFAULT =
"//ss:Workbook/ss:Worksheet/ss:Table";

private readonly string EXCEL_XPATH_ELEMENT_NAME_ROW = "Row";
private readonly string EXCEL_XPATH_ELEMENT_NAME_CELL = "Cell";
private readonly string EXCEL_XPATH_ELEMENT_NAME_DATA = "Data";
private readonly string EXCEL_XPATH_ELEMENT_NAME_TYPE = "Type";

private readonly string EXCEL_WORKSHEET_GENERICNAME_PREFIX = "WorkSheet";

private readonly string EXCEL_XPATH_ELEMENT_TYPE_VALUE_STRING = "String";
private readonly string EXCEL_XPATH_ELEMENT_TYPE_VALUE_NUMBER = "Number";

private readonly string EXCEL_XPATH_ATTRIBUTE_NAME_COLUMN = "Column";
private readonly string EXCEL_XPATH_ATTRIBUTE_NAME_STYLEID = "StyleID";
private readonly string EXCEL_XPATH_ATTRIBUTE_NAME_INDEX = "Index";
private readonly string EXCEL_XPATH_ATTRIBUTE_NAME_FORMULA = "Formula";
private readonly string EXCEL_XPATH_ATTRIBUTE_NAME_SS_NAME = "ss:Name";
private readonly string EXCEL_XPATH_ATTRIBUTE_NAME_SS_COLUMN =
"ss:Column";
private readonly string EXCEL_XPATH_ATTRIBUTE_NAME_SS_EXPCOLUMNCOUNT =
"ss:ExpandedColumnCount";
private readonly string EXCEL_XPATH_ATTRIBUTE_NAME_AUTOFITWIDTH =
"AutoFitWidth";
private readonly string EXCEL_XPATH_ATTRIBUTE_NAME_WIDTH = "Width";


private readonly string EXCEL_XPATH_ELEMENT_STYLE_VALUE_HEADER =
"sHeader";
private readonly string EXCEL_XPATH_ELEMENT_STYLE_VALUE_DETAILS =
"sDetails";
private readonly string EXCEL_XPATH_ELEMENT_STYLE_VALUE_SUMMARY =
"sSummary";
private readonly string EXCEL_XPATH_ELEMENT_STYLE_VALUE_SUMMARY_ALT =
"sSummaryAlt";

private readonly string EXCEL_NAMESPACE_PREFIX_OFFICE = "o";
private readonly string EXCEL_NAMESPACE_PREFIX_EXCEL = "x";
private readonly string EXCEL_NAMESPACE_PREFIX_SPREADSHEET = "ss";
//
private readonly string EXCEL_NAMESPACE_FULLNAME_OFFICE =
"urn:schemas-microsoft-com:office:office";
private readonly string EXCEL_NAMESPACE_FULLNAME_EXCEL =
"urn:schemas-microsoft-com:office:excel";
private readonly string EXCEL_NAMESPACE_FULLNAME_SPREADSHEET =
"urn:schemas-microsoft-com:office:spreadsheet";


public void
ExecuteViewer(MeasInc.ReportFrameworkCS.EventArgs.ReportEventArgs rargs)//,
object[] objs)
{

/*
m_multipleCallsErrorCheck+=1;
if (m_multipleCallsErrorCheck > 1 )
{
//this check is put in because there could possibly be
//multiple calls to this procedure by accident
throw new ArgumentException("The ExecuteViewer method was called more
than once. Adjust code so it is only called once");
}
*/


m_resultDoc = GetVirginExcelXmlDocument();

//Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager nsmgr = new
XmlNamespaceManager(m_resultDoc.NameTable);
nsmgr.AddNamespace (EXCEL_NAMESPACE_PREFIX_OFFICE,
EXCEL_NAMESPACE_FULLNAME_OFFICE);
nsmgr.AddNamespace(EXCEL_NAMESPACE_PREFIX_EXCEL,
EXCEL_NAMESPACE_FULLNAME_EXCEL);
nsmgr.AddNamespace(EXCEL_NAMESPACE_PREFIX_SPREADSHEET,
EXCEL_NAMESPACE_FULLNAME_SPREADSHEET);


XmlElement root = m_resultDoc.DocumentElement;

XmlNodeList tableLevel;
XmlNode tableNode;

tableLevel = m_resultDoc.SelectNodes(EXCEL_XPATH_TABLE_DEFAULT, nsmgr);
tableNode = tableLevel[0];

m_xmlDocExcelSpreadSheetNameSpace =
root.GetNamespaceOfPrefix(EXCEL_NAMESPACE_PREFIX_SPREADSHEET);

m_xmlDocDefaultNamespace = root.NamespaceURI;

// InsertOtherStuff(tableNode); Now you have a virgin excel document (as
xml )... do something with it here


}

private XmlDocument GetVirginExcelXmlDocument ()
{

//This procedure gets a plain/jane (no data) version of an Excel
spreadsheet, but in a xml format
//Some of the "styles" are precoded, and reflect the CONST's above

XmlDocument returnDoc = new XmlDocument();

System.Text.StringBuilder sb = new System.Text.StringBuilder();

sb.Append("<?xml version=\"1.0\"?>");
sb.Append("<Workbook
xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sb.Append(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");


sb.Append(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
sb.Append(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sb.Append(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");

sb.Append(" <Styles>");
sb.Append(" <Style ss:ID=\"Default\" ss:Name=\"Normal\">");
sb.Append(" <Alignment ss:Vertical=\"Bottom\"/>");
sb.Append(" <Borders/>");
sb.Append(" <Font/>");
sb.Append(" <Interior/>");
sb.Append(" <NumberFormat/>");
sb.Append(" <Protection/>");
sb.Append(" </Style>");


sb.Append(" <Style ss:ID=\"sHeader\">");
sb.Append(" <Alignment ss:Horizontal=\"Left\" ss:Vertical=\"Bottom\"
ss:WrapText=\"1\"/>");
sb.Append(" <Borders>");
sb.Append(" <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" </Borders>");
sb.Append(" <Interior ss:Color=\"#FFFFCC\" ss:Pattern=\"Solid\"/>");
sb.Append(" </Style>");

sb.Append(" <Style ss:ID=\"sSummary\">");
sb.Append(" <Alignment ss:Horizontal=\"Left\" ss:Vertical=\"Bottom\"
ss:WrapText=\"1\"/>");
sb.Append(" <Borders>");
sb.Append(" <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" </Borders>");
sb.Append(" <Interior ss:Color=\"#B6B6B6\" ss:Pattern=\"Solid\"/>");
sb.Append(" </Style>");

sb.Append(" <Style ss:ID=\"sSummaryAlt\">");
sb.Append(" <Alignment ss:Horizontal=\"Left\" ss:Vertical=\"Bottom\"
ss:WrapText=\"1\"/>");
sb.Append(" <Borders>");
sb.Append(" <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\"
ss:Weight=\"1\"/>");
sb.Append(" </Borders>");
sb.Append(" <Interior ss:Color=\"#C0C0C0\" ss:Pattern=\"Solid\"/>");
sb.Append(" </Style>");


sb.Append(" <Style ss:ID=\"sDetails\">");
sb.Append(" <Alignment ss:Horizontal=\"Left\" ss:Vertical=\"Bottom\"
ss:WrapText=\"1\"/>");
sb.Append(" <Font ss:FontName=\"Arial Unicode MS\"
x:Family=\"Swiss\"/>");
sb.Append(" </Style>");
sb.Append(" </Styles>");
sb.Append(" <Worksheet ss:Name=\"WorkSheet1\">");
sb.Append(" <Table ss:ExpandedColumnCount=\"1\"
ss:ExpandedRowCount=\"92\" x:FullColumns=\"1\" x:FullRows=\"1\">");

sb.Append(" </Table>");
sb.Append(" </Worksheet>");
sb.Append(" </Workbook>");

returnDoc.LoadXml(sb.ToString());
return returnDoc;
}


DG said:
Is there a documentation on how to build Word documents with just writing
XML?
Footer, header, tables, insert images, fonts, ect....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top