1. 제품
  2.   워드 프로세싱
  3.   .NET
  4.   Open XML SDK
 
  

워드 프로세싱 문서 생성을 위한 .NET 라이브러리

Word 파일 읽기, 쓰기, 조작 및 변환, 오픈 소스 .NET API를 통해 DOCX에서 테이블 생성.

Open XML SDK는 를 포함한 워드 프로세싱 문서를 조작하기 위해 Microsoft에서 개발하고 Microsoft 오픈 소스 행동 강령에 따라 배포되는 오픈 소스 API입니다. DOCXDOTXDOCMDOTM 파일 형식

API를 사용하여 텍스트, 머리글, 바닥글, 미주, 각주, 스타일, 테마 등을 추가할 수 있습니다. 고성능 워드 문서를 생성하고 데이터를 추출할 수 있습니다. API는 .NET 3.5, .NET 4.0, .NET 4.6 및 .NET Standard 1.3을 포함한 다양한 .NET 플랫폼을 지원합니다.

Previous Next

Open XML SDK 시작하기

먼저 .NET Framework 3.5 이상이 필요합니다. 그런 다음 GitHub에서 수동으로 저장소를 다운로드하거나 NuGet.

NuGet에서 Open XML SDK 설치

 Install-Package DocumentFormat.OpenXml

C#을 사용하여 DOCX 파일 조작

Open XML SDK를 사용하면 .NET 프로그래머가 자신의 .NET 응용 프로그램에서 워드 프로세싱을 만들고 수정할 수 있습니다. 기존 파일을 수정하기 위해 기존 파일을 열고 텍스트, 단락, 표 등과 같은 변경 사항을 추가할 수 있습니다.

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"));
}

C#을 사용하여 DOCX에서 테이블 만들기

API를 통해 개발자는 워드 프로세싱 문서에 표를 추가할 수 있습니다. 테이블을 추가하고, 테이블 속성을 설정하고, 테이블 그리드 및 기둥 그리드 속성을 설정할 수 있습니다. 또한 TableCell 및 TableRow 클래스를 각각 사용하여 테이블 셀과 행을 관리할 수 있습니다.

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);
}

워드 프로세싱 문서의 머리글 및 바닥글

문서의 적절한 구성은 매우 중요하며 모든 조직의 필요입니다. 머리글과 바닥글은 관련 날짜, 주제, 작성자 이름, 사진, 페이지 번호 등과 같은 몇 가지 추가 정보를 배치하여 워드 프로세싱 문서를 적절하게 구성하는 데 도움이 될 수 있는 문서의 핵심 부분입니다. 또한 여러 헤더 추가를 지원합니다.

Word 문서에서 머리글 관리

 
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);
}

Word 문서에서 바닥글 관리

 
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);
}
 한국인