效果
代码
using NPOI.OpenXmlFormats.Wordprocessing;
using NPOI.SS.UserModel;
using NPOI.XWPF.UserModel;
using System;
using System.IO;
namespace NPOIWord
{
class Program
{
static void Main(string[] args)
{
string path = Path.Combine(Directory.GetCurrentDirectory()
, "word", Guid.NewGuid() + ".docx");
Save_Word(path);
}
private static void Save_Word(string path)
{
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
{
//初始化文档
XWPFDocument m_Doc = new XWPFDocument();
#region 创建段落
XWPFParagraph paragraph = m_Doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.SpacingBeforeLines = 40;
paragraph.SpacingAfterLines = 40;
XWPFRun run = paragraph.CreateRun();
run.FontFamily = "宋体";
run.FontSize = 14;
run.IsBold = true;
run.SetText("西安二日游行程单");
paragraph = m_Doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.LEFT;
paragraph.SpacingBeforeLines = 40;
paragraph.SpacingAfterLines = 40;
run = paragraph.CreateRun();
run.FontFamily = "宋体";
run.FontSize = 12;
run.IsBold = true;
run.SetText("一、行程特色");
paragraph = m_Doc.CreateParagraph();
paragraph.SpacingAfterLines = 40;
paragraph.Alignment = ParagraphAlignment.LEFT;
run = paragraph.CreateRun();
run.FontFamily = "宋体";
run.FontSize = 12;
run.SetText("测试测试测试测试测试测试测试测试测试测试");
paragraph = m_Doc.CreateParagraph();
paragraph.SpacingBeforeLines = 40;
paragraph.SpacingAfterLines = 40;
paragraph.Alignment = ParagraphAlignment.LEFT;
run = paragraph.CreateRun();
run.FontFamily = "宋体";
run.FontSize = 12;
run.IsBold = true;
run.SetText("二、目标客户");
paragraph = m_Doc.CreateParagraph();
paragraph.SpacingAfterLines = 40;
paragraph.Alignment = ParagraphAlignment.LEFT;
run = paragraph.CreateRun();
run.FontFamily = "宋体";
run.FontSize = 12;
run.SetText("测试测试测试测试测试测试测试测试测试测试");
paragraph = m_Doc.CreateParagraph();
paragraph.SpacingBeforeLines = 40;
paragraph.SpacingAfterLines = 40;
paragraph.Alignment = ParagraphAlignment.LEFT;
run = paragraph.CreateRun();
run.FontFamily = "宋体";
run.FontSize = 12;
run.IsBold = true;
run.SetText("三、行程安排");
#endregion
#region 创建表格
XWPFTable table = m_Doc.CreateTable(3, 4);
table.Width = 5000;
#region 第一行
CT_P para = new CT_P();
paragraph = new XWPFParagraph(para, table.Body);
paragraph.Alignment = ParagraphAlignment.CENTER;
run = paragraph.CreateRun();
run.FontFamily = "宋体";
run.FontSize = 12;
run.IsBold = true;
run.SetText("日期");
table.GetRow(0).GetCell(0).SetColor("#d1d1d2");
table.GetRow(0).GetCell(0).SetParagraph(paragraph);
para = new CT_P();
paragraph = new XWPFParagraph(para, table.Body);
paragraph.Alignment = ParagraphAlignment.CENTER;
run = paragraph.CreateRun();
run.FontFamily = "宋体";
run.FontSize = 12;
run.IsBold = true;
run.SetText("行程安排");
table.GetRow(0).GetCell(1).SetColor("#d1d1d2");
table.GetRow(0).GetCell(1).SetParagraph(paragraph);
para = new CT_P();
paragraph = new XWPFParagraph(para, table.Body);
paragraph.Alignment = ParagraphAlignment.CENTER;
run = paragraph.CreateRun();
run.FontFamily = "宋体";
run.FontSize = 12;
run.IsBold = true;
run.SetText("用餐");
table.GetRow(0).GetCell(2).SetColor("#d1d1d2");
table.GetRow(0).GetCell(2).SetParagraph(paragraph);
para = new CT_P();
paragraph = new XWPFParagraph(para, table.Body);
paragraph.Alignment = ParagraphAlignment.CENTER;
run = paragraph.CreateRun();
run.FontFamily = "宋体";
run.FontSize = 12;
run.IsBold = true;
run.SetText("住宿");
table.GetRow(0).GetCell(3).SetColor("#d1d1d2");
table.GetRow(0).GetCell(3).SetParagraph(paragraph);
#endregion
#region 第二行
para = new CT_P();
paragraph = new XWPFParagraph(para, table.Body);
paragraph.Alignment = ParagraphAlignment.CENTER;
run = paragraph.CreateRun();
run.FontFamily = "宋体";
run.FontSize = 12;
run.IsBold = true;
run.SetText("D1");
table.GetRow(1).GetCell(0).SetParagraph(paragraph);
para = new CT_P();
paragraph = new XWPFParagraph(para, table.Body);
paragraph.Alignment = ParagraphAlignment.CENTER;
run = paragraph.CreateRun();
run.FontFamily = "宋体";
run.FontSize = 12;
run.IsBold = true;
run.SetText("西安");
table.GetRow(1).GetCell(1).SetParagraph(paragraph);
para = new CT_P();
paragraph = new XWPFParagraph(para, table.Body);
paragraph.Alignment = ParagraphAlignment.CENTER;
run = paragraph.CreateRun();
run.FontFamily = "宋体";
run.FontSize = 12;
run.IsBold = true;
run.SetText("--,中,晚");
table.GetRow(1).GetCell(2).SetParagraph(paragraph);
para = new CT_P();
paragraph = new XWPFParagraph(para, table.Body);
paragraph.Alignment = ParagraphAlignment.CENTER;
run = paragraph.CreateRun();
run.FontFamily = "宋体";
run.FontSize = 12;
run.IsBold = true;
run.SetText("西安");
table.GetRow(1).GetCell(3).SetParagraph(paragraph);
#endregion
#region 第三行
//合并
table.GetRow(2).MergeCells(0, 3);
para = new CT_P();
paragraph = new XWPFParagraph(para, table.Body);
paragraph.SpacingBeforeLines = 20;
paragraph.Alignment = ParagraphAlignment.LEFT;
run = paragraph.CreateRun();
run.FontFamily = "宋体";
run.FontSize = 10;
run.SetText(" 08:30 西安新城广场集合");
table.GetRow(2).GetCell(0).SetParagraph(paragraph);
//换行
run = paragraph.CreateRun();
run.AddBreak(BreakType.TEXTWRAPPING);
paragraph = new XWPFParagraph(para, table.Body);
paragraph.Alignment = ParagraphAlignment.LEFT;
run = paragraph.CreateRun();
run.FontFamily = "宋体";
run.FontSize = 10;
run.SetText(" 08:30 西安新城广场集合");
table.GetRow(2).GetCell(0).SetParagraph(paragraph);
run = paragraph.CreateRun();
run.AddBreak(BreakType.TEXTWRAPPING);
paragraph = new XWPFParagraph(para, table.Body);
paragraph.Alignment = ParagraphAlignment.LEFT;
run = paragraph.CreateRun();
run.FontFamily = "宋体";
run.FontSize = 10;
run.SetText(" 08:30 西安新城广场集合");
table.GetRow(2).GetCell(0).SetParagraph(paragraph);
#endregion
#endregion
//5、写入到文件
m_Doc.Write(fs);
}
}
}
}