spire.doc 感谢技术大哥查看,如何让创建的表格每页只显示17条数据,如果超过17条数据,第二页显示表头和档案编号也要显示出来。
/// <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;
}
private void Header(Document doc)
{
Spire.Doc.Section section = doc.AddSection();
//section.PageSetup.DifferentOddAndEvenPagesHeaderFooter = true;
Paragraph oddHeader = section.HeadersFooters.OddHeader.AddParagraph();
TextRange oddHT = oddHeader.AppendText("档案编号:");
//Paragraph evenHeader = section.HeadersFooters.EvenHeader.AddParagraph();
//TextRange evenHT = evenHeader.AppendText("Time is the most valuable thing");
//Format the headers
oddHeader.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
oddHT.CharacterFormat.FontName = "宋体";
oddHT.CharacterFormat.FontSize = 14;
//oddHT.CharacterFormat.TextColor = Color.Green;
oddHT.CharacterFormat.Bold = true;
}
/// <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 ml(List<string> list2, List<int> pageNum, string code,int page)
{
#region 数据类型转换
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);
}
#endregion
//创建Word文档
Document doc = new Document();
Spire.Doc.Section section = doc.AddSection();
#region 目录
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;
#endregion
#region 档案编号
Paragraph p1 = section.AddParagraph();
var savecode = CreateCode(code);
TextRange tr1 = p1.AppendText(savecode);
tr1.CharacterFormat.FontName = "宋体";
tr1.CharacterFormat.FontSize = 11;
p1.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
#endregion
#region 表头
Table table = section.AddTable(); //新建表格
//table.ResetCells(10,4);
table.TableFormat.Borders.BorderType = Spire.Doc.Documents.BorderStyle.Single;
TableRow head = table.AddRow(); ; //添加行
head.IsHeader = true; //设为表头
head.Height = 16;
//head.HeightType = 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;
#endregion
#region 数据填充
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);
}
#endregion
//保存文档
doc.SaveToFile("Table.docx", Spire.Doc.FileFormat.Docx);
//System.Diagnostics.Process.Start("Table.docx");
//保存为PDF格式
//doc.SaveToFile("abc.PDF", Spire.Doc.FileFormat.PDF);
wordtopdf("Table.docx");
}