Last post Feb 23, 2010 01:35 PM by hans_v
Feb 22, 2010 04:00 PM|priteshmp|LINK
Hi Basically I'm opeing an excel sheet, selecting the 2nd workbook and trying to retrive only the columns, rows that have been populated. However when I do a column count I find an extra 20 columns with a null value.
I checked the wroksheet name and it is accessing the correct one.
does anyone know why this might be happening?
Feb 23, 2010 02:41 AMemail@example.com|LINK
Please try the below code which will be display only columns which have values
excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
object missing = Type.Missing;
excel.Sheets xlSheets = null;
excel.Worksheet xlSheet = null;
excel.Workbook xlWorkbook = null;
xlWorkbook = excelApp.Workbooks.Open("C:\\Book1.xlsx", missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing);
xlSheets = (excel.Sheets)xlWorkbook.Sheets;
xlSheet = (excel.Worksheet)xlSheets;
xlRange = xlSheet.UsedRange;
if (xlRange != null)
int nRows = xlRange.Rows.Count;
int nCols = xlRange.Columns.Count;
for (int iRow = 1; iRow <= nRows; iRow++)
for (int iCount = 1; iCount <= nCols; iCount++)
xlRange = (Microsoft.Office.Interop.Excel.Range)xlSheet.Cells[iRow, iCount];
Feb 23, 2010 01:35 PM|hans_v|LINK
You shouldn't use Excel interop at all:
"Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit
unstable behavior and/or deadlock when Office is run in this environment."