Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I am having a excel sheet with highlighted rows and columns . Need to remove those highlighted color rows and columns and import into datatable in c#

What I have tried:

I am having a excel sheet with highlighted rows and columns . Need to remove those highlighted color rows and columns and import into datatable in c#
Posted
Updated 19-Feb-18 6:04am
Comments
CHill60 19-Feb-18 8:25am    
The "What I have tried" section is for the code you have tried. At this point in time we have no idea even how you are manipulating the excel workbook.
ZurdoDev 19-Feb-18 9:39am    
And what did you want from us?

1 solution

Hi,

I think your desired code will look like to the below one, try to test the code in console application by creating a Test.xlsx excel file with some used cells (like make the first rows dirty with inserting letters or numbers into them) then make the back color black. Just do not forget to reference Microsoft.Office.Interop.Excel in your project. Keep in your mind you need a better exception handling and also SOLID implementation the below code is only a small indication of your final work.

static void Main(string[] args)
		{
			Excel.Application xlApp;
			Excel.Workbook xlWorkBook;
			Excel.Worksheet xlWorkSheet;
			xlApp = new Excel.Application();
			try
			{
				xlWorkBook = xlApp.Workbooks.Open(@"C:\Test\Test.xlsx");
				xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[1];
				var rows = xlWorkSheet.UsedRange;
				foreach (Excel.Range row in rows)
				{
					double color = row.Interior.Color;
					if (color == 0) // 0 indicates black color
						row.Delete();
				}

				xlWorkBook.Save();
				xlWorkBook.Close();
			}
			catch (Exception)
			{
				throw;
			}
			finally
			{
				xlApp.Quit();
				Marshal.ReleaseComObject(xlApp);
			}
		}


Cheers,
--AH.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900