Tworzenie sekcji powtarzanej tabeli odwzorowanej na niestandardową część Xml
tym samouczku pokazano, jak utworzyć tabelę z powtarzającą się sekcją odwzorowaną na niestandardową część Xml w dokumencie programu Word przy użyciu Aspose.Words dla .NET. Sekcja powtarzania umożliwia dynamiczne dodawanie wierszy na podstawie danych XML przechowywanych w niestandardowej części Xml.
Warunki wstępne
Aby skorzystać z tego samouczka, musisz mieć następujące elementy:
- Zainstalowana biblioteka Aspose.Words dla .NET.
- Podstawowa znajomość języka C# i przetwarzania tekstów w dokumentach Word.
Krok 1: Skonfiguruj katalog dokumentów
Rozpocznij od ustawienia ścieżki do katalogu dokumentów. Zastępować"YOUR DOCUMENT DIRECTORY"
z rzeczywistą ścieżką do katalogu, w którym chcesz zapisać dokument.
string dataDir = "YOUR DOCUMENT DIRECTORY";
Krok 2: Utwórz dokument i narzędzie do tworzenia dokumentów
Utwórz nową instancjęDocument
klasa i ADocumentBuilder
do zbudowania treści dokumentu.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Krok 3: Dodaj niestandardowe dane XML do CustomXmlPart
StwórzCustomXmlPart
i dodaj do niego niestandardowe dane XML. W tym przykładzie tworzymy ciąg XML reprezentujący zbiór książek wraz z ich tytułami i autorami.
CustomXmlPart xmlPart = doc.CustomXmlParts.Add("Books",
"<books><book><title>Everyday Italian</title><author>Giada De Laurentiis</author></book>" +
"<book><title>Harry Potter</title><author>J K. Rowling</author></book>" +
"<book><title>Learning XML</title><author>Erik T. Ray</author></book></books>");
Krok 4: Utwórz tabelę i strukturę tabeli
Rozpocznij tworzenie tabeli za pomocąStartTable
metodaDocumentBuilder
. Dodaj komórki tabeli i zawartość za pomocąInsertCell
IWrite
metody.
Table table = builder.StartTable();
builder.InsertCell();
builder.Write("Title");
builder.InsertCell();
builder.Write("Author");
builder.EndRow();
builder.EndTable();
Krok 5: Utwórz sekcję powtarzalną odwzorowaną na niestandardowy kod XML
StwórzStructuredDocumentTag
zSdtType.RepeatingSection
reprezentujący powtarzającą się sekcję. Ustaw mapowanie XML dla sekcji powtarzanej za pomocąSetMapping
metodaXmlMapping
nieruchomość. W tym przykładzie mapujemy sekcję powtarzaną na/books[1]/book
.
StructuredDocumentTag repeatingSectionSdt =
new StructuredDocumentTag(doc, SdtType.RepeatingSection, MarkupLevel.Row);
repeatingSectionSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book", "");
table.AppendChild(repeatingSectionSdt);
Krok 6: Utwórz element sekcji powtarzalnej i dodaj komórki
StwórzStructuredDocumentTag
zSdtType.RepeatingSectionItem
reprezentujący powtarzający się element sekcji. Dołącz go jako element podrzędny do sekcji powtarzanej.
StructuredDocumentTag repeatingSectionItemSdt =
new StructuredDocumentTag(doc, SdtType.RepeatingSectionItem, MarkupLevel.Row);
repeatingSectionSdt.AppendChild(repeatingSectionItemSdt);
StwórzRow
aby reprezentować każdy element sekcji powtarzanej i dołączać go do elementu sekcji powtarzanej.
Row row = new Row(doc);
repeatingSectionItemSdt.AppendChild(row);
Krok 7: Dodaj kontrolę treści w sekcji powtarzanej
TworzyćStructuredDocumentTag
obiekty zSdtType.PlainText
do reprezentowania kontroli treści tytułu i autora. Ustaw mapowanie XML dla każdej kontrolki zawartości za pomocąSetMapping
metodaXmlMapping
nieruchomość. W tym przykładzie mapujemy kontrolkę tytułu na/books[1]/book[1]/title[1]
i kontrola autora do/books[1]/book[1]/author[1]
.
StructuredDocumentTag titleSdt =
new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell);
titleSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/title[1]", "");
row.AppendChild(titleSdt);
StructuredDocumentTag authorSdt =
new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell);
authorSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/author[1]", "");
row.AppendChild(authorSdt);
Krok 8: Zapisz dokument
Zapisz zmodyfikowany dokument w określonym katalogu za pomocąSave
metoda. Podaj żądaną nazwę pliku z odpowiednim rozszerzeniem. W tym przykładzie zapisujemy dokument jako „WorkingWithSdt.CreatingTableRepeatingSectionMappedToCustomXmlPart.docx”.
doc.Save(dataDir + "WorkingWithSdt.CreatingTableRepeatingSectionMappedToCustomXmlPart.docx");
Przykładowy kod źródłowy do tworzenia sekcji powtarzania tabeli odwzorowanej na niestandardową część Xml przy użyciu Aspose.Words dla .NET
// Ścieżka do katalogu dokumentów
string dataDir = "YOUR DOCUMENT DIRECTORY";
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
CustomXmlPart xmlPart = doc.CustomXmlParts.Add("Books",
"<books><book><title>Everyday Italian</title><author>Giada De Laurentiis</author></book>" +
"<book><title>Harry Potter</title><author>J K. Rowling</author></book>" +
"<book><title>Learning XML</title><author>Erik T. Ray</author></book></books>");
Table table = builder.StartTable();
builder.InsertCell();
builder.Write("Title");
builder.InsertCell();
builder.Write("Author");
builder.EndRow();
builder.EndTable();
StructuredDocumentTag repeatingSectionSdt =
new StructuredDocumentTag(doc, SdtType.RepeatingSection, MarkupLevel.Row);
repeatingSectionSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book", "");
table.AppendChild(repeatingSectionSdt);
StructuredDocumentTag repeatingSectionItemSdt =
new StructuredDocumentTag(doc, SdtType.RepeatingSectionItem, MarkupLevel.Row);
repeatingSectionSdt.AppendChild(repeatingSectionItemSdt);
Row row = new Row(doc);
repeatingSectionItemSdt.AppendChild(row);
StructuredDocumentTag titleSdt =
new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell);
titleSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/title[1]", "");
row.AppendChild(titleSdt);
StructuredDocumentTag authorSdt =
new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell);
authorSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/author[1]", "");
row.AppendChild(authorSdt);
doc.Save(dataDir + "WorkingWithSdt.CreatingTableRepeatingSectionMappedToCustomXmlPart.docx");
Otóż to! Pomyślnie utworzyłeś tabelę z powtarzającą się sekcją odwzorowaną na CustomXmlPart w dokumencie programu Word przy użyciu Aspose.Words dla .NET.