我有一个要求,即需要根据多个规则擦除excel文档;其中之一是以编程方式删除给定列中具有红色文本和删除线字体样式的所有单元格。使用Microsoft.Office.Interop.Excel库在C#中访问excel文档后,有没有办法检测单元格文本的颜色和应用的字体样式,如果单元格的文本为红色并带有删除线样式,则删除该单元格? / p>
public static void GetExcelFile() {
//Establish COM Objects
Excel.Application excelApp = new Excel.Application();
Excel.Workbook excelWorkBook = excelApp.Workbooks.Open(@"\MyExcelBook.xlsx");
Excel._Worksheet excelWorkSheet = excelWorkBook.Sheet[1];
Excel.Range excelRange = excelWorkSheet.UsedRange;
//Retrieve Column E
object[] columnE = excelWorkSheet.Range["E"].Values2;
//Scrub Column E
//Remove expired codes (signified by red font and strikethrough)
//Remove Whitespace
string trColumnE = columnE.ToString();
string[] strColumnE = trColumnE.Replace(" ", "").Split(',');
答案 0 :(得分:1)
在检索之前,您可以检查如下:
if (excelWorkSheet.Range["E"].Font.Strikethrough == true && excelWorkSheet.Range["E"].Interior.Color = 255)
excelWorkSheet.Range["E"].Value == "";