c# - 我想在我的 excel 工作表中找到重复的值并在 c# 中更改文本颜色
问题描述
我想在我的 excel 工作表中找到重复的值,并使用以下代码更改 c# 中的文本颜色:
Excel.Application xlApp =
new Excel.Application();
//xlApp.Visible = true;
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(DtaSource1);
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
for(int k=1;k<=rowCount;k++)
{
for (int i = 1; i <= rowCount; i++)
{
for (int j = 1; j <= colCount; j++)
{
if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j] == xlRange.Cells[k, 1])
{
xlRange.Cells[i, j].value = "00000";
xlWorksheet.SaveAs(DtaSource1);
}
}
}
}
但它不起作用,当我想保存更改时,它会抛出错误 excel 表只读!
解决方案
这是一个使用 Spire.XLS在 Excel 中突出显示重复值和唯一值的示例,也许您可以尝试一下。
//Load the Excel file
Workbook workbook = new Workbook();
workbook.LoadFromFile("Input.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Use conditional formatting to highlight duplicate values in range "A2:A10" with IndianRed color
ConditionalFormatWrapper format1 = sheet.Range["A2:A10"].ConditionalFormats.AddCondition();
format1.FormatType = ConditionalFormatType.DuplicateValues;
format1.BackColor = Color.IndianRed;
//Save the file
workbook.SaveToFile("HighlightDuplicates.xlsx", ExcelVersion.Version2013);
推荐阅读
- php - 为什么没有 echo $e->getMessage(); 打印错误信息
- machine-learning - 评估 AutoKeras 模型给出不同的结果,然后与纯 Keras 评估相同的模型(h5 文件)
- bash - Bash:由“文本在找到匹配的引号之前结束”引起的“无法解析参数”
- html - 具有高度的 flexbox:100% 不适用于 chrome 72+
- python - 关于 tf.layers .dense 的问题
- c# - Windows 服务对运行 .exe 文件的限制
- java - gle.firebase.database.core.utilities.encoding.CustomClassMapper.serialize 错误是什么?
- nginx - Geth websocket over nginx 反向代理
- typescript - 如何更新firestore中的文档字段?
- symfony - 根据第三方捆绑包中的配置使用服务进入服务