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.

Wed Sep 11, 2019 12:16 pm

感谢技术大哥,提供代码,万分感谢。

spire.doc 数据填充完毕后,最后一页如何用空行填充,判断最后一页,如果只有2条数据,就添加15个空行来填充,如下截图:

QQ截图20190911200952.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.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);




}

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

Thu Sep 12, 2019 6:14 am

您好,

感谢咨询。
建议您通过float pageHeight = section.PageSetup.ClientHeight获取页面内容的高度,然后通过 double rowHeight = pageHeight / 17 获取每行的高度, 计算 tableHeight/rowHeight % 17的数值判断最后一个页面有多少行数据,假如tableHeight/rowHeight % 17 = 2, 那么就可以了解到最后一页包含2行数据,还需添加15个空白行。像下面代码所示循环添加15行就可以了
Code: Select all
  for (int i = 0; i < 15; i++)
            {
                table.AddRow(true);
            }


Sincerely,
Betsy
E-iceblue support team
User avatar

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

Mon Jun 21, 2021 1:47 am

Jiang,你好。
我用office 365 免费版spire.doc,PageSetup.ClientHeight属性返回的值好像不正确。
我使用了如下代码:
Code: Select all
         var table = doc.LastSection.AddTable(true);
         float clientHeight = doc.LastSection.PageSetup.ClientHeight;
         float clientWidth = doc.LastSection.PageSetup.ClientWidth;
         table.ResetCells(31, 10);
         float averageRowHeight = table.Document.LastSection.PageSetup.ClientHeight / 33;
         float averageColumnsWidth = table.Document.LastSection.PageSetup.ClientWidth / 10;
         table.Rows[0].Height = averageRowHeight * 3;
         for (int i = 1; i < 31; i++)
         {
            table.Rows[i].Height = averageRowHeight;
         }
         float[] columnsWidth = new float[10];

         for (int i = 0; i < 10; i++)
         {
            columnsWidth[i] = averageColumnsWidth;
         }
         table.ColumnWidth = columnsWidth;

这样运行下来,表格宽度可以充满整个client区域,但是高度上,每一页只能绘制25个表格,而不是31个。
请教高手,这是怎么回事,我什么地方用的不对吗?

cairunbin
 
Posts: 3
Joined: Fri Nov 22, 2019 8:37 am

Mon Jun 21, 2021 6:58 am

经过试验,我感觉ClientHeight里面可能是没有扣除页眉页脚占用的空间。
请问,如何计算页面和页脚的高度?

cairunbin
 
Posts: 3
Joined: Fri Nov 22, 2019 8:37 am

Mon Jun 21, 2021 12:25 pm

您好,

感谢来信!
我这边使用微软Word创建了一个空白的模板文件,是可以添加31行到页面,并且充满整个clientsize。请注意基于微软Word文档规范,添加表格后文档默认是会添加一个空白段落的,因此必须要减去空白段落的高度。请查看我的代码中示例是减去了20磅值的,我也附上我的输入输出文档供您更好的参考。
Code: Select all
            Document doc = new Document();
            doc.LoadFromFile("sample.docx");
            doc.LastSection.Paragraphs.Clear();
           
            var table = doc.LastSection.AddTable(true);
           
            SizeF size = doc.LastSection.PageSetup.PageSize;
       
            table.ResetCells(31, 10);
         
            float averageRowHeight = (table.Document.LastSection.PageSetup.ClientHeight-20) / 33;
            float averageColumnsWidth = table.Document.LastSection.PageSetup.ClientWidth / 10;

            table.Rows[0].Height = averageRowHeight * 3;   
           
            for (int i = 1; i < 31; i++)
            {
                table.Rows[i].HeightType = TableRowHeightType.Exactly;
                table.Rows[i].Height = averageRowHeight;
            }

            float[] columnsWidth = new float[10];

            for (int i = 0; i < 10; i++)
            {
                columnsWidth[i] = averageColumnsWidth;
            }
            table.ColumnWidth = columnsWidth;         
            doc.SaveToFile("output.docx", FileFormat.Docx);

ClientHeight是不包含页眉页脚距离的,您不需要进行扣除。不过您也可以使用下面的代码获取值。
Code: Select all
float headerDistance = section.PageSetup.HeaderDistance;
float footerDistance = section.PageSetup.FooterDistance;


Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Thu Jun 24, 2021 2:57 am

您好:
感谢您非常及时的回复。
你的测试文档,里面没有包含页眉页脚,您可以加上页眉页脚试一下。
另外,HeaderDistance成员好像是页眉顶部到纸张边界的值,而非页眉自身高度。

cairunbin
 
Posts: 3
Joined: Fri Nov 22, 2019 8:37 am

Thu Jun 24, 2021 7:42 am

您好,

感谢您的反馈。
是的,HeaderDistance是页眉顶部距离纸张边界的值。微软Word是流式布局文档,抱歉我们产品没有提供直接的方法获取页眉高度,您需要通过计算页眉的内容高度进行获取,这个过程是很复杂的。建议您可以将HeaderDistance设置为0,以使得页面能容纳更多的表格行。如果您有其他问题,请随时联系我们。

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc