1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106:
| [WebMethod] public string createDoc() { xmlstring.Replace('\n', ' '); XmlDocument xmldoc = new XmlDocument(); xmldoc.Load("C:\\Inetpub\\wwwroot\\wss\\VirtualDirectories\\4700\\xmldata\\bestellung_temp.xml"); XmlNamespaceManager nsManager = new XmlNamespaceManager(xmldoc.NameTable); nsManager.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2005-10-21T21:12:27"); object missing = System.Reflection.Missing.Value; Word.ApplicationClass oWordApp = new Word.ApplicationClass(); Word.Document oWordDoc = new Word.Document(); try { oWordApp = new Word.ApplicationClass(); object oTemplate = "C:\\Inetpub\\wwwroot\\wss\\VirtualDirectories\\4700\\xmldata\\vorlage_bestellung.dotx"; oWordDoc = oWordApp.Documents.Add(ref oTemplate, ref missing, ref missing, ref missing); } catch (Exception exc) { sw = new StreamWriter(new FileStream("C:\\Inetpub\\wwwroot\\wss\\VirtualDirectories\\4700\\xmldata\\bestellung_temp1.xml", FileMode.Append)); sw.Write(exc.ToString()); sw.Close(); } oWordDoc.Activate(); object oBookMark = "Firma"; string xpathquery = "/my:expenseReport/my:items/my:item/my:distributor"; XmlNode lieferantenNode = xmldoc.SelectSingleNode(xpathquery, nsManager); oWordDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = lieferantenNode.InnerText + "\n"; oBookMark = "BestNr"; xpathquery = "/my:expenseReport/my:bestellnummer"; XmlNode bestNode = xmldoc.SelectSingleNode(xpathquery, nsManager); oWordDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = bestNode.InnerText; int bestellnummer = 0; try { bestellnummer = Convert.ToInt32(bestNode.InnerText); } catch (Exception) { return "Fehler"; }
oBookMark = "BestTab"; Word.Table oTable; Word.Range wrdRng = oWordDoc.Bookmarks.get_Item(ref oBookMark).Range;
XmlNodeList items = xmldoc.GetElementsByTagName("my:item");
oTable = oWordDoc.Tables.Add(wrdRng, items.Count + 2, 5, ref missing, ref missing); oTable.Range.ParagraphFormat.SpaceAfter = 6;
oTable.Cell(1, 1).Range.Text = "Pos."; oTable.Columns[1].Width = 30; oTable.Cell(1, 2).Range.Text = "Menge"; oTable.Columns[2].Width = 40; oTable.Cell(1, 3).Range.Text = "Artikelnummer/Bezeichnung"; oTable.Columns[3].Width = 250; oTable.Cell(1, 4).Range.Text = "Einzelpreis"; oTable.Columns[4].Width = 80; oTable.Cell(1, 5).Range.Text = "Gesamtpreis"; oTable.Columns[5].Width = 80;
int activeItem = 1; foreach (XmlNode item in items) { oTable.Cell(++activeItem, 1).Range.Text = item.SelectSingleNode("my:position", nsManager).InnerText; oTable.Cell(activeItem, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight; oTable.Cell(activeItem, 2).Range.Text = item.SelectSingleNode("my:stueck", nsManager).InnerText; oTable.Cell(activeItem, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight; oTable.Cell(activeItem, 3).Range.Text = item.SelectSingleNode("my:description", nsManager).InnerText; oTable.Cell(activeItem, 4).Range.Text = item.SelectSingleNode("my:ep", nsManager).InnerText + " €"; oTable.Cell(activeItem, 4).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight; oTable.Cell(activeItem, 5).Range.Text = item.SelectSingleNode("my:amount", nsManager).InnerText + " €"; ; oTable.Cell(activeItem, 5).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight; }
oTable.Cell(oTable.Rows.Count, 1).Merge(oTable.Cell(oTable.Rows.Count, 4)); oTable.Cell(oTable.Rows.Count, 1).Range.Text = "Summe:"; oTable.Cell(oTable.Rows.Count, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight; xpathquery = "/my:expenseReport/my:items/my:total"; XmlNode totalNode = xmldoc.SelectSingleNode(xpathquery, nsManager); oTable.Cell(oTable.Rows.Count, 2).Range.Text = totalNode.InnerText + " €"; oTable.Cell(oTable.Rows.Count, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
oTable.Rows[1].Range.Font.Bold = 1; oTable.Rows[oTable.Rows.Count].Range.Font.Bold = 1;
oTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; oTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; object filename = "C:\\Inetpub\\wwwroot\\wss\\VirtualDirectories\\4700\\xmldata\\bestellung_" + bestellnummer + ".docx"; oWordDoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); object savechanges = false; oWordDoc.Close(ref savechanges, ref missing, ref missing); oWordApp.Quit(ref savechanges, ref missing, ref missing); return "alles ok"; } |