以个人实际操作为例
1.创建一个类保存表和单元格的属性以及插入表数据的方法
/// <summary>
/// 根据书签插入表格
/// </summary>
/// <param name="wordDoc">Aspose.Words.Document</param>
/// <param name="builder">Aspose.Words.DocumentBuilder</param>
/// <param name="bookMark">书签名称</param>
/// <param name="value">替换值</param>
public static void BookMarkInsertTable(Document wordDoc, DocumentBuilder builder, string bookMark, WordTable value)
{
var bm = wordDoc.Range.Bookmarks[bookMark];
if (bm == null) { return; }
bm.Text = "";
builder.MoveToBookmark(bookMark);
builder.MoveToBookmark(bookMark);
builder.StartTable();//开始画Table
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; // RowAlignment.Center;
builder.RowFormat.Height = value.RowFormatHeight;
//添加每行数据
for (int i = 0; i < value.CellValue.Count; i++)
{
for (int j = 0; j < value.CellValue[i].Count; j++)
{
builder.InsertCell();//插入Table单元格
builder.Font.Size = value.CellValue[i][j].Size;//字体大小
builder.Bold = value.CellValue[i][j].Bold;//是否加粗
//Table此单元格宽度
builder.CellFormat.Width = value.CellValue[i][j].Width;
//Table单元格边框线样式
builder.CellFormat.Borders.LineStyle = (LineStyle)value.CellValue[i][j].LineStyle;
//此单元格中内容垂直对齐方式
builder.ParagraphFormat.Alignment = (ParagraphAlignment)value.CellValue[i][j].Alignment;
builder.CellFormat.VerticalAlignment = (Aspose.Words.Tables.CellVerticalAlignment)value.CellValue[i][j].VerticalAlignment;
builder.CellFormat.HorizontalMerge = (Aspose.Words.Tables.CellMerge)value.CellValue[i][j].HorizontalMerge;
builder.CellFormat.VerticalMerge = (Aspose.Words.Tables.CellMerge)value.CellValue[i][j].VerticalMerge;
//向此单元格中添加内容
builder.Write(value.CellValue[i][j].value);
}
//Table行结束
builder.EndRow();
}
builder.EndTable();
}
/// <summary>
/// word表格数据
/// word表格属性
/// </summary>
public class WordTable
{
/// <summary>
/// 单元格数据,第一行为表头
/// </summary>
public List<List<CellValue>> CellValue { get; set; }
/// <summary>
/// 设置表格最大列数
/// </summary>
public int columnCount { get; set; }
/// <summary>
/// 行高
/// </summary>
public int RowFormatHeight { get; set; } = 20;
}
/// <summary>
/// 单元格值
/// 单元格属性
/// </summary>
public class CellValue
{
/// <summary>
/// 单元格数值
/// </summary>
public string value { get; set; }
/// <summary>
/// 单元格宽度
/// </summary>
public int Width { get; set; }
/// <summary>
/// 字体大小
/// </summary>
public int Size { get; set; }
/// <summary>
/// 字体加粗
/// </summary>
public bool Bold { get; set; } = false;
/// <summary>
/// 单元格边框样式
///None = 0,
///Single = 1,
///Thick = 2,
///Double = 3,
///Hairline = 5,
///Dot = 6,
///DashLargeGap = 7,
///DotDash = 8,
///DotDotDash = 9,
///Triple = 10,
///ThinThickSmallGap = 11,
///ThickThinSmallGap = 12,
///ThinThickThinSmallGap = 13,
///ThinThickMediumGap = 14,
///ThickThinMediumGap = 15,
///ThinThickThinMediumGap = 16,
///ThinThickLargeGap = 17,
///ThickThinLargeGap = 18,
///ThinThickThinLargeGap = 19,
///Wave = 20,
///DoubleWave = 21,
///DashSmallGap = 22,
///DashDotStroker = 23,
///Emboss3D = 24,
///Engrave3D = 25,
///Outset = 26,
///Inset = 27
/// </summary>
public int LineStyle { get; set; } = 1;
/// <summary>
/// 水平对齐
///Left = 0,
///Center = 1,
///Right = 2,
///Justify = 3,
///Distributed = 4,
///ArabicMediumKashida = 5,
///ArabicHighKashida = 7,
///ArabicLowKashida = 8,
///ThaiDistributed = 9
/// </summary>
public int Alignment { get; set; } = 1;
/// <summary>
/// 垂直对齐[Top = 0,Center = 1,Bottom = 2]
/// </summary>
public int VerticalAlignment { get; set; } = 1;
/// <summary>
/// 水平合并[None = 0,First = 1,Previous = 2]
/// </summary>
public int HorizontalMerge { get; set; } = 0;
/// <summary>
/// 垂直合并[None = 0,First = 1,Previous = 2]
/// </summary>
public int VerticalMerge { get; set; } = 0;
}
2.在MVC控制器中调用此方法
using static Inteall.HospitalEquipmentManageMentSystem.Common.AWApi;
//引用命名空间 ***.AWApi
public ActionResult ToContractPrints(string id, int num)//num==1 表示打印正式合同 ,num==2表示打印合同简本
{
//1.根据ID查询合同信息
string filepath = "";
string filter = " where ContractID='" + id + "'";
IList<V_ContractDetial> ContractDetial = _ContractDetialService.GetVContractDetial(filter);//通过合同ID查询出相关的合同信息
//以上为查询数据的操作,可根据自己需要自行修改
if (ContractDetial.Count > 0)//如果有关于这条合同的信息
{
string filenamePDF = DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ".pdf";
string PdfFile = Server.MapPath("~/Uploads/" + filenamePDF);
if (num == 1)//表示打印正式合同
{
filepath = System.Web.HttpContext.Current.Server.MapPath("~/Template/中国中医科学院望京医院正式合同.dot");
}
else if (num == 2)
{
filepath = System.Web.HttpContext.Current.Server.MapPath("~/Template/中国中医科学院望京医院医疗设备采购合同合同简本.dot");
}
Aspose.Words.Document doc = new Aspose.Words.Document(filepath);
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
WordTable value = new WordTable();
value.columnCount = 8;//表的列数量
value.RowFormatHeight = 20;//单元格行高
value.CellValue = new List<List<CellValue>>//CellValue为表的值
{
new List<CellValue>{//表头
new CellValue{value="产品名称",Width=90,Size=12,LineStyle=1,Alignment=0},
new CellValue{value="规格型号",Width=80,Size=12,LineStyle=1,Alignment=0},
new CellValue{value="生产国",Width=50,Size=12,LineStyle=1,Alignment=0},
new CellValue{value="生产厂商",Width=60,Size=12,LineStyle=1,Alignment=0},
new CellValue{value="单位",Width=50,Size=12,LineStyle=1,Alignment=0},
new CellValue{value="数量",Width=50,Size=12,LineStyle=1,Alignment=0},
new CellValue{value="单价",Width=65,Size=12,LineStyle=1,Alignment=0},
new CellValue{value="总价",Width=65,Size=12,Alignment=0},
},
};
int sum;//定义一个变量保存总价 可根据自己需要自行定义
int combined=0;//合计(小写) 可根据需要自行定义
string CapitalMoney = null;//大写金额 可根据需要自行定义
//循环查询得到的数据条数,有几条就添加几行数据
for (int i = 0; i < ContractDetial.Count; i++)
{
//判断绑定的数据源值是否为null,如果为null,则赋值为" "
if (ContractDetial[i].InstrumentChineseName == null)
{
ContractDetial[i].InstrumentChineseName = " ";
}
if (ContractDetial[i].Model == null)
{
ContractDetial[i].Model = " ";
}
if (ContractDetial[i].Country == null)
{
ContractDetial[i].Country = " ";
}
if (ContractDetial[i].SUPPLIER_NAME == null)
{
ContractDetial[i].SUPPLIER_NAME = " ";
}
sum =ContractDetial[i].Account *Convert.ToInt32(ContractDetial[i].Price);// 单个 总价=数量*价格
value.CellValue.Add(new List<CellValue> {
new CellValue{value=ContractDetial[i].InstrumentChineseName,Width=90,Size=12,LineStyle=1,Alignment=0},//产品名称
new CellValue{value=ContractDetial[i].Model,Width=80,Size=12,LineStyle=1,Alignment=0},//规格型号
new CellValue{value=ContractDetial[i].Country,Width=50,Size=12,LineStyle=1,Alignment=0},//生产国
new CellValue{value=ContractDetial[i].SUPPLIER_NAME,Width=60,Size=12,LineStyle=1,Alignment=0},//生产厂商
new CellValue{value="",Width=50,Size=12,LineStyle=1,Alignment=0},//单位
new CellValue{value=""+ContractDetial[i].Account+"",Width=50,Size=12,LineStyle=1,Alignment=0},//数量
new CellValue{value=""+ContractDetial[i].Price+"",Width=65,Size=12,LineStyle=1,Alignment=0},//单价
new CellValue{value=""+sum+"",Width=65,Size=12,LineStyle=1},//总价
});
combined += sum;//总计=单个总价累加
}
CapitalMoney = MoneyOfConversion.ConvertToChinese(combined);//小写金额转换为大写金额
value.CellValue.Add(new List<CellValue> {
new CellValue{value="总价(大写人民币): "+CapitalMoney+" 总价(小写): "+combined+" ",Width=500,Size=12,Alignment=0,HorizontalMerge=1},
});
value.CellValue.Add(new List<CellValue> {
new CellValue{value="*上述成交价为用户现场价。\n" +
"*设备配置(见附页配置清单) 共 页 ,以医院使用科室签字认可配置清单为准",Width=500,Size=12,Alignment=0,HorizontalMerge=1},
});
//指定标签位置插入表格
AWApi.BookMarkInsertTable(doc, builder, "devicetable", value);
doc.Save(PdfFile, SaveFormat.Pdf);
return Json(new { Message = filenamePDF, filePath = filenamePDF, });
}
else {
return Json(new { Message = "无合同信息", filePath = "", });
}
}
3.以上方法中的小写金额转换为大写金额 可创建一个公共类保存
using System.Text.RegularExpressions;
//传入小写金额,返回大写金额
public static String ConvertToChinese(Decimal number)
{
var s = number.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
var d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");
var r = Regex.Replace(d, ".", m => "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万亿兆京垓秭穰"[m.Value[0] - '-'].ToString());
return r;
}
4.以上所需要的DLL文件
链接: https://pan.baidu.com/s/1hHZBGbQXbqBhg4lrkgcT4A 提取码: k4x2