color change
using System;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelRedToBlack
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter full path to the Excel file:");
string filePath = Console.ReadLine();
if (string.IsNullOrWhiteSpace(filePath))
{
Console.WriteLine("Invalid file path.");
return;
}
Excel.Application excelApp = null;
Excel.Workbook workbook = null;
try
{
excelApp = new Excel.Application();
workbook = excelApp.Workbooks.Open(filePath);
foreach (Excel.Worksheet sheet in workbook.Sheets)
{
Console.WriteLine($"Processing Sheet: {sheet.Name}");
// Check and clear red tab color
if (sheet.Tab.Color != null && (int)sheet.Tab.Color == 255)
{
sheet.Tab.Color = Type.Missing; // No color
}
Excel.Range usedRange = sheet.UsedRange;
// Convert red font to black
foreach (Excel.Range cell in usedRange)
{
if (cell.Font.Color != null && (int)(cell.Font.Color) == 255)
{
cell.Font.Color = 0; // Black
}
// Convert red borders to black
foreach (Excel.XlBordersIndex borderIndex in Enum.GetValues(typeof(Excel.XlBordersIndex)))
{
var border = cell.Borders[borderIndex];
if (border.Color != null && (int)border.Color == 255)
{
border.Color = 0; // Black
}
}
}
Marshal.ReleaseComObject(sheet);
}
workbook.Save();
workbook.Close(false);
Console.WriteLine("Completed: Red converted to black and red tab colors removed.");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
finally
{
if (workbook != null) Marshal.ReleaseComObject(workbook);
if (excelApp != null)
{
excelApp.Quit();
Marshal.ReleaseComObject(excelApp);
}
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
Comments
Post a Comment