using Aspose.Words;
namespace HStation.ReportFile
{
public static class SimulationAsposeWordHelper
{
public enum MarkTypeEnum
{
///
/// 中括号 [[]]
///
DEFAULT,
///
/// 花括号 {{}}
///
CURLYBRACES
}
///
/// 基于模版新建Word文件
///
/// 模板流
public static void OpenTempelteStream(this Document doc, System.IO.Stream stream)
{
doc = new Document(stream);
}
///
/// 文本域赋值用法
///
/// key
/// value
public static void ExecuteField(this Document doc, string[] fieldNames, object[] fieldValues)
{
doc.MailMerge.Execute(fieldNames, fieldValues);
}
///
/// 文本域赋值用法
///
/// key
/// value
public static void ExecuteField(this Document doc, string fieldName, object fieldValue)
{
doc.MailMerge.Execute(new string[] { fieldName }, new object[] { fieldValue });
}
///
/// Doc文件保存
///
/// 文件路径+文件名
public static void SaveDocx(this Document doc, string filename)
{
doc.Save(filename, SaveFormat.Doc);
}
///
/// 只读
///
public static void ReadOnly(this Document doc)
{
doc.Protect(ProtectionType.ReadOnly);
}
///
/// 文本域处理泛型集合赋值
///
///
///
public static void Execute(this Document doc, TEntity entity)
{
var type = entity.GetType();
var properties = type.GetProperties();
List names = new List();
List values = new List();
foreach (var item in properties)
{
names.Add(item.Name);
values.Add(item.GetValue(entity, null).ToString());
}
doc.MailMerge.Execute(names.ToArray(), values.ToArray());
}
///
/// 文本域处理键值对赋值
///
///
public static void Execute(this Document doc, Dictionary entity)
{
List names = new List();
List values = new List();
foreach (var item in entity)
{
names.Add(item.Key);
values.Add(item.Value);
}
doc.MailMerge.Execute(names.ToArray(), values.ToArray());
}
public static Aspose.Words.Font BuilderIni(this Aspose.Words.DocumentBuilder builder,
string _defaultFontName = "宋体",
int red = 0, int green = 0, int blue = 0,
int size = 16
)
{
System.Drawing.Color color = System.Drawing.Color.FromArgb(red, green, blue);
builder.Font.Color = color;
builder.Font.Size = size;
builder.Font.Name = _defaultFontName;
return builder.Font;
}
///
/// 设置字体(默认黑色)
///
/// 字体
/// 字体大小
/// 是否加粗(无效)
///
public static Aspose.Words.Font SetBuilderFont(this Aspose.Words.DocumentBuilder builder, string fontName, double fontSize, bool isBlod = false)
{
builder.Font.Bold = isBlod;
builder.Font.Size = fontSize;
builder.Font.Name = fontName;
builder.Font.Color = System.Drawing.Color.Black;
return builder.Font;
}
///
/// 设置字体
///
/// 字体
/// 字体大小
/// 字体颜色
/// 是否加粗(无效)
///
public static Aspose.Words.Font SetBuilderFont(this Aspose.Words.DocumentBuilder builder, string fontName, double fontSize, System.Drawing.Color color, bool isBlod = false)
{
builder.Font.Bold = isBlod;
builder.Font.Color = color;
builder.Font.Size = fontSize;
builder.Font.Name = fontName;
return builder.Font;
}
///
/// 设置纸张
///
///
/// 方向
/// 上边距
/// 下边距
/// 左边距
/// 右边距
public static void SetSheet(this Aspose.Words.DocumentBuilder builder,
Aspose.Words.Orientation orientation = Aspose.Words.Orientation.Portrait,
double topMargin = 50, double bottomMargin = 50, double leftMargin = 50, double rightMargin = 50)
{
builder.PageSetup.PaperSize = Aspose.Words.PaperSize.A4;//A4纸
builder.PageSetup.Orientation = Aspose.Words.Orientation.Portrait;//方向
builder.PageSetup.VerticalAlignment = Aspose.Words.PageVerticalAlignment.Top;//垂直对准
builder.PageSetup.LeftMargin = leftMargin;//页面左边距
builder.PageSetup.RightMargin = rightMargin;//页面右边距
builder.PageSetup.TopMargin = topMargin;
builder.PageSetup.BottomMargin = bottomMargin;
}
///
/// 创建横向合并的单元格
///
///
/// 文本
/// 字体
/// 横向合并格数
/// 边框样式
/// 是否加粗
/// 是否倾斜
/// 下划线格式
public static void CreateHorizontalMerge(this Aspose.Words.DocumentBuilder builder, string text, int colspanNum, Aspose.Words.ParagraphAlignment alignment,
LineStyle lineStyle = LineStyle.Single,
LineStyle top = LineStyle.None, LineStyle bottom = LineStyle.None,
LineStyle left = LineStyle.None, LineStyle right = LineStyle.None,
bool isBold = false, bool isItalic = false, Underline isUnderline = Underline.None)
{
builder.InsertCell();
builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.First;
builder.CellFormat.Borders[BorderType.Bottom].LineStyle = bottom;
builder.CellFormat.Borders[BorderType.Top].LineStyle = top;
builder.CellFormat.Borders[BorderType.Left].LineStyle = left;
builder.CellFormat.Borders[BorderType.Right].LineStyle = right;
builder.Bold = isBold;
builder.Italic = isItalic;
builder.Underline = isUnderline;
builder.ParagraphFormat.Alignment = alignment;
builder.Write(text);
for (int i = 0; i < colspanNum; i++)
{
builder.InsertCell();
builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.Previous;
}
}
///
/// 开始纵向合并
///
///
/// 文本
/// 字体
/// 边框样式
/// 单元格宽
/// 是否加粗
/// 是否倾斜
/// 下划线格式
public static void FirstCellVerticalMerge(this Aspose.Words.DocumentBuilder builder, string text, ParagraphAlignment alignment,
LineStyle lineStyle = LineStyle.Single, double width = -1,
bool isBold = false, bool isItalic = false, Underline isUnderline = Underline.None)
{
builder.InsertCell();
builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.First;
builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None;
builder.CellFormat.Borders.LineStyle = lineStyle;
builder.CellFormat.Width = width;
builder.Bold = isBold;
builder.Italic = isItalic;
builder.Underline = isUnderline;
builder.ParagraphFormat.Alignment = alignment;
builder.Write(text);
}
///
/// 开始纵向合并(空单元格)
///
///
/// 边框样式
/// 单元格宽
public static void FirstCellVerticalMerge(this Aspose.Words.DocumentBuilder builder, LineStyle lineStyle = LineStyle.Single, double width = -1)
{
builder.InsertCell();
builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.First;
builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None;
builder.CellFormat.Borders.LineStyle = lineStyle;
builder.CellFormat.Width = width;
}
///
/// 附加纵向合并
///
///
/// 边框样式
/// 单元格宽
public static void PreviousCellVerticalMerge(this Aspose.Words.DocumentBuilder builder, LineStyle lineStyle = LineStyle.Single, double width = -1)
{
builder.InsertCell();
builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.Previous;
builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None;
builder.CellFormat.Borders.LineStyle = lineStyle;
builder.CellFormat.Width = width;
}
///
/// 创建单元格(默认左对齐)
///
///
/// 文本
/// 字体
/// 边框样式
/// 垂直格式
/// 横向格式
/// 单元格宽
/// 是否加粗
/// 是否倾斜
/// 下划线格式
///
public static void CreateRowCell(this Aspose.Words.DocumentBuilder builder,
string text,
LineStyle lineStyle = LineStyle.Single,
ParagraphAlignment alignment = ParagraphAlignment.Left,
bool isBold = false,
Aspose.Words.Tables.CellMerge verticalMerge = Aspose.Words.Tables.CellMerge.None,
Aspose.Words.Tables.CellMerge horizontalMerge = Aspose.Words.Tables.CellMerge.None,
double width = -1,
LineStyle top = LineStyle.None, LineStyle bottom = LineStyle.None,
LineStyle left = LineStyle.None, LineStyle right = LineStyle.None,
bool isItalic = false, Underline isUnderline = Underline.None)
{
builder.InsertCell();
builder.CellFormat.VerticalMerge = verticalMerge;
builder.CellFormat.HorizontalMerge = horizontalMerge;
builder.CellFormat.Borders.LineStyle = lineStyle;
builder.CellFormat.Borders[BorderType.Bottom].LineStyle = bottom;
builder.CellFormat.Borders[BorderType.Top].LineStyle = top;
builder.CellFormat.Borders[BorderType.Left].LineStyle = left;
builder.CellFormat.Borders[BorderType.Right].LineStyle = right;
builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;
builder.ParagraphFormat.Alignment = alignment;
builder.CellFormat.Width = width;
//builder.CellFormat.Borders.LineStyle = lineStyle;
builder.Bold = isBold;
builder.Italic = isItalic;
builder.Underline = isUnderline;
if (text == null)
builder.Write("");
else
builder.Write(text);
}
public static void CreateRowCellLink(this Aspose.Words.DocumentBuilder builder,
string text, string url,
LineStyle lineStyle = LineStyle.Single,
ParagraphAlignment alignment = ParagraphAlignment.Left,
bool isBold = false,
Aspose.Words.Tables.CellMerge verticalMerge = Aspose.Words.Tables.CellMerge.None,
Aspose.Words.Tables.CellMerge horizontalMerge = Aspose.Words.Tables.CellMerge.None,
double width = -1,
LineStyle top = LineStyle.None, LineStyle bottom = LineStyle.None,
LineStyle left = LineStyle.None, LineStyle right = LineStyle.None,
bool isItalic = false, Underline isUnderline = Underline.None)
{
builder.InsertCell();
builder.CellFormat.VerticalMerge = verticalMerge;
builder.CellFormat.HorizontalMerge = horizontalMerge;
builder.CellFormat.Borders.LineStyle = lineStyle;
builder.CellFormat.Borders[BorderType.Bottom].LineStyle = bottom;
builder.CellFormat.Borders[BorderType.Top].LineStyle = top;
builder.CellFormat.Borders[BorderType.Left].LineStyle = left;
builder.CellFormat.Borders[BorderType.Right].LineStyle = right;
builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;
builder.ParagraphFormat.Alignment = alignment;
builder.CellFormat.Width = width;
builder.Bold = isBold;
builder.Italic = isItalic;
builder.Underline = isUnderline;
if (string.IsNullOrEmpty(text))
{
builder.Write("");
}
else
{
var hy = builder.InsertHyperlink(text, url, false);// builder.Write(text);
}
}
public static void CreateRowCell(this Aspose.Words.DocumentBuilder builder,
double text,
LineStyle lineStyle = LineStyle.Single,
ParagraphAlignment alignment = ParagraphAlignment.Left,
Aspose.Words.Tables.CellMerge verticalMerge = Aspose.Words.Tables.CellMerge.None,
Aspose.Words.Tables.CellMerge horizontalMerge = Aspose.Words.Tables.CellMerge.None,
LineStyle top = LineStyle.None, LineStyle bottom = LineStyle.None,
LineStyle left = LineStyle.None, LineStyle right = LineStyle.None,
double width = -1,
bool isBold = false, bool isItalic = false, Underline isUnderline = Underline.None)
{
CreateRowCell(builder, text.ToString(), alignment, lineStyle, verticalMerge, horizontalMerge, top, bottom, left, right, width, isBold);
}
public static void CreateRowCell(this Aspose.Words.DocumentBuilder builder,
double? text,
LineStyle lineStyle = LineStyle.Single,
ParagraphAlignment alignment = ParagraphAlignment.Left,
Aspose.Words.Tables.CellMerge verticalMerge = Aspose.Words.Tables.CellMerge.None,
Aspose.Words.Tables.CellMerge horizontalMerge = Aspose.Words.Tables.CellMerge.None,
LineStyle top = LineStyle.None, LineStyle bottom = LineStyle.None,
LineStyle left = LineStyle.None, LineStyle right = LineStyle.None,
double width = -1,
bool isBold = false, bool isItalic = false, Underline isUnderline = Underline.None)
{
if (text == null)
CreateRowCell(builder, "", alignment, lineStyle, verticalMerge, horizontalMerge, top, bottom, left, right, width, isBold);
else
CreateRowCell(builder, text.ToString(), alignment, lineStyle, verticalMerge, horizontalMerge, top, bottom, left, right, width, isBold);
}
///
/// 创建单元格
///
///
/// 文本
/// 字体
/// 文字位置
/// 边框样式
/// 垂直格式
/// 横向格式
/// 单元格宽
/// 是否加粗
/// 是否倾斜
/// 下划线格式
public static void CreateRowCell(this Aspose.Words.DocumentBuilder builder, string text, ParagraphAlignment alignment,
LineStyle lineStyle = LineStyle.Single,
Aspose.Words.Tables.CellMerge verticalMerge = Aspose.Words.Tables.CellMerge.None,
Aspose.Words.Tables.CellMerge horizontalMerge = Aspose.Words.Tables.CellMerge.None,
LineStyle top = LineStyle.None, LineStyle bottom = LineStyle.None,
LineStyle left = LineStyle.None, LineStyle right = LineStyle.None,
double width = -1,
bool isBold = false, bool isItalic = false, Underline isUnderline = Underline.None)
{
builder.InsertCell();
builder.CellFormat.VerticalMerge = verticalMerge;
builder.CellFormat.HorizontalMerge = horizontalMerge;
//builder.CellFormat.Borders.LineStyle = lineStyle;
builder.CellFormat.Borders[BorderType.Bottom].LineStyle = bottom;
builder.CellFormat.Borders[BorderType.Top].LineStyle = top;
builder.CellFormat.Borders[BorderType.Left].LineStyle = left;
builder.CellFormat.Borders[BorderType.Right].LineStyle = right;
builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;
builder.ParagraphFormat.Alignment = alignment;
builder.CellFormat.Width = width;
//builder.CellFormat.Borders.LineStyle = lineStyle;
builder.Bold = isBold;
builder.Italic = isItalic;
builder.Underline = isUnderline;
builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.FromArgb(255, 255, 255);
builder.Write(text);
}
///
/// 创建单元格(带背景颜色)
///
///
/// 文本
/// 字体
/// 文字位置
/// 边框样式
/// 垂直格式
/// 横向格式
/// 单元格宽
/// 是否加粗
/// 是否倾斜
/// RGB值
/// RGB值
/// RGB值
///
public static void CreateRowCell_withBackgroundColor(this Aspose.Words.DocumentBuilder builder, string text, ParagraphAlignment alignment,
LineStyle lineStyle = LineStyle.Single,
Aspose.Words.Tables.CellMerge verticalMerge = Aspose.Words.Tables.CellMerge.None,
Aspose.Words.Tables.CellMerge horizontalMerge = Aspose.Words.Tables.CellMerge.None,
LineStyle top = LineStyle.None, LineStyle bottom = LineStyle.None,
LineStyle left = LineStyle.None, LineStyle right = LineStyle.None,
double width = -1, bool isBold = false,
string rgbstr = "", bool isItalic = false)
{
builder.InsertCell();
builder.CellFormat.VerticalMerge = verticalMerge;
builder.CellFormat.HorizontalMerge = horizontalMerge;
//builder.CellFormat.Borders.LineStyle = lineStyle;
builder.CellFormat.Borders[BorderType.Bottom].LineStyle = bottom;
builder.CellFormat.Borders[BorderType.Top].LineStyle = top;
builder.CellFormat.Borders[BorderType.Left].LineStyle = left;
builder.CellFormat.Borders[BorderType.Right].LineStyle = right;
builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;
builder.ParagraphFormat.Alignment = alignment;
builder.CellFormat.Width = width;
//builder.CellFormat.Borders.LineStyle = lineStyle;
builder.Bold = isBold;
builder.Italic = isItalic;
//builder.CellFormat.Shading.ForegroundPatternColor = System.Drawing.Color.FromArgb(255, 255, 255);//设置单元格背景色
if (!string.IsNullOrEmpty(rgbstr))
{
var rgblist = rgbstr.Split(',').ToList();
if (rgblist.Count == 3)
builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.FromArgb(Convert.ToInt32(rgblist[0]), Convert.ToInt32(rgblist[1]), Convert.ToInt32(rgblist[2]));//设置单元格背景色
}
//builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.FromArgb(red, green, blue);//设置单元格背景色
//builder.Underline = isUnderline;
builder.Write(text);
}
///
/// 创建空的单元格
///
///
/// 边框样式
/// 垂直格式
/// 横向格式
///
public static void CreateRowCell(this Aspose.Words.DocumentBuilder builder,
LineStyle lineStyle = LineStyle.Single,
Aspose.Words.Tables.CellMerge verticalMerge = Aspose.Words.Tables.CellMerge.None,
Aspose.Words.Tables.CellMerge horizontalMerge = Aspose.Words.Tables.CellMerge.None,
LineStyle top = LineStyle.None, LineStyle bottom = LineStyle.None,
LineStyle left = LineStyle.None, LineStyle right = LineStyle.None,
double width = -1)
{
builder.InsertCell();
builder.CellFormat.VerticalMerge = verticalMerge;
builder.CellFormat.HorizontalMerge = horizontalMerge;
//builder.CellFormat.Borders.LineStyle = lineStyle;
builder.CellFormat.Borders[BorderType.Bottom].LineStyle = bottom;
builder.CellFormat.Borders[BorderType.Top].LineStyle = top;
builder.CellFormat.Borders[BorderType.Left].LineStyle = left;
builder.CellFormat.Borders[BorderType.Right].LineStyle = right;
builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.FromArgb(255, 255, 255);
builder.CellFormat.Width = width;
}
///
/// Pdf文件保存
///
/// 文件路径+文件名
public static void SavePdf(this Document doc, string filename)
{
doc.Save(filename, SaveFormat.Pdf);
}
}
}