1. Produkti
  2.   Tekstapstrāde
  3.   .NET
  4.   Open XML SDK
 
  

.NET bibliotēka tekstapstrādes dokumentu izveidei

Lasiet, rakstiet, manipulējiet un konvertējiet Word failus, izveidojiet tabulas DOCX, izmantojot atvērtā pirmkoda .NET API.

Open XML SDK ir atvērtā pirmkoda API, ko izstrādājusi Microsoft un izplata saskaņā ar Microsoft atklātā pirmkoda rīcības kodeksu, lai manipulētu ar tekstapstrādes dokumentiem, tostarp DOCXDOTXDOCMDOTM failu formāti

Izmantojot API, varat pievienot tekstu, galveni, kājeni, beigu piezīmes, zemsvītras piezīmes, stilus, motīvus un daudz ko citu. Tas ļauj ģenerēt augstas veiktspējas Word dokumentus un iegūt no tiem datus. API atbalsta dažādas .NET platformas, tostarp .NET 3.5, .NET 4.0, .NET 4.6 un .NET Standard 1.3.

Previous Next

Darba sākšana ar Open XML SDK

Pirmkārt, jums ir nepieciešama .NET Framework 3.5 vai jaunāka versija. Pēc tam, lūdzu, manuāli lejupielādējiet repozitoriju no GitHub vai instalējiet to no NuGet.

Instalēšana Atveriet XML SDK no NuGet

 Install-Package DocumentFormat.OpenXml

Manipulēt DOCX failu, izmantojot C#

Open XML SDK ļauj .NET programmētājiem izveidot, kā arī modificēt tekstapstrādi no savām .NET lietojumprogrammām. Lai modificētu esošu failu, varat atvērt esošu failu un pievienot izmaiņas, piemēram, tekstu, rindkopas, tabulas un citas.

Pievienot rindkopu DOCX — C#

// Open an existing word processing document
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open("fileformat.docx", true))
{
  Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
  // Add paragraph
  Paragraph para = body.AppendChild(new Paragraph());
  Run run = para.AppendChild(new Run());
  run.AppendChild(new Text("File Format Developer Guide"));
}

Izveidojiet tabulu programmā DOCX, izmantojot C#

API ļauj izstrādātājiem tekstapstrādes dokumentos pievienot tabulu. Varat pievienot tabulu, iestatīt tabulas rekvizītus, iestatīt tabulas režģi un kolonnu režģa rekvizītus. Turklāt jūs varat pārvaldīt tabulas šūnas un rindas, izmantojot attiecīgi TableCell un TableRow klases.

Izveidot tabulu programmā DOCX — C#

// Open an existing word processing document
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open("fileformat.docx", true))
{
  Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
  // Create a table.
  Table table = new Table();
  // Set the style and width for the table.
  TableProperties tableProperties = new TableProperties();
  TableStyle tableStyle = new TableStyle() { Val = "TableGrid" };
  // Make the table width 100% of the page width.
  TableWidth tableWidth = new TableWidth() { Width = "5000", Type = TableWidthUnitValues.Pct };
  // Apply
  tableProperties.Append(tableStyle, tableWidth);
  table.AppendChild(tableProperties);
  // Add columns
  TableGrid tableGrid = new TableGrid(new GridColumn(), new GridColumn(), new GridColumn());
  table.AppendChild(tableGrid);
  // Create 1 row to the table.
  TableRow tableRow = new TableRow();
  // Add a cell to each column in the row.
  TableCell tableCell = new TableCell(new Paragraph(new Run(new Text("Column 1"))));
  TableCell tableCell1 = new TableCell(new Paragraph(new Run(new Text("Column 2"))));
  //Append data
  tableRow.Append(tableCell, tableCell1);
  // Add row to the table.
  table.AppendChild(tableRow);
  // Add the table to the document
  body.AppendChild(table);
}

Galvenes un kājenes tekstapstrādes dokumentos

Pareiza dokumentu sakārtošana ir ļoti svarīga un katras organizācijas nepieciešamība. Galvenes un kājenes ir galvenās dokumentu daļas, kas var palīdzēt pareizi sakārtot tekstapstrādes dokumentus, ievietojot papildu informāciju, piemēram, attiecīgos datumus, tēmas, autora vārdu, attēlus, lappušu numurus utt. Tā atbalsta arī vairāku galveņu pievienošanu.

Pārvaldiet galvenes Word dokumentā

 
public static void ApplyHeader(WordprocessingDocument doc)
{
  // Get the main document part.
  MainDocumentPart mainDocPart = doc.MainDocumentPart;
  HeaderPart headerPart1 = mainDocPart.AddNewPart("r97");
  Header header1 = new Header();
  Paragraph paragraph1 = new Paragraph(){ };
  Run run1 = new Run();
  Text text1 = new Text();
  text1.Text = "Header stuff";
  run1.Append(text1);
  paragraph1.Append(run1);
  header1.Append(paragraph1);
  headerPart1.Header = header1;
  SectionProperties sectionProperties1 = mainDocPart.Document.Body.Descendants().FirstOrDefault();
  if (sectionProperties1 == null)
  {
  sectionProperties1 = new SectionProperties() { };
  mainDocPart.Document.Body.Append(sectionProperties1);
  }
  HeaderReference headerReference1 = new HeaderReference() { Type = HeaderFooterValues.Default, Id = "r97" };
  sectionProperties1.InsertAt(headerReference1,0);
}

Pārvaldiet kājenes Word dokumentā

 
public static void ApplyFooter(WordprocessingDocument doc)
{
  // Get the main document part.
  MainDocumentPart mainDocPart = doc.MainDocumentPart;
  FooterPart footerPart1 = mainDocPart.AddNewPart("r98");
  Footer footer1 = new Footer();
  Paragraph paragraph1 = new Paragraph() { };
  Run run1 = new Run();
  Text text1 = new Text();
  text1.Text = "Footer stuff";
  run1.Append(text1);
  paragraph1.Append(run1);
  footer1.Append(paragraph1);
  footerPart1.Footer = footer1;
  SectionProperties sectionProperties1 = mainDocPart.Document.Body.Descendants().FirstOrDefault();
  if (sectionProperties1 == null)
  {
    sectionProperties1 = new SectionProperties() { };
    mainDocPart.Document.Body.Append(sectionProperties1);
  }
  FooterReference footerReference1 = new FooterReference() { Type = DocumentFormat.OpenXml.Wordprocessing.HeaderFooterValues.Default, Id = "r98" };
  sectionProperties1.InsertAt(footerReference1, 0);
}
 Latviski