执行程序——控制台应用程序
using System;
using System.IO;
using System.Linq;
using System.Text;
namespace ItemDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("开始创建word");
new WordRelated().CreateWord();
Console.WriteLine("创建结束");
Console.ReadLine();
}
}
}
Word相关操作类 WordRelated.cs
using Aspose.Words;
using Aspose.Words.Drawing;
using Aspose.Words.Tables;
using System.Drawing;
using System.IO;
namespace ItemDemo
{
public class WordRelated
{
private string _savePath = @"C:\Test";
public void CreateWord()
{
/*
* 友情提示:Aspose.Words 在NuGet程序包里有自带的安装包
*
*/
#region 不使用模板情况下使用
//Aspose.Words.Document doc = new Document();//新建一个空白的文档
//Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
设置Word页面的样式
//builder.PageSetup.PaperSize = PaperSize.A4;//A4纸
//builder.PageSetup.Orientation = Aspose.Words.Orientation.Portrait;//方向
//builder.PageSetup.VerticalAlignment = Aspose.Words.PageVerticalAlignment.Top;//垂直对准
//builder.PageSetup.LeftMargin = 42;//页面左边距
//builder.PageSetup.RightMargin = 42;//页面右边距
#endregion
//使用模板
Document doc = new Document(@"C:\Test\template.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
//==========写入文字============
//关于行的样式(对齐方式,行距,缩进等)
var rowPH = builder.ParagraphFormat;
rowPH.Alignment = ParagraphAlignment.Center;//对齐方式
rowPH.LineSpacing = 12;//行距(单倍还是固定值 取决于模板首行的段落设置里面行距的设定)
rowPH.RightIndent = 2;//缩进
//关于文字的样式(字体,大小,颜色等)
var font = builder.Font;
font.Size = 16;//字体大小
font.Bold = false; //是否粗体
font.Underline = Underline.Single; //下划线样式(Single 单条直线)
font.Color = Color.Black;//字体颜色
font.NameFarEast = "宋体";//设置字体
builder.Write("添加的文字"); //向Word写入文字
builder.Writeln(); //空格行(在空格行里面添加文字,启到自动换行作用)
//==========插入表格(此表格合并第一行,合并第第二行第一列至第四行第一列)============
Table table = builder.StartTable();//开始添加表格
//此处可设置多种行的样式 rowf.
RowFormat rowf = builder.RowFormat;
rowf.Height = 40;//设置行高
//遍历行
for (int row = 0; row < 7; row++)
{
//遍历列
for (int col = 0; col < 5; col++)
{
builder.InsertCell(); //插入一个单元格
builder.CellFormat.Width = 70; //设置单元格宽
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; //垂直对齐
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; //水平对齐
builder.CellFormat.FitText = true;//单元格内文字设为多行(默认为单行,会影响单元格宽)
builder.CellFormat.HorizontalMerge = CellMerge.None; //水平合并,None为不合并
builder.CellFormat.VerticalMerge = CellMerge.None;//垂直合并,None为不合并
bool isMerge = false;//是否是合并
if (row == 0)
{
isMerge = true;
if (col == 0)
{
builder.Write($"{row + 1}行{col + 1}列");
builder.CellFormat.HorizontalMerge = CellMerge.First;
}
else
{
builder.CellFormat.HorizontalMerge = CellMerge.Previous;
}
}
if (col == 1 && row >= 1 && row < 4)
{
isMerge = true;
if (row == 2)
{
builder.Write($"{row + 1}行{col + 1}列");
builder.CellFormat.VerticalMerge = CellMerge.First;
}
else
{
builder.CellFormat.VerticalMerge = CellMerge.Previous;
}
}
//合并时只需在首次添加即可
if (!isMerge)
builder.Write($"{row + 1}行{col + 1}列");
}
//table.AutoFit(AutoFitBehavior.FixedColumnWidths); //可固定表格宽度不会自适应
builder.EndRow();//结束row行
}
builder.EndTable(); //结束表格(不能漏掉)
//拓展:设置这个表格的上下左右,内部水平,垂直的线为白色(当背景为白色的时候就相当于隐藏边框了)
//table.SetBorder(BorderType.Left, LineStyle.Double, 1, Color.White, false);
//table.SetBorder(BorderType.Top, LineStyle.Double, 1, Color.White, false);
//table.SetBorder(BorderType.Right, LineStyle.Double, 1, Color.White, false);
//table.SetBorder(BorderType.Bottom, LineStyle.Double, 1, Color.White, false);
//table.SetBorder(BorderType.Vertical, LineStyle.Double, 1, Color.White, false);
//==========写入文字============
var pathImage = @"";//图片地址
if (System.IO.File.Exists(pathImage))
{
Shape shape = builder.InsertImage(pathImage);
shape.Width = 100;
shape.Height = 100;
shape.WrapType = WrapType.None;
shape.HorizontalAlignment = HorizontalAlignment.Right;
}
//判断是否存在文件夹
if (!Directory.Exists(_savePath))
{
Directory.CreateDirectory(_savePath);
}
//保存
doc.Save(Path.Combine(_savePath, "test.doc"));
}
}
}