当前位置:网站首页>NPOI 创建Word
NPOI 创建Word
2022-06-12 21:26:00 【曹振华】
效果
代码
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);
}
}
}
}
边栏推荐
- Sorting out the knowledge points of primary and secondary indicators
- Yanghui triangle code implementation
- 模块八:设计消息队列存储消息数据的MySQL表
- 指针与数组&指针与const&结构体与const
- 二分查找
- Lombok package is successfully installed, but the runtime prompts that get, set method and constructor solution cannot be found
- String Basics
- Draw according to weight
- Data visualization - biaxial comparison effect
- Mxnet record IO details
猜你喜欢
Sorting out the knowledge points of primary and secondary indicators
nn. PReLU(planes)
C language learning notes (II)
Risk control modeling X: Discussion on problems existing in traditional modeling methods and Exploration on improvement methods
Lintcode:127. Topology sorting
The salted fish has been transmitted for 5W times, and the latest spring recruit face-to-face test questions of bytes have been leaked
Product Manager: "click here to jump to any page I want to jump" -- decoupling efficiency improving artifact "unified hop routing"
一款高颜值的MySQL管理工具
Test basis: unit test
shell语言
随机推荐
选择排序
A high-value MySQL management tool
#981 Time Based Key-Value Store
Shell script Basics
shell语言
Data visualization - broken line area chart
#886 Possible Bipartition
Limit of advanced mathematics
Solution of multi machine room dynamic loop status network touch screen monitoring
【目标检测】|Dive Deeper Into Box for Object Detection 基于FCOS新训练方法
Ubuntu 16.04 installing mysql5.6
新品发布丨竣达智能综合环境监测终端
The Post-00 financial woman with a monthly salary of 2W conquered the boss with this set of report template
New product release Junda intelligent integrated environmental monitoring terminal
Li Mu [practical machine learning] 1.4 data annotation
torch. Finfo function
drf 接收嵌套数据并创建对象, 解决:drf NOT NULL constraint failed
指针与数组&指针与const&结构体与const
Preliminary understanding of regular expressions (regex)
求解一维数组前缀和