views:

352

answers:

1

I'm trying to make a report generator inside of a C# application for my boss, I came across this page and looked into RichTextBoxes and think that I can build on this idea to do what my boss is looking for. http://openxmldeveloper.org/articles/OpenXMLDocFromDotNet.aspx

The issue I'm running into is their example code for the XML portion assumed you were creating an application in an Office 2007 beta. The schema listed here doesn't work for retail Office 2007. Can anyone show me where I can look to find out more about schema in general, or explain what the code is doing here? Alternatively, if anyone has a different suggestion for creating a .docx file based on the contents of a rich text box, that would be greatly appreciated. I found different resources that offered advice similar to this: http://nishantrana.wordpress.com/2007/11/03/creating-word-document-using-c/

But I kept having issues getting it to recognize what a WordApp was.

Here's the code from the first link with the schema issues.

private void GenerateDocument_Click(object sender, EventArgs e)
{
    string _nameSpaceURI = "http://schemas.microsoft.com/office/word/2005/10/wordml";
    string docFileName = GetSavePath();

    //-- Step 1 - Creating the document xml
    XmlDocument doc = new XmlDocument();
    XmlElement _wWordDoc = doc.CreateElement("w:wordDocument", _nameSpaceURI);
    doc.AppendChild (_wWordDoc);
    XmlElement _wbody = doc.CreateElement("w:body",_nameSpaceURI);
    _wWordDoc.AppendChild(_wbody);
    // Check if the string contains a line feed
    string[] _SplitStr = mleTextForDocument.Text.Split('\n');
    // if it contains line feed then each entry with a line feed goes to a new paragraph.
    for (int row = 0; row < _SplitStr.Length; row++)
    {
         XmlElement _wp1 = doc.CreateElement("w:p",_nameSpaceURI);
         _wbody.AppendChild(_wp1);
         XmlElement _wr1 = doc.CreateElement("w:r", _nameSpaceURI);
         _wp1.AppendChild(_wr1);
         XmlElement _wt11 = doc.CreateElement("w:t", _nameSpaceURI);
         _wr1.AppendChild(_wt11);
         XmlNode _wt1 = doc.CreateNode(XmlNodeType.Text, "w:t",_nameSpaceURI);
         _wt1.Value = _SplitStr[row];
         _wt11.AppendChild(_wt1);
    }

    //-- Step 2 - Creating the Package
    Package package = null;
    package = Package.Open(docFileName, FileMode.Create, FileAccess.ReadWrite);

    //-- Step 3 - Create the main document part (document.xml)
    Uri uri = new Uri("/word/document.xml", UriKind.Relative);
    PackagePart part = package.CreatePart(uri, "application/vnd.ms-word.main+xml");
    StreamWriter partWrt = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.Write));
    doc.Save(partWrt);
    partWrt.Close();
    package.Flush();

    //-- Step 4 - Create the relationship file
    uri = new Uri("/word/document.xml", UriKind.Relative);
    PackageRelationship rel = package.CreateRelationship(uri, TargetMode.Internal, "http://schemas.microsoft.com/office/2006/relationships/officeDocument", "rId1");
    package.Flush();

    //-- Step 5- Close the document.
    package.Close();
 }

I'm sorry for the lack of a clear question, but I really don't know what question to ask. I've never used schemas before, never used XML, and never had to add references to my projects before. Any advice or suggestions are appreciated.

+2  A: 

Dispite the ambigious question, and apparently it's coming from my bizzaro evil twin (nwonknu) (elgoog), joke right.

Anyhow, I've said it before and I'll say it again THE source of quality advise for XML/OpenXML is Eric White. He's a very active blogger, looks like 4+ years of consistant postings (sux when good sources just evaporate sometimes), anyhow breeze through his blog for a few minutes and I'm sure your grasp of OpenXML + Linq 2 XML will be a bit more solid.

RandomNickName42
Thanks for the link!