| Home | Forums | Reviews | Articles | Register |
![]() |
| Thread Tools | Rate Thread |
|
|
|
| |
|
JLGWhiz
Guest
Posts: n/a
|
Not sure what you are after, but maybe this will help.
The VBA syntax for counting rows and columns is: rCnt = ActiveSheet.Rows.Count 'counts entire sheet cCnt = ActiveSheet.Columns.Count 'counts entire sheet rngRcnt = ActiveSheet.UsedRange.Rows.Count 'rows in Used Range rngCcnt = ActiveSheet.UsedRange.Columns.Count 'cols om Used Range "Jean Osorio" <(E-Mail Removed)> wrote in message news:36DCD8FC-BB65-47C5-88F1-(E-Mail Removed)... > How can I count Rows and Columns on a Excel Worksheet, this is the code, > but > columnas and rows always are equal to zero, does any body can help me > please!!!!. If I read then the data from the work sheet with out this when > de > apps find an empty cell, I get an exception. Here is the code. > > > using System; > using System.Collections.Generic; > using System.Linq; > using System.Text; > using Microsoft.Office.Interop.Excel; > using System.Data.SqlClient; > > > namespace LeerExcel > { > class Program > { > static void Main(string[] args) > { > string Path = @"c:\Libro1.xlsx"; > // initialize the Excel Application class > Microsoft.Office.Interop.Excel.ApplicationClass app = new > ApplicationClass(); > // create the workbook object by opening the excel file. > Microsoft.Office.Interop.Excel.Workbook workBook = > app.Workbooks.Open(Path, > 0, > true, > 5, > "", > "", > true, > > Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, > "\t", > false, > false, > 0, > true, > 1, > 0); > // get the active worksheet using sheet name or active sheet > Microsoft.Office.Interop.Excel.Worksheet workSheet = > (Microsoft.Office.Interop.Excel.Worksheet)workBook.ActiveSheet; > int index = 3; > object rowIndex = 4; > object colIndex2 = 2; > object colIndex3 = 3; > object colIndex4 = 4; > object colIndex5 = 5; > object colIndex6 = 6; > object colIndex7 = 7; > object colIndex8 = 8; > object colIndex9 = 9; > object colIndex10 = 10; > object colIndex11 = 11; > object colIndex12 = 12; > object colIndex13 = 13; > //object colIndex3 = 7; > Console.WriteLine("Nombre de la BD: "); > string basedeDatos = Console.ReadLine(); > Console.WriteLine("Usuario: "); > string usuariobd = Console.ReadLine(); > Console.WriteLine("Contraseña: "); > string contrasena = Console.ReadLine(); > Console.WriteLine("Ingrese Nombre de Arrendamiento: "); > string arrendamiento = Console.ReadLine(); > try > { > > > while > (((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex2]).Value2 != null) > { > //rowIndex = 2 + index; > rowIndex = index; > string RazonSocial; > string Rif; > string Contacto; > string Zona; > string Estado; > string Direcion; > string Telf; > string Serial; > object documentDate; > DateTime fechaInstalacion; > string Observaciones; > string Afiliado; > string terminalID; > > > > try > { > RazonSocial = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex2]).Value2.ToString(); > Rif = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex3]).Value2.ToString(); > Contacto = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex4]).Value2.ToString(); > Zona = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex5]).Value2.ToString(); > Estado = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex6]).Value2.ToString(); > Direcion = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex7]).Value2.ToString(); > Telf = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex8]).Value2.ToString(); > Serial = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex9]).Value2.ToString(); > documentDate = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex10]).Value2; > fechaInstalacion = > DateTime.FromOADate(Convert.ToDouble(documentDate)); > Observaciones = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex11]).Value2.ToString(); > Afiliado = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex12]).Value2.ToString(); > terminalID = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex13]).Value2.ToString(); > > > Console.WriteLine(RazonSocial); > Console.WriteLine(Rif); > Console.WriteLine(Zona); > Console.WriteLine(Estado); > Console.WriteLine(Direcion); > Console.WriteLine(Telf); > Console.WriteLine(Serial); > Console.WriteLine(fechaInstalacion); > Console.WriteLine(Observaciones); > Console.WriteLine(Afiliado); > Console.WriteLine(terminalID); > Console.WriteLine(); > } > catch (Exception e) > { > //Console.WriteLine(e.Message); > Console.WriteLine("Error en celda # {0}", index); > Console.ReadLine(); > app.Quit(); > } > > > index++; > } > > > app.Quit(); > } > > > catch (Exception ex) > { > > > Console.WriteLine(ex.Message); > Console.ReadLine(); > app.Quit(); > } > > } > > } > } > > > Thanks for your helps!!! > |
|
||
|
||||
|
Jacob Skaria
Guest
Posts: n/a
|
Once you get the worksheet object
If you are looking at total number of rows and columns WorkSheet.Rows.Count WorkSheet.Columns.Count OR Workbook.ActiveSheet.Rows.Count Workbook.ActiveSheet.Columns.Count If you are using MS 2007 (***i think...**) WorkSheet.Rows.CountLarge WorkSheet.Columns.CountLarge If you are looking to get the rows used Worksheet.Usedrange.rows.count Worksheet.Usedrange.Columns.count If this post helps click Yes --------------- Jacob Skaria "Jean Osorio" wrote: > How can I count Rows and Columns on a Excel Worksheet, this is the code, but > columnas and rows always are equal to zero, does any body can help me > please!!!!. If I read then the data from the work sheet with out this when de > apps find an empty cell, I get an exception. Here is the code. > > > using System; > using System.Collections.Generic; > using System.Linq; > using System.Text; > using Microsoft.Office.Interop.Excel; > using System.Data.SqlClient; > > > namespace LeerExcel > { > class Program > { > static void Main(string[] args) > { > string Path = @"c:\Libro1.xlsx"; > // initialize the Excel Application class > Microsoft.Office.Interop.Excel.ApplicationClass app = new > ApplicationClass(); > // create the workbook object by opening the excel file. > Microsoft.Office.Interop.Excel.Workbook workBook = > app.Workbooks.Open(Path, > 0, > true, > 5, > "", > "", > true, > > Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, > "\t", > false, > false, > 0, > true, > 1, > 0); > // get the active worksheet using sheet name or active sheet > Microsoft.Office.Interop.Excel.Worksheet workSheet = > (Microsoft.Office.Interop.Excel.Worksheet)workBook.ActiveSheet; > int index = 3; > object rowIndex = 4; > object colIndex2 = 2; > object colIndex3 = 3; > object colIndex4 = 4; > object colIndex5 = 5; > object colIndex6 = 6; > object colIndex7 = 7; > object colIndex8 = 8; > object colIndex9 = 9; > object colIndex10 = 10; > object colIndex11 = 11; > object colIndex12 = 12; > object colIndex13 = 13; > //object colIndex3 = 7; > Console.WriteLine("Nombre de la BD: "); > string basedeDatos = Console.ReadLine(); > Console.WriteLine("Usuario: "); > string usuariobd = Console.ReadLine(); > Console.WriteLine("Contraseña: "); > string contrasena = Console.ReadLine(); > Console.WriteLine("Ingrese Nombre de Arrendamiento: "); > string arrendamiento = Console.ReadLine(); > try > { > > > while > (((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex2]).Value2 != null) > { > //rowIndex = 2 + index; > rowIndex = index; > string RazonSocial; > string Rif; > string Contacto; > string Zona; > string Estado; > string Direcion; > string Telf; > string Serial; > object documentDate; > DateTime fechaInstalacion; > string Observaciones; > string Afiliado; > string terminalID; > > > > try > { > RazonSocial = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex2]).Value2.ToString(); > Rif = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex3]).Value2.ToString(); > Contacto = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex4]).Value2.ToString(); > Zona = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex5]).Value2.ToString(); > Estado = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex6]).Value2.ToString(); > Direcion = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex7]).Value2.ToString(); > Telf = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex8]).Value2.ToString(); > Serial = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex9]).Value2.ToString(); > documentDate = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex10]).Value2; > fechaInstalacion = > DateTime.FromOADate(Convert.ToDouble(documentDate)); > Observaciones = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex11]).Value2.ToString(); > Afiliado = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex12]).Value2.ToString(); > terminalID = > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > colIndex13]).Value2.ToString(); > > > Console.WriteLine(RazonSocial); > Console.WriteLine(Rif); > Console.WriteLine(Zona); > Console.WriteLine(Estado); > Console.WriteLine(Direcion); > Console.WriteLine(Telf); > Console.WriteLine(Serial); > Console.WriteLine(fechaInstalacion); > Console.WriteLine(Observaciones); > Console.WriteLine(Afiliado); > Console.WriteLine(terminalID); > Console.WriteLine(); > } > catch (Exception e) > { > //Console.WriteLine(e.Message); > Console.WriteLine("Error en celda # {0}", index); > Console.ReadLine(); > app.Quit(); > } > > > index++; > } > > > app.Quit(); > } > > > catch (Exception ex) > { > > > Console.WriteLine(ex.Message); > Console.ReadLine(); > app.Quit(); > } > > } > > } > } > > > Thanks for your helps!!! > |
|
||
|
||||
|
Jean Osorio
Guest
Posts: n/a
|
Thanks Jacob, it works perfectly!!!!
"Jacob Skaria" wrote: > Once you get the worksheet object > > If you are looking at total number of rows and columns > WorkSheet.Rows.Count > WorkSheet.Columns.Count > OR > Workbook.ActiveSheet.Rows.Count > Workbook.ActiveSheet.Columns.Count > > If you are using MS 2007 (***i think...**) > WorkSheet.Rows.CountLarge > WorkSheet.Columns.CountLarge > > > If you are looking to get the rows used > Worksheet.Usedrange.rows.count > Worksheet.Usedrange.Columns.count > > > If this post helps click Yes > --------------- > Jacob Skaria > > > "Jean Osorio" wrote: > > > How can I count Rows and Columns on a Excel Worksheet, this is the code, but > > columnas and rows always are equal to zero, does any body can help me > > please!!!!. If I read then the data from the work sheet with out this when de > > apps find an empty cell, I get an exception. Here is the code. > > > > > > using System; > > using System.Collections.Generic; > > using System.Linq; > > using System.Text; > > using Microsoft.Office.Interop.Excel; > > using System.Data.SqlClient; > > > > > > namespace LeerExcel > > { > > class Program > > { > > static void Main(string[] args) > > { > > string Path = @"c:\Libro1.xlsx"; > > // initialize the Excel Application class > > Microsoft.Office.Interop.Excel.ApplicationClass app = new > > ApplicationClass(); > > // create the workbook object by opening the excel file. > > Microsoft.Office.Interop.Excel.Workbook workBook = > > app.Workbooks.Open(Path, > > 0, > > true, > > 5, > > "", > > "", > > true, > > > > Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, > > "\t", > > false, > > false, > > 0, > > true, > > 1, > > 0); > > // get the active worksheet using sheet name or active sheet > > Microsoft.Office.Interop.Excel.Worksheet workSheet = > > (Microsoft.Office.Interop.Excel.Worksheet)workBook.ActiveSheet; > > int index = 3; > > object rowIndex = 4; > > object colIndex2 = 2; > > object colIndex3 = 3; > > object colIndex4 = 4; > > object colIndex5 = 5; > > object colIndex6 = 6; > > object colIndex7 = 7; > > object colIndex8 = 8; > > object colIndex9 = 9; > > object colIndex10 = 10; > > object colIndex11 = 11; > > object colIndex12 = 12; > > object colIndex13 = 13; > > //object colIndex3 = 7; > > Console.WriteLine("Nombre de la BD: "); > > string basedeDatos = Console.ReadLine(); > > Console.WriteLine("Usuario: "); > > string usuariobd = Console.ReadLine(); > > Console.WriteLine("Contraseña: "); > > string contrasena = Console.ReadLine(); > > Console.WriteLine("Ingrese Nombre de Arrendamiento: "); > > string arrendamiento = Console.ReadLine(); > > try > > { > > > > > > while > > (((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > > colIndex2]).Value2 != null) > > { > > //rowIndex = 2 + index; > > rowIndex = index; > > string RazonSocial; > > string Rif; > > string Contacto; > > string Zona; > > string Estado; > > string Direcion; > > string Telf; > > string Serial; > > object documentDate; > > DateTime fechaInstalacion; > > string Observaciones; > > string Afiliado; > > string terminalID; > > > > > > > > try > > { > > RazonSocial = > > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > > colIndex2]).Value2.ToString(); > > Rif = > > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > > colIndex3]).Value2.ToString(); > > Contacto = > > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > > colIndex4]).Value2.ToString(); > > Zona = > > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > > colIndex5]).Value2.ToString(); > > Estado = > > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > > colIndex6]).Value2.ToString(); > > Direcion = > > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > > colIndex7]).Value2.ToString(); > > Telf = > > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > > colIndex8]).Value2.ToString(); > > Serial = > > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > > colIndex9]).Value2.ToString(); > > documentDate = > > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > > colIndex10]).Value2; > > fechaInstalacion = > > DateTime.FromOADate(Convert.ToDouble(documentDate)); > > Observaciones = > > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > > colIndex11]).Value2.ToString(); > > Afiliado = > > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > > colIndex12]).Value2.ToString(); > > terminalID = > > ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, > > colIndex13]).Value2.ToString(); > > > > > > Console.WriteLine(RazonSocial); > > Console.WriteLine(Rif); > > Console.WriteLine(Zona); > > Console.WriteLine(Estado); > > Console.WriteLine(Direcion); > > Console.WriteLine(Telf); > > Console.WriteLine(Serial); > > Console.WriteLine(fechaInstalacion); > > Console.WriteLine(Observaciones); > > Console.WriteLine(Afiliado); > > Console.WriteLine(terminalID); > > Console.WriteLine(); > > } > > catch (Exception e) > > { > > //Console.WriteLine(e.Message); > > Console.WriteLine("Error en celda # {0}", index); > > Console.ReadLine(); > > app.Quit(); > > } > > > > > > index++; > > } > > > > > > app.Quit(); > > } > > > > > > catch (Exception ex) > > { > > > > > > Console.WriteLine(ex.Message); > > Console.ReadLine(); > > app.Quit(); > > } > > > > } > > > > } > > } > > > > > > Thanks for your helps!!! > > |
|
||
|
||||
|
|
|
| |
![]() |
| Thread Tools | |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| count rows with content in some columns | Jack Sons | Microsoft Excel Misc | 8 | 4th Aug 2009 05:48 PM |
| Count rows with criteria from two columns? | Bert Hyman | Microsoft Excel Worksheet Functions | 0 | 15th Jan 2009 10:01 PM |
| How to count rows with values in two columns | Stapes | Microsoft Excel Discussion | 1 | 26th Jul 2007 02:23 PM |
| Reduce columns and rows count? | =?Utf-8?B?bXVyYXQ=?= | Microsoft Excel Worksheet Functions | 3 | 16th Mar 2005 07:43 PM |
| Count unique rows and columns | George | Microsoft Excel Worksheet Functions | 2 | 5th Aug 2004 06:19 PM |
Powered by vBulletin®. Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2010, Crawlability, Inc. |




