Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Sep 10, 2019 1:00 am

spire.doc 感谢技术大哥查看,如何让创建的表格每页只显示17条数据,如果超过17条数据,第二页显示表头和档案编号也要显示出来。

QQ截图20190910085714.jpg







/// <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");


}

fhn123456
 
Posts: 3
Joined: Tue Sep 10, 2019 12:35 am

Tue Sep 10, 2019 3:52 am

您好,

感谢咨询。
在检查代码后,我发现您没有调用自定义的Header方法。我加在了AddSection之后。
Code: Select all
            //创建Word文档
            Document doc = new Document();
            Spire.Doc.Section section = doc.AddSection();
            //调用Header方法
            Header(doc);

随后,我使用最新的Spire.Doc Pack(hot fix) Version:7.9.0进行测试,页眉里档案编号和表头(head.IsHeader = true)在每页都显示了。如果您使用的是老版本,我建议您用包含更多修复的最新版本7.9.0进行测试。 如果还有问题,请提供一个简单的示例工程来帮助我重现您的问题。

此外,MS Word 文档是流文档,理论上不包含“页”和“行”布局信息, 所以没办法判断一页里的数据是否超过了17行,无法控制一页只显示17行。

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Sep 10, 2019 5:30 am

感谢您的回复,和代码,我使用了代码后发现生成的第一页有点问题,如下截图,万分感谢您。
请您再次检查小弟得代码,帮忙查出问题所在,万分感谢您。

QQ截图20190910132652.jpg



/// <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.OddHeader.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;




}
//批量获取
String pilaing = null;
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();
//调用Header方法
Header(doc, section, code);

/* #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.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", pilaing,code);


}

fhn123456
 
Posts: 3
Joined: Tue Sep 10, 2019 12:35 am

Tue Sep 10, 2019 7:49 am

您好,

我用最新的Spire.Doc V7.9.0测试了您的新代码,没发现您所说的第一页多出空行的问题。在使用7.9.0时如果不运用license,您所说的空行位置是警告信息而非空行。如果运用了license,就不会有空行跟警告信息。为了帮助我重现您的问题,能否提供一个示例工程? 请通过邮箱发送给我(betsy.jiang@e-iceblue.com)。

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.Doc

cron