Example that uses the OpenXmlDocument, WordprocessingML, and SpreadsheetML Classes (Dec 19, 2007)
This page presents simple uses of the OpenXmlDocument, WordprocessingML, and SpreadsheetML classes.
This blog is inactive.
New blog: EricWhite.com/blog
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.Examples.LtxOpenXml;
class Program
{
public static void WriteParagraphs(string filename)
{
Console.WriteLine("Printing Paragraphs");
Console.WriteLine("===================");
Console.WriteLine();
using (WordprocessingML doc = new WordprocessingML(filename))
{
foreach (var p in doc.Paragraphs())
{
Console.WriteLine("Style: {0} Text: >{1}<",
p.StyleName.PadRight(16), p.Text);
foreach (var c in p.Comments())
{
Console.WriteLine(" Comment:");
Console.WriteLine(" Id: {0}", c.Id);
Console.WriteLine(" Author: {0}", c.Author);
Console.WriteLine(" Text: >{0}<", c.Text);
}
}
}
Console.WriteLine();
}
static void PrintRelationships(List<Relationship> source, int indentLevel)
{
string indent = "".PadRight(indentLevel * 2);
foreach (var r in source)
{
Console.WriteLine("{0}Id: {1}",
indent, r.Id);
Console.WriteLine("{0}RelationshipType: {1}",
indent, r.RelationshipType);
Console.WriteLine("{0}ContentType: {1}",
indent, r.ContentType);
Console.WriteLine("{0}SourceUri: {1}",
indent, r.SourceUri);
Console.WriteLine("{0}TargetUri: {1}",
indent, r.TargetUri);
Console.WriteLine("{0}TargetMode: {1}",
indent, r.TargetMode);
if (r.XDocument != null)
Console.WriteLine("{0}XDocument descendant node count: {1}",
indent, r.XDocument.DescendantNodes().Count());
Console.WriteLine();
if (r.Relationships != null)
PrintRelationships(r.Relationships, indentLevel + 1);
}
}
static void PrintAllRelationships(string filename)
{
Console.WriteLine("Printing Relationships");
Console.WriteLine("======================");
Console.WriteLine();
using (SpreadsheetML doc = new SpreadsheetML(filename))
{
PrintRelationships(doc.Relationships, 0);
}
}
static void PrintCells(string filename)
{
Console.WriteLine("Printing Cells of Spreadsheet");
Console.WriteLine("=============================");
Console.WriteLine();
using (SpreadsheetML doc = new SpreadsheetML(filename))
{
foreach (var sh1 in doc.Sheets())
{
Console.WriteLine("Sheet");
Console.WriteLine("=====");
Console.WriteLine("Name:{0}", sh1.Name);
Console.WriteLine("Id:{0}", sh1.Id);
Console.WriteLine("SheetId:{0}", sh1.SheetId);
foreach (var row in sh1.Rows())
{
Console.WriteLine(" RowId:{0}", row.RowId);
Console.WriteLine(" Spans:{0}", row.Spans);
foreach (var cell in row.Cells())
{
Console.WriteLine(" Column:{0}", cell.Column);
if (cell.Type != null)
Console.WriteLine(" Type:{0}", cell.Type);
if (cell.Value != null)
Console.WriteLine(" Value:{0}", cell.Value);
if (cell.Formula != null)
Console.WriteLine(" Formula:>{0}<", cell.Formula);
if (cell.SharedString != null)
Console.WriteLine(" SharedString:>{0}<", cell.SharedString);
}
}
Console.WriteLine();
}
}
}
public static void Main(string[] args)
{
PrintAllRelationships("Book1.xlsx");
PrintAllRelationships("OfficeXMLMarkupExplained_en.docx");
PrintAllRelationships("Test.docx");
WriteParagraphs("Test.docx");
PrintCells("Book1.xlsx");
}
}
Comments
- Anonymous
December 19, 2007
This post describes the SpreadsheetML class (derives from the OpenXmlDocument class), which presents