- NPOI
- Spire.Doc
- Download Sample Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using NPOI.XWPF.UserModel; using System.IO; namespace NPOI { class Program { static void Main(string[] args) { //Create document XWPFDocument doc = new XWPFDocument(); //Create table int row = 3; int col = 2; XWPFTable table = doc.CreateTable(row, col); //Set table width table.Width = 4000; //Loop through the table for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { XWPFTableCell cell = table.GetRow(i).GetCell(j); //Set style of the table XWPFParagraph para=cell.AddParagraph(); XWPFRun run=para.CreateRun(); run.SetBold(true); run.FontFamily = "NSimSun"; run.SetText("Row" + i + " " + "Col" + j); table.GetRow(i).SetHeight(800); para.Alignment = ParagraphAlignment.CENTER; cell.SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); } } //Save the file FileStream file = new FileStream("Table.docx", FileMode.Create); doc.Write(file); file.Close(); //Launch System.Diagnostics.Process.Start("Table.docx"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Spire.Doc; using Spire.Doc.Documents; namespace Spire.Doc { class Program { static void Main(string[] args) { //Create document Document doc = new Document(); doc.AddSection(); //Declare a paragraph style ParagraphStyle style = new ParagraphStyle(doc); style.Name = "Style"; style.CharacterFormat.Bold = true; style.CharacterFormat.FontName = "NSimSun"; doc.Styles.Add(style); //Add table Table table = doc.Sections[0].AddTable(true); table.ResetCells(3, 2); //Loop through the table for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { TableCell cell = table.Rows[i].Cells[j]; //Add paragraph and fill text Paragraph para=cell.AddParagraph(); para.Text = "Row" + i + " " + "Col" + j; //Apply paragraph style and set table style para.ApplyStyle(style.Name); para.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle; table.Rows[i].Height = 60; } } //Save and Launch doc.SaveToFile("Table.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("Table.docx"); } } }