简体   繁体   English

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

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

I'm generating an Excel file for the user to do stuff with. 我正在生成一个Excel文件供用户使用。 I'm populating values, and some of them will have comments. 我正在填充值,其中一些会有评论。 Adding the values to a range of cells works perfectly. 将值添加到一系列单元格可以完美地工作。 Adding comments to more than one cell at a time gives me an error when I try opening the Excel file. 尝试一次打开Excel文件时,一次向多个单元格添加注释会给我一个错误。 Visual Studio acts like everything'll be fine, but when I open the file it says: Visual Studio的行为就像一切都会好起来的,但是当我打开文件时,它说:

"We found a problem with some content in [filename]. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes." “我们在[文件名]中的某些内容上发现了问题。您是否要我们尝试尽可能地恢复?如果您信任此工作簿的来源,请单击“是”。 I click yes, I get a message that says "Excel was able to open the file by repairing or removing the unreadable content. 单击“是”,我收到一条消息,指出“ Excel能够通过修复或删除不可读的内容来打开文件。
Removed Records: Comments from /xl/comments1.xml part (Comments)" 删除的记录:/xl/comments1.xml部分的注释(注释)“

Here's the log file listing repairs: <?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> 以下是修复的日志文件: <?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>

So when the file opens, only the very first cell in the range still has the comment. 因此,当文件打开时,只有范围中的第一个单元格仍具有注释。

    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        

` `

I've tried using the AddComments() method on Excel Ranges of cells, selected using Cells["A2:A5"], SelectedRange["A2:A5"], Cells[2,1,5,1], and SelectedRange[2,1,5,1]. 我试过在单元格的Excel Ranges上使用AddComments()方法,使用Cells [“ A2:A5”],SelectedRange [“ A2:A5”],Cells [2,1,5,1]和SelectedRange [ 2,1,5,1]。 Everything seems fine in Visual Studio, but I keep getting the same error when I open the Excel file. 在Visual Studio中,一切似乎都很好,但是当我打开Excel文件时,我仍然收到相同的错误。

I know I can use AddComment() to add comments to individual cells, but I really don't want to have to loop through each cell and do them one at a time. 我知道我可以使用AddComment()向单个单元格添加注释,但是我真的不想遍历每个单元格一次做一个。 The documentation is making it sound like I should be able to add it to a range as well, so I'd much rather do that. 该文档使我听起来应该也可以将其添加到范围中,所以我宁愿这样做。

I would avoiding using the [int FromRow, int FromCol, int ToRow, int ToCol] indexer since the AddComment logic does not seem to properly support. 我会避免使用[int FromRow, int FromCol, int ToRow, int ToCol]索引器,因为AddComment逻辑似乎未正确支持。 But a for will works properly: 但是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