打印 excel sheet (EPPlus) 时如何获取最后一行的行号

How to get the row number of the last row when you are printing an excel sheet (EPPlus)

我想通过 C# EPPlus 中的可打印 Excel 工作表添加一张图片(显示 "DRAFT")。

我想知道在打印工作表时是否有办法找到工作表每一页的最后可见行。我不能假装每页的行数总是固定的,因为它取决于单元格的内容。

这是我当前的代码,它使用每页固定行数 (30) 来插入图像。这导致每个可打印页面大约有一个图像,除了在每个新页面中图像不在同一位置。 (略有偏差,具体取决于单元格的内容。)

public void InsertDraftImage(ExcelWorksheet worksheet, FileInfo draft_image)
{

    int maxRowNumber = worksheet.Dimension.End.Row;
    int rowByPage = 30;
    int numberOfPage = (maxRowNumber / rowByPage) + 1;


    ExcelPicture picture = null;
    for(int i = 0; i < numberOfPage; i++)
    {
        if(draft_image != null)
        {
            picture = worksheet.Drawings.AddPicture(i.ToString(), draft_image);
            picture.SetSize(609, 545); //original image size
            picture.SetPosition(i * rowByPage, 0, 1, 0); 
            picture.EditAs = eEditAs.Absolute;
        }
    }

在尝试与同事从 EPPlus 中实现 'ExcelHeaderFooter.cs' 中缺失的代码后,没有成功,我们最终按照 Ernie 的建议完成了!!

我的最终代码是在 C# 中使用 EPPlus 生成的可打印 excel 文件的每一页中插入一张图片。

这是通过在页脚中添加图片并将布尔 ScaleWithDoc 设置为 false(默认 = true)来完成的。

public void InsertDraftImage(ExcelWorksheet worksheet, FileInfo draft_image)
{
    ExcelHeaderFooterText footer = worksheet.HeaderFooter.OddFooter; //all page have same footer
    footer.InsertPicture(draft_image, PictureAlignment.Centered);
}

在我的方法中添加此代码以创建 ExcelWorksheet(所有其他 excel 样式、填充、设置)。

XmlAttribute temp = worksheet.WorksheetXml.CreateAttribute("scaleWithDoc");
temp.Value = "0";
worksheet.WorksheetXml.GetElementsByTagName("headerFooter")[0].Attributes.Append(temp);

package.Save();