感谢技术大哥,提供代码,万分感谢。
spire.doc 数据填充完毕后,最后一页如何用空行填充,判断最后一页,如果只有2条数据,就添加15个空行来填充,如下截图:
以下代码复制便可使用:
/// <summary>
/// 对齐方式
/// </summary>
/// <param name="cell"></param>
/// <param name="ver"></param>
/// <param name="hor"></param>
/// <returns></returns>
private Paragraph VerticalHorizontal(TableCell cell, Spire.Doc.Documents.VerticalAlignment ver, Spire.Doc.Documents.HorizontalAlignment hor)
{
cell.CellFormat.VerticalAlignment = ver;
Paragraph para = cell.AddParagraph();
para.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
return para;
}
/// <summary>
/// 档案编号
/// </summary>
/// <param name="code"></param>
private string CreateCode(string code)
{
var newcode = string.Empty;
if (!string.IsNullOrWhiteSpace(code))
{
var path = code.Split('\\');
if (path.Length > 4)
{
var s3 = path[path.Length - 2];
var s2 = path[path.Length - 3];
var s1 = path[path.Length - 4];
newcode = string.Format("档案编号:{0}-{1}-{2}", s1, s2, s3);
}
}
return newcode;
}
private void Header(Document doc, Section section, string code)
{
Paragraph p0 = section.HeadersFooters.Header.AddParagraph();
TextRange t = p0.AppendText("目录");
p0.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
t.CharacterFormat.FontName = "宋体";
t.CharacterFormat.FontSize = 14;
t.CharacterFormat.Bold = true;
// 档案编号
Paragraph p1 = section.HeadersFooters.OddHeader.AddParagraph();
var savecode = CreateCode(code);
TextRange tr1 = p1.AppendText(savecode);
tr1.CharacterFormat.FontName = "宋体";
tr1.CharacterFormat.FontSize = 11;
tr1.CharacterFormat.Bold = true;
p1.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
}
private void ml(List<string> list2, List<int> pageNum, string code,int page)
{
//数据类型转换
List<TableData> list = new List<TableData>();
for (int d = 0; d < list2.Count; d++)
{
TableData data = new TableData();
data.Index = d + 1;
data.Name = list2[d].ToString();
data.PageNum = pageNum[d];
list.Add(data);
}
//创建Word文档
Document doc = new Document();
Spire.Doc.Section section = doc.AddSection();
//调用Header方法
Header(doc, section, code);
Paragraph p0 = section.AddParagraph();
TextRange t = p0.AppendText("目录");
p0.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
t.CharacterFormat.FontName = "宋体";
t.CharacterFormat.FontSize = 14;
t.CharacterFormat.Bold = true;
// 档案编号
Paragraph p1 = section.AddParagraph();
var savecode = CreateCode(code);
TextRange tr1 = p1.AppendText(savecode);
tr1.CharacterFormat.FontName = "宋体";
tr1.CharacterFormat.FontSize = 11;
tr1.CharacterFormat.Bold = true;
p1.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
section.PageSetup.DifferentFirstPageHeaderFooter = true;
// 表头
Table table = section.AddTable(true); //新建表格
//Header(doc, section, code);
//table.TableFormat.Borders.BorderType = Spire.Doc.Documents.BorderStyle.Single;
TableRow head = table.AddRow(); //添加行
head.IsHeader = true; //设为表头
//head.Height = 16;
//head.HeightType = Spire.Doc.Documents.TableRowHeightType.AtLeast;
Spire.Doc.TableCell cell1 = head.AddCell();
Spire.Doc.TableCell cell2 = head.AddCell();
Spire.Doc.TableCell cell3 = head.AddCell();
Spire.Doc.TableCell cell4 = head.AddCell();
//Paragraph p_h = section.AddParagraph();
cell1.SetCellWidth(74, CellWidthType.Point);
cell2.SetCellWidth(300, CellWidthType.Point);
cell3.SetCellWidth(60, CellWidthType.Point);
cell4.SetCellWidth(70, CellWidthType.Point);
head.Height = 40;
TextRange text1 = VerticalHorizontal(cell1, Spire.Doc.Documents.VerticalAlignment.Middle, Spire.Doc.Documents.HorizontalAlignment.Center).AppendText("顺序号");
TextRange text2 = VerticalHorizontal(cell2, Spire.Doc.Documents.VerticalAlignment.Middle, Spire.Doc.Documents.HorizontalAlignment.Center).AppendText("资料名称");
TextRange text3 = VerticalHorizontal(cell3, Spire.Doc.Documents.VerticalAlignment.Middle, Spire.Doc.Documents.HorizontalAlignment.Center).AppendText("页码");
TextRange text4 = VerticalHorizontal(cell4, Spire.Doc.Documents.VerticalAlignment.Middle, Spire.Doc.Documents.HorizontalAlignment.Center).AppendText("附注");
text1.CharacterFormat.FontName = "宋体";
text2.CharacterFormat.FontName = "宋体";
text3.CharacterFormat.FontName = "宋体";
text4.CharacterFormat.FontName = "宋体";
text1.CharacterFormat.FontSize = 11;
text2.CharacterFormat.FontSize = 11;
text3.CharacterFormat.FontSize = 11;
text4.CharacterFormat.FontSize = 11;
// 数据填充
for (int i = 0; i < list.Count; i++)
{
TableRow row = new TableRow(doc);
row.Height = 40;
for (int j = 0; j < 4; j++)
{
//TableCell cl = new TableCell(doc);
TableCell cl = row.AddCell();
cl.CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;
Paragraph p = cl.AddParagraph();
p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
TextRange textRange = new TextRange(doc);
switch (j)
{
case 0:
textRange = p.AppendText(list[i].Index.ToString());
break;
case 1:
textRange = p.AppendText(list[i].Name);
break;
case 2:
if (list[i].Index==list.Count) {
textRange = p.AppendText(list[i].PageNum.ToString() + "-" + page);
}
else {
textRange = p.AppendText(list[i].PageNum.ToString());
}
break;
case 3:
textRange = p.AppendText(list[i].Note);
break;
default:
break;
}
row.Cells.Add(cl);
textRange.CharacterFormat.FontName = "宋体";
}
table.Rows.Add(row);
}