How to check a Cell contains formula or not in Excel through oledb reader ?
System.Data.OleDb.OleDbConnection conn2 = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source = " + strFileName + "; Extended Properties = \"Excel 8.0;HDR=NO;IMEX=1\";");
conn2.Open();
string strQuery2 = "SELECT * FROM [" + Table + "]";
System.Data.OleDb.OleDbDataAdapter adapter2 = new System.Data.OleDb.OleDbDataAdapter(strQuery2, conn2);
System.Data.DataTable DT2 = new System.Data.DataTable();
adapter2.Fill(DT2);
You can use the OpenXML SDK to read Xlsx files.
To do this you need to add a reference to the OpenXML library which can be done via the nuget package (you'll also need a reference to WindowsBase). You then need to load the spreadsheet, find the sheet you're interested in and iterate the cells.
Each
Cell
has aCellFormula
property which will be non-null if there is a formula in that cell.As an example, the following code will iterate each cell and output a line for any cell that has a formula. it will return
true
if any cell has a formula in it; otherwise it will returnfalse
:This can be called with the name of the file and the name of the sheet you're interested in although it would be trivial to update the code to iterate all sheets. An example call:
An example output from the above:
If you're only interested if there are any cells in the sheet that have a formula you can simplify the above by using the
Any
extension method: