繁体   English   中英

如何使用EPPlus向单元格区域添加注释

[英]How to add comments to range of cells using EPPlus

我正在生成一个Excel文件供用户使用。 我正在填充值,其中一些会有评论。 将值添加到一系列单元格可以完美地工作。 尝试一次打开Excel文件时,一次向多个单元格添加注释会给我一个错误。 Visual Studio的行为就像一切都会好起来的,但是当我打开文件时,它说:

“我们在[文件名]中的某些内容上发现了问题。您是否要我们尝试尽可能地恢复?如果您信任此工作簿的来源,请单击“是”。 单击“是”,我收到一条消息,指出“ Excel能够通过修复或删除不可读的内容来打开文件。
删除的记录:/xl/comments1.xml部分的注释(注释)“

以下是修复的日志文件: <?xml version="1.0" encoding="UTF-8" standalone="true"?>-<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> <logFileName>error272000_02.xml</logFileName> <summary>Errors were detected in file 'C:\\Users\\aletreb\\Downloads\\Import_20190826_7E'sSales (11).xlsx'</summary> -<removedRecords> <removedRecord>Removed Records: Comments from /xl/comments1.xml part (Comments)</removedRecord> </removedRecords> </recoveryLog>

因此,当文件打开时,只有范围中的第一个单元格仍具有注释。

    var worksheet = excel.Workbook.Worksheets["Sheet1"];
    var customerName = bulkUpload.CustomerId != 0 ? _customerModel.GetCustomerByID(bulkUpload.CustomerId).CustomerName : "No Customer";
    var finalRow = bulkUpload.UnitCount != null && bulkUpload.UnitCount > 0 ? (int)bulkUpload.UnitCount + 1 : 2;

    var headerRow = new List<string> {
        "CustomerID", // A1
        "VIN", // B1
        "Unit Number", // C1
        "Year", // D1
        "Make", // E1
        "Model", // F1
        "Contact Name", // G1
        "Phone Number", // H1
        "Fax Number" // I1
    };

    var customerIdrange = worksheet.SelectedRange[2, 1, finalRow, 1];
    customerIdrange.Value = bulkUpload.CustomerId;
    customerIdrange.AddComment(customerName, "author"); // Problem here        

`

我试过在单元格的Excel Ranges上使用AddComments()方法,使用Cells [“ A2:A5”],SelectedRange [“ A2:A5”],Cells [2,1,5,1]和SelectedRange [ 2,1,5,1]。 在Visual Studio中,一切似乎都很好,但是当我打开Excel文件时,我仍然收到相同的错误。

我知道我可以使用AddComment()向单个单元格添加注释,但是我真的不想遍历每个单元格一次做一个。 该文档使我听起来应该也可以将其添加到范围中,所以我宁愿这样做。

我会避免使用[int FromRow, int FromCol, int ToRow, int ToCol]索引器,因为AddComment逻辑似乎未正确支持。 但是for将正常工作:

//var customerIdrange = worksheet.SelectedRange[2, 1, finalRow, 1];
for (var i = 2; i <= finalRow; i++)
{
    var customerIdrange = worksheet.SelectedRange[i, 1];
    customerIdrange.Value = bulkUpload.CustomerId;
    customerIdrange.AddComment(customerName, "author"); // Problem here 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM