C#에서 Office Interop 없이 Word 문서를 만드는 방법
Microsoft Word 문서는 공식 비즈니스 보고서에서 개인 편지에 이르기까지 다양한 용도로 널리 사용됩니다. C#에서는 개발자가 프로그램적으로 Microsoft Word 문서를 생성해야 하는 경우가 많습니다. Windows 응용 프로그램 개발자는 전통적으로 Microsoft Office Interop을 사용하여 C#으로 Word 문서를 생성하고 만듭니다.
그러나 이 접근 방식은 모두에게 접근 가능한 것은 아니며, Microsoft Office 인터페이스가 제공되지 않는 OS나 Linux 기기에서 개발자를 찾을 수 있습니다. 이런 경우 개발자는 독립적으로 다양한 플랫폼에서 작동할 수 있는 다른 라이브러리를 탐색해야 합니다. Word 파일을 프로그램적으로 작업하기 위한 강력한 라이브러리 중 하나는 IronWord로, Iron Software에서 제공됩니다.
IronWord는 .NET 애플리케이션에서 Word 문서를 작업하기 위한 강력한 기능을 제공하며, 다른 플랫폼 및 Linux 기반 Docker 이미지/컨테이너에서도 실행할 수 있습니다. 직관적인 C#, VB.NET Word 및 Docx 문서 API가 있는 IronWord를 사용하면 Microsoft Office, Office 자동화, 또는 Word Interop을 설치할 필요 없이 Word 문서 파일을 작성, 편집 및 내보낼 수 있습니다. IronWord는 .NET 8, 7, 6, Framework, Core 및 Azure와 완전 호환됩니다.
이 기사에서는 IronWord 라이브러리를 사용하여 C#에서 Word 문서를 생성하는 방법을 탐구할 것입니다.
How to Create Word Documents Without Office Interop in C
- 새로운 C# 프로젝트를 만드세요.
- IronWord 라이브러리를 설치합니다.
- IronWord 라이브러리를 사용하여 Word 문서를 생성합니다.
- 현재 있는 문서에 콘텐츠를 추가합니다.
- 생성된 Word 문서를 저장합니다.
- 생성된 Word 문서를 열고 표시합니다.
사전 요구사항:
- Visual Studio: Visual Studio 또는 다른 C# 개발 환경이 설치되어 있는지 확인합니다.
- NuGet 패키지 관리자: 프로젝트에서 패키지를 관리할 수 있도록 NuGet을 사용할 수 있는지 확인합니다.
1단계: 새로운 C# 프로젝트 생성
새로운 C# 콘솔 애플리케이션을 생성하거나 Word 문서를 생성하고자 하는 기존 프로젝트를 사용합니다.
콘솔 애플리케이션 템플릿을 선택하고 다음을 클릭하세요.

다음 단계에서는 솔루션 및 프로젝트 이름을 제공할 수 있습니다.

.NET 버전을 선택하고 '생성'을 클릭하세요.

2단계: IronWord 라이브러리 설치
C# 프로젝트를 열고 NuGet 패키지 관리자 콘솔을 사용하여 IronWord 라이브러리를 설치합니다.
Install-Package IronWord -Version 2024.1.2
아래와 같이 Visual Studio 패키지 관리자를 통해 NuGet 패키지를 설치할 수도 있습니다.

3단계: IronWord 라이브러리를 사용하여 Word 문서를 생성 및 저장
간단한 예제를 통해 IronWord 라이브러리를 사용하여 Word 문서를 생성하는 것으로 시작하겠습니다. 다음 코드는 텍스트 'Hello, World!'가 포함된 단일 문단의 기본 Word 문서를 생성하는 방법을 보여줍니다.
using IronWord;
using IronWord.Models;
// Create a text run instance with "Hello, World!" content
TextRun textRun = new TextRun("Hello, World!");
// Create a paragraph and add the text run to it
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Create a new Word document object with the paragraph
WordDocument doc = new WordDocument(paragraph);
// Save the document as a .docx file
doc.SaveAs("example.docx"); // Saves the file to disk with the name example.docx
using IronWord;
using IronWord.Models;
// Create a text run instance with "Hello, World!" content
TextRun textRun = new TextRun("Hello, World!");
// Create a paragraph and add the text run to it
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Create a new Word document object with the paragraph
WordDocument doc = new WordDocument(paragraph);
// Save the document as a .docx file
doc.SaveAs("example.docx"); // Saves the file to disk with the name example.docx
Imports IronWord
Imports IronWord.Models
' Create a text run instance with "Hello, World!" content
Private textRun As New TextRun("Hello, World!")
' Create a paragraph and add the text run to it
Private paragraph As New Paragraph()
paragraph.AddTextRun(textRun)
' Create a new Word document object with the paragraph
Dim doc As New WordDocument(paragraph)
' Save the document as a .docx file
doc.SaveAs("example.docx") ' Saves the file to disk with the name example.docx
위의 코드 예제를 실행한 후, 새로운 문서 파일 example.docx이 생성되며 출력은 아래와 같습니다.

IronWord를 사용하여 Word 문서 파일을 생성하는 기본 예제입니다. 더 많은 정보를 원하시면 문서를 읽을 수 있습니다.
스타일이 적용된 문단을 Word 문서에 추가하기
이제 IronWord를 사용하여 간단한 Word 문서를 생성하는 방법을 알았으니 문단과 스타일이 적용된 텍스트를 추가해 봅시다.
TextRun은 스타일 데이터를 받을 수 있어 텍스트의 시각적 표현을 향상시킬 수 있습니다. 텍스트는 취소선, 굵게, 기울임꼴, 밑줄과 같은 스타일로 설정할 수 있습니다.
이전에 작성한 프로그램에 아래 코드를 추가 및 수정하십시오.
using IronWord;
using IronWord.Models;
// Create text runs with different styles
TextRun textRun = new TextRun("Hello, World!"); // Simple string
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Configure and add italic text
TextRun introText = new TextRun("This is an example paragraph with italic and bold styling.");
TextStyle italicStyle = new TextStyle()
{
IsItalic = true
};
TextRun italicText = new TextRun("Italic example sentence.", italicStyle);
// Configure and add bold text
TextStyle boldStyle = new TextStyle()
{
IsBold = true
};
TextRun boldText = new TextRun("Bold example sentence.", boldStyle);
// Add text runs to the paragraph
paragraph.AddTextRun(introText);
paragraph.AddTextRun(italicText);
paragraph.AddTextRun(boldText);
// Create and save the new Word document
WordDocument doc = new WordDocument(paragraph);
doc.SaveAs("example.docx"); // Saves the file to disk with the name example.docx
using IronWord;
using IronWord.Models;
// Create text runs with different styles
TextRun textRun = new TextRun("Hello, World!"); // Simple string
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Configure and add italic text
TextRun introText = new TextRun("This is an example paragraph with italic and bold styling.");
TextStyle italicStyle = new TextStyle()
{
IsItalic = true
};
TextRun italicText = new TextRun("Italic example sentence.", italicStyle);
// Configure and add bold text
TextStyle boldStyle = new TextStyle()
{
IsBold = true
};
TextRun boldText = new TextRun("Bold example sentence.", boldStyle);
// Add text runs to the paragraph
paragraph.AddTextRun(introText);
paragraph.AddTextRun(italicText);
paragraph.AddTextRun(boldText);
// Create and save the new Word document
WordDocument doc = new WordDocument(paragraph);
doc.SaveAs("example.docx"); // Saves the file to disk with the name example.docx
Imports IronWord
Imports IronWord.Models
' Create text runs with different styles
Private textRun As New TextRun("Hello, World!") ' Simple string
Private paragraph As New Paragraph()
paragraph.AddTextRun(textRun)
' Configure and add italic text
Dim introText As New TextRun("This is an example paragraph with italic and bold styling.")
Dim italicStyle As New TextStyle() With {.IsItalic = True}
Dim italicText As New TextRun("Italic example sentence.", italicStyle)
' Configure and add bold text
Dim boldStyle As New TextStyle() With {.IsBold = True}
Dim boldText As New TextRun("Bold example sentence.", boldStyle)
' Add text runs to the paragraph
paragraph.AddTextRun(introText)
paragraph.AddTextRun(italicText)
paragraph.AddTextRun(boldText)
' Create and save the new Word document
Dim doc As New WordDocument(paragraph)
doc.SaveAs("example.docx") ' Saves the file to disk with the name example.docx
Word 문서에 테이블 추가하기
데이터를 명확하게 표현하기 위해, 데이터는 그리드 형태로도 나타낼 수 있습니다. 데이터가 그리드 형태로 배열될 때, 이를 테이블이라고 합니다. IronWord를 사용하여, 아래와 같이 Word 문서에 테이블과 이미지를 추가할 수 있습니다:
using IronWord;
using IronWord.Models;
// Create a table cell with sample text
TableCell cell = new TableCell();
TextRun textRun = new TextRun("Sample text");
// Add text run to the cell as a paragraph
cell.AddContent(new Paragraph(textRun));
// Configure cell border style
BorderStyle borderStyle = new BorderStyle
{
BorderColor = new IronColor(IronSoftware.Drawing.Color.Black),
BorderValue = IronWord.Models.Enums.BorderValues.Thick,
BorderSize = 5
};
// Set table borders
TableBorders tableBorders = new TableBorders
{
TopBorder = borderStyle,
RightBorder = borderStyle,
BottomBorder = borderStyle,
LeftBorder = borderStyle
};
cell.Borders = tableBorders;
// Create a table row and add cells to it
TableRow row = new TableRow();
row.AddCell(cell); // Add the first cell
row.AddCell(cell); // Add the second cell, duplicating to mimic a row with two identical cells
// Create a table and add the row to the table
Table table = new Table();
table.AddRow(row);
// Create and save a Word document using the table
WordDocument doc = new WordDocument(table);
doc.SaveAs("Document.docx"); // Saves the file to disk with the name Document.docx
using IronWord;
using IronWord.Models;
// Create a table cell with sample text
TableCell cell = new TableCell();
TextRun textRun = new TextRun("Sample text");
// Add text run to the cell as a paragraph
cell.AddContent(new Paragraph(textRun));
// Configure cell border style
BorderStyle borderStyle = new BorderStyle
{
BorderColor = new IronColor(IronSoftware.Drawing.Color.Black),
BorderValue = IronWord.Models.Enums.BorderValues.Thick,
BorderSize = 5
};
// Set table borders
TableBorders tableBorders = new TableBorders
{
TopBorder = borderStyle,
RightBorder = borderStyle,
BottomBorder = borderStyle,
LeftBorder = borderStyle
};
cell.Borders = tableBorders;
// Create a table row and add cells to it
TableRow row = new TableRow();
row.AddCell(cell); // Add the first cell
row.AddCell(cell); // Add the second cell, duplicating to mimic a row with two identical cells
// Create a table and add the row to the table
Table table = new Table();
table.AddRow(row);
// Create and save a Word document using the table
WordDocument doc = new WordDocument(table);
doc.SaveAs("Document.docx"); // Saves the file to disk with the name Document.docx
Imports IronWord
Imports IronWord.Models
' Create a table cell with sample text
Private cell As New TableCell()
Private textRun As New TextRun("Sample text")
' Add text run to the cell as a paragraph
cell.AddContent(New Paragraph(textRun))
' Configure cell border style
Dim borderStyle As New BorderStyle With {
.BorderColor = New IronColor(IronSoftware.Drawing.Color.Black),
.BorderValue = IronWord.Models.Enums.BorderValues.Thick,
.BorderSize = 5
}
' Set table borders
Dim tableBorders As New TableBorders With {
.TopBorder = borderStyle,
.RightBorder = borderStyle,
.BottomBorder = borderStyle,
.LeftBorder = borderStyle
}
cell.Borders = tableBorders
' Create a table row and add cells to it
Dim row As New TableRow()
row.AddCell(cell) ' Add the first cell
row.AddCell(cell) ' Add the second cell, duplicating to mimic a row with two identical cells
' Create a table and add the row to the table
Dim table As New Table()
table.AddRow(row)
' Create and save a Word document using the table
Dim doc As New WordDocument(table)
doc.SaveAs("Document.docx") ' Saves the file to disk with the name Document.docx
위 예제에서는 테두리를 사용하여 Word 문서에 테이블을 추가했습니다.
Word 문서에 이미지 추가하기
이미지는 문서의 프레젠테이션을 향상시키며, 더 많은 색상과 시각적 매력을 더할 수 있습니다.
이미지는 아래의 코드에서 보여주듯이 IronWord를 사용하여 Word 문서에 프로그래밍 방식으로 추가할 수 있습니다:
using IronWord;
using IronWord.Models;
// Load a new document
WordDocument doc = new WordDocument();
// Configure and add image to the document
IronWord.Models.Image image = new IronWord.Models.Image("SalesChart.jpg")
{
Width = 200, // Set image width in pixels
Height = 200 // Set image height in pixels
};
Paragraph paragraph = new Paragraph();
paragraph.AddImage(image); // Add image to paragraph
doc.AddParagraph(paragraph); // Add paragraph to the document
// Save the document as a .docx file
doc.SaveAs("save_document.docx"); // Saves the file to disk with the name save_document.docx
using IronWord;
using IronWord.Models;
// Load a new document
WordDocument doc = new WordDocument();
// Configure and add image to the document
IronWord.Models.Image image = new IronWord.Models.Image("SalesChart.jpg")
{
Width = 200, // Set image width in pixels
Height = 200 // Set image height in pixels
};
Paragraph paragraph = new Paragraph();
paragraph.AddImage(image); // Add image to paragraph
doc.AddParagraph(paragraph); // Add paragraph to the document
// Save the document as a .docx file
doc.SaveAs("save_document.docx"); // Saves the file to disk with the name save_document.docx
Imports IronWord
Imports IronWord.Models
' Load a new document
Private doc As New WordDocument()
' Configure and add image to the document
Private image As New IronWord.Models.Image("SalesChart.jpg") With {
.Width = 200,
.Height = 200
}
Private paragraph As New Paragraph()
paragraph.AddImage(image) ' Add image to paragraph
doc.AddParagraph(paragraph) ' Add paragraph to the document
' Save the document as a .docx file
doc.SaveAs("save_document.docx") ' Saves the file to disk with the name save_document.docx
여기서는 IronWord.Models.Image을 사용하여 높이와 너비가 200 픽셀인 이미지를 단락에 추가하고 있습니다.
라이센스 (무료 체험 가능)
IronWord를 사용하려면 라이선스가 필요합니다. Iron Software 웹사이트에서 체험판 키를 받으세요. 이 키는 appsettings.json에 위치해야 합니다.
{
"IronWord.LicenseKey": "IRONWORD.MYLICENSE.KEY.TRIAL"
}
체험판 라이센스를 받으려면 이메일을 제공하세요. 이메일 ID를 제출하면 키가 이메일을 통해 발송됩니다.

결론
C#에서 IronWord 라이브러리를 사용하여 Word 문서를 생성하면 Microsoft Office에 의존하지 않고도 유연하고 효율적으로 문서를 생성할 수 있습니다. 간단한 편지부터 테이블이나 이미지를 포함한 복잡한 보고서까지, IronWord는 프로그래밍 방식으로 이를 가능하게 합니다. 이 기사에서는 Word 문서를 만드는 종합적인 튜토리얼을 제공합니다. IronWord를 사용하면 Word 문서 생성을 자동화할 수 있어 C# 응용 프로그램이 더욱 다양하고 생산적이 됩니다.
그리고 생성된 Word 문서와 함께 작동할 PDF 파일 조작에 관심 있는 개발자는 Iron Software에서 제작한 또 다른 C# 라이브러리인 IronPDF를 참고하면 됩니다.
자주 묻는 질문
Microsoft Office Interop을 사용하지 않고 C#에서 Word 문서를 만드는 방법은 무엇인가요?
IronWord 라이브러리를 활용하면 Microsoft Office Interop을 사용하지 않고도 C#에서 Word 문서를 생성할 수 있습니다. 이 라이브러리를 사용하면 Microsoft Office를 설치하지 않고도 Word 문서를 프로그래밍 방식으로 생성, 편집 및 저장할 수 있습니다.
IronWord Microsoft Office Interop보다 사용하는 장점은 무엇입니까?
IronWord Microsoft Office Interop을 사용할 수 없는 Linux와 같은 비 Windows 플랫폼에서도 실행할 수 있는 유연성을 제공합니다. 또한 Microsoft Office 설치에 대한 의존성을 없애 개발 프로세스를 간소화합니다.
IronWord 사용하여 Word 문서를 만드는 방법은 무엇인가요?
IronWord 사용하여 Word 문서를 생성하려면 먼저 C# 프로젝트의 NuGet 패키지 관리자를 통해 IronWord 라이브러리를 설치하세요. 그런 다음 IronWord API를 사용하여 Word 문서를 프로그래밍 방식으로 생성하고 조작할 수 있습니다.
최신 .NET 버전에서 IronWord 사용할 수 있나요?
네, IronWord 는 .NET 8, 7, 6, Framework, Core 및 Azure를 포함한 다양한 .NET 버전과 호환되므로 최신 C# 애플리케이션에 통합할 수 있습니다.
IronWord 사용하여 Word 문서에 스타일이 적용된 텍스트를 추가하려면 어떻게 해야 하나요?
IronWord TextRun 및 TextStyle 클래스를 사용하여 스타일이 적용된 텍스트를 추가할 수 있습니다. 이러한 클래스를 사용하면 Word 문서 내의 특정 텍스트 부분에 굵게, 기울임꼴, 밑줄과 같은 스타일을 적용할 수 있습니다.
IronWord 사용하여 Word 문서에 표를 포함할 수 있습니까?
네, IronWord 사용하면 Word 문서에 표를 포함할 수 있습니다. 이 라이브러리를 사용하면 행과 셀로 구성된 표를 정의할 수 있으며, 테두리 및 기타 스타일을 사용하여 표의 모양을 사용자 지정할 수 있습니다.
IronWord 사용하여 Word 문서에 이미지를 삽입하는 방법은 무엇입니까?
IronWord 사용하여 Word 문서에 이미지를 삽입하려면 IronWord.Models.Image 클래스를 사용하여 지정된 크기의 이미지를 단락에 삽입함으로써 문서의 시각적 콘텐츠를 향상시킬 수 있습니다.
IronWord 평가판을 사용할 수 있나요?
네, Iron Software IronWord 의 무료 평가판을 제공하며, 해당 웹사이트에서 다운로드할 수 있습니다. 이 평가판을 통해 구매하기 전에 라이브러리의 기능과 성능을 평가해 볼 수 있습니다.
Iron Software 어떤 다른 문서 조작 라이브러리를 제공합니까?
Iron Software IronWord 외에도 PDF 파일을 조작하기 위한 라이브러리인 IronPDF 제공합니다. 이 두 라이브러리를 함께 사용하면 C# 애플리케이션에서 Word 및 PDF 문서를 모두 처리하는 포괄적인 솔루션을 제공할 수 있습니다.



