Hello,
I need to iterate worksheet columns collection in code but i need to know the last used column in worksheet.
Could you advise me how to find last used column?
my code example:
using Infragistics.Documents.Excel;
...
for (int i = 0; i < lastUsedColumn; i++)
{
}
before I used Worksheets[0].Columns collection but it skipped some columns in worksheet.
foreach(var column in workbook.Worksheets[0].Columns)
Thank you
Libor
I see. Maybe this would be a better way to accomplish this. You could create an object called something like CellsInColumnInfo which would keep track of the information about the cells in the column as you were walking over data. Then you could loop over all created rows and their created cells, which would be the fastest way to loop over all the data. When you come across cells with data, add information about that cell to the CellsInColumnInfo for its column. Then after walking over all data, do something with your finalized CellsInColumnInfo instances, like this:
(Sorry about the code formatting)
Dictionary<int, CellsInColumnInfo> columnProperties = new Dictionary<int, CellsInColumnInfo>(); foreach (WorksheetRow row in workbook.Worksheets[0].Rows) { foreach (WorksheetCell cell in row.Cells) { object cellValue = cell.Value; if (cellValue != null) { // scan all cell values to find the value properties e.g. biggest value in the column, is value numeric etc. CellsInColumnInfo columnProps; if (columnProperties.TryGetValue(cell.ColumnIndex, out columnProps) == false) { columnProps = new CellsInColumnInfo(); columnProperties.Add(cell.ColumnIndex, columnProps); } // Store some information about the cells in the column columnProps.AddInformationAboutCell(cell, cellValue); } } } foreach (KeyValuePair<int, CellsInColumnInfo> pair in columnProperties) { int columnIndex = pair.Key; CellsInColumnInfo columnProps = pair.Value; //column type decision code // on the basis of column properties determin appropriate sql data type // ... }
Dictionary<int, CellsInColumnInfo> columnProperties = new Dictionary<int, CellsInColumnInfo>();
foreach (WorksheetRow row in workbook.Worksheets[0].Rows)
foreach (WorksheetCell cell in row.Cells)
object cellValue = cell.Value;
if (cellValue != null)
// scan all cell values to find the value properties e.g. biggest value in the column, is value numeric etc.
CellsInColumnInfo columnProps;
if (columnProperties.TryGetValue(cell.ColumnIndex, out columnProps) == false)
columnProps = new CellsInColumnInfo();
columnProperties.Add(cell.ColumnIndex, columnProps);
// Store some information about the cells in the column
columnProps.AddInformationAboutCell(cell, cellValue);
foreach (KeyValuePair<int, CellsInColumnInfo> pair in columnProperties)
int columnIndex = pair.Key;
CellsInColumnInfo columnProps = pair.Value;
//column type decision code
// on the basis of column properties determin appropriate sql data type
// ...
thanks for the reply,I am creating an application that according to the content of excel file creates SQL script for generating the tables on the SQL server.
result example:
CREATE TABLE <table_name> (ID int IDENTITY (1,1) Primary key,[ProductID] bigint,[ProductName] nvarchar(28),[Column_03] datetime,[UnitPrice] nvarchar(5),[UnitsInStock] decimal(8,3),[UnitsOnOrder] tinyint
...)
The colum names and data types in sql script accurately reflect the contents of individual fields of Excel.
Application uses Infragistics dll.
It opens the excel file and have to scan through all values in the colum to determin correct sql server data type.
Example of code:
numberOfColumns = workbook.CurrentFormat == WorkbookFormat.Excel2007 ? Workbook.MaxExcel2007ColumnCount : Workbook.MaxExcelColumnCount;
for (int i = 0; i < numberOfColumns; i++) // here I need last used column in the file {
WorksheetColumn column = workbook.Worksheets[0].Columns[i];
foreach (var row in workbook.Worksheets[0].Rows) {
if (row.GetCellValue(column.Index) != null) { columnValue = row.GetCellValue(column.Index).ToString().Trim(); }
...}Mentioned code scans all columns (16384 columns) in excel file but I need to scan only used columns (e.g. tens).
And my question is: How to determin the last used column in the excel worksheet?
The Columns collection is a lazily loaded collection. WorksheetColumn instances are only created when they are requested. When loading a workbook from a file, the Excel library will internally request the columns so they can be populated with width and formatting information (if they are not default values). When you iterate over the Columns collection with a foreach loop, you are just iterating the already created columns. When you iterate over the collection with a for loop and therefore request the columns in order, you are forcing them to get created. What you could do is use a foreach loop to determine the last created column, then use a for loop to iterate from column 0 to that column. However, you should know that the columns do not hold any cell data. That is owned by the rows. So it is possible that no columns have been created even though the worksheet has data. What are you trying to do by iterating the columns?