Finding the last filled row in Excel sheet in c# -


i have requirement fine how many rows in excel sheet filled data i.e rows not empty. need using c#.

currently using following code :

excel.application excelapp = new excel.application();             string workbookpath = "c:\\scripttest\\bin\\debug\\sdownloadscripts\\excelresult.xlsx";             excel.workbook excelworkbook = excelapp.workbooks.open(workbookpath,         0, false, 5, "", "", false, excel.xlplatform.xlwindows, "",         true, false, 0, true, false, false);             excel.sheets excelsheets = excelworkbook.worksheets;             string currentsheet = "sheet1";             excel.worksheet excelworksheet = (excel.worksheet)excelsheets.get_item(currentsheet);             //long = excelworksheet.usedrange.rows.count;             int lastusedrow = excelworksheet.cells.specialcells(excel.xlcelltype.xlcelltypelastcell,  type.missing).row;             return lastusedrow;         } 

my sheet has 4 rows filled getting 65536. need 4 result i.e no of rows filled data. please suggest.

try this..

string excelconnectionstring = @"provider=microsoft.ace.oledb.12.0;data source='c:\users\xxxxxx\desktop\1-8-13-ct.xlsx';extended properties=excel 12.0;persist security info=false";     //create connection excel work book     oledbconnection excelconnection =new oledbconnection(excelconnectionstring);     //create oledbcommand fetch data excel      oledbcommand cmd = new oledbcommand("select count(*) [sheet1$]", excelconnection);     con.open();     int rows = (int)cmd.executescalar();   con.close(); 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -