카테고리 없음

[씨#] Word 문서 템플릿 C #에서 테이블 채우기

행복을전해요 2021. 3. 1. 11:10

그것이 나라면 이것이 내가 사용할 것입니다.

DOCX

-------------------

가장 좋은 옵션 (최소한 docx 형식의 경우)은 http://docx.codeplex.com/입니다.

아래 블로그 게시물에서 매우 간단한 문서 조작을 DocX, Microsoft의 OOXML API 및 클래식 Office Interop 라이브러리와 비교하는 코드 샘플을 찾을 수 있습니다. http://cathalscorner.blogspot.com/2010/06/cathal-why-did-you -create-docx.html

-------------------

상용 제품에 관심이 있고 DOCX 파일 형식으로 작업하는 경우 GemBox.Document 구성 요소를 사용해 볼 수 있습니다 .

독자적인 읽기 / 쓰기 엔진과 간단한 콘텐츠 모델을 가지고 있으며 MS Word를 설치하지 않고도 사용할 수 있습니다.

다음은 편지 병합 기능을 사용하여 데이터로 확장 될 테이블이있는 간단한 템플릿 문서를 만드는 방법의 샘플 C # 코드입니다.

// Use the component in free mode.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");

// Define DataTable with two columns: 'Name' and 'Surname', and fill it with some data.
// You don't have to do this if you already have a DataTable instance.
var dataTable = new DataTable("People")
{
    Columns =
        {
                new DataColumn("Name", typeof(string)),
                        new DataColumn("Surname", typeof(string))
                            },
                                Rows =
                                    {
                                            new object[] { "John", "Doe" },
                                                    new object[] { "Fred", "Nurk" },
                                                            new object[] { "Hans", "Meier" },
                                                                    new object[] { "Ivan", "Horvat" }
                                                                        }
                                                                        };
                                                                        
                                                                        // Create and save a template document. 
                                                                        // You don't have to do this if you already have a template document.
                                                                        // This code is only provided as a reference how template document should look like.
                                                                        var document = new DocumentModel();
                                                                        document.Sections.Add(
                                                                            new Section(document,
                                                                                    new Table(document,
                                                                                                new TableRow(document,
                                                                                                                new TableCell(document,
                                                                                                                                    new Paragraph(document, "Name")),
                                                                                                                                                    new TableCell(document,
                                                                                                                                                                        new Paragraph(document, "Surname"))),
                                                                                                                                                                                    new TableRow(document,
                                                                                                                                                                                                    new TableCell(document,
                                                                                                                                                                                                                        new Paragraph(document,
                                                                                                                                                                                                                                                new Field(document, FieldType.MergeField, "RangeStart:People"),
                                                                                                                                                                                                                                                                        new Field(document, FieldType.MergeField, "Name"))),
                                                                                                                                                                                                                                                                                        new TableCell(document,
                                                                                                                                                                                                                                                                                                            new Paragraph(document,
                                                                                                                                                                                                                                                                                                                                    new Field(document, FieldType.MergeField, "Surname"),
                                                                                                                                                                                                                                                                                                                                                            new Field(document, FieldType.MergeField, "RangeEnd:People")))))));
                                                                                                                                                                                                                                                                                                                                                            document.Save("TemplateDocument.docx", SaveOptions.DocxDefault);
                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                            // Load a template document.
                                                                                                                                                                                                                                                                                                                                                            document = DocumentModel.Load("TemplateDocument.docx", LoadOptions.DocxDefault);
                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                            // Mail merge template document with DataTable.
                                                                                                                                                                                                                                                                                                                                                            // Important: DataTable.TableName and RangeStart/RangeEnd merge field names must match.
                                                                                                                                                                                                                                                                                                                                                            document.MailMerge.ExecuteRange(dataTable);
                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                            // Save the mail merged document.
                                                                                                                                                                                                                                                                                                                                                            document.Save("Document.docx", SaveOptions.DocxDefault);
                                                                                                                                                                                                                                                                                                                                                            


출처
https://stackoverflow.com/questions/22089812