Row and column count and dynamic access of data values
In connection to the code in this link under the topic accessing a cell or range using IMigrantRange, 2 variables rowCount and colCount have to be created. They must obviously be integers. But my question is that, is there a method or a property that can be used to read data values from every row and column in an excel file that is being retrieved from the database dyamically?
IWorksheet sheet = workbook.Worksheets[w];
IMigrantRange migrantRange = sheet.MigrantRange;
IRange[] rowCount = sheet.Rows;
IRange[] colCount = sheet.Columns;
foreach (IRange Rows in rowCount)
{
foreach(IRange Columns in colCount)
{
//Code to read data in row and column and manipulate them accordingly.
}
}
What I need to be able to do is read every row and column grid cell and then set appropriate data type to that grid. Another problem with the above code you might notice is that rowCount and colCount are not integers and hence cannot be used in a for loop.
Setting the data types of individual grid cells is not effective because the data values are being loaded dynamically and need to be parsed at runtime.
Any help and assistance would be greatly appreciated.
Regards,
Ajay Shastry
|
IWorksheet sheet = workbook.Worksheets[0];
IMigrantRange migrantRange = sheet.MigrantRange;
int rowCount = sheet.UsedRange.LastRow;
int colCount = sheet.UsedRange.LastColumn;
for (int i = 1; i <= rowCount; i++)
{
for (int j = 1; j <= colCount; j++)
{
migrantRange.ResetRowColumn(i,j);
string value = migrantRange.DisplayText;
}
} |
To know more about the used range, please refer the following link.
UG documentation Link: https://help.syncfusion.com/file-formats/xlsio/worksheet-cells-manipulation#accessing-used-range-of-a-worksheet
|
//Set dynamic data to Value2 property
sheet.Range["A1"].Value2 = value; |
UG Documentation Link: https://help.syncfusion.com/cr/file-formats/Syncfusion.XlsIO.IRange.html#Syncfusion_XlsIO_IRange_Value2
Please let us know if you have any concern.
for (int column = 1; column <= colCount; column++)
if(sheet.UsedRange[rowCount:colCount].NumberFormat = "Text") // If the data in the current grid is a string, set it to a string format.
}
}
|
foreach (IRange cell in worksheet.UsedRange)
{
if(cell.HasString)
{
cell.NumberFormat = "@";
}
else if(cell.HasNumber)
{
cell.NumberFormat = "0.00";
}
else if(cell.HasDateTime)
{
ell.NumberFormat = "m/d/yyyy";
}
} |
{
{
cell.NumberFormat = "0.00";
}
}
{
{
migrantRange.NumberFormat = "0.00";
}
}
}
|
IMigrantRange migrantRange = worksheet.MigrantRange;
int rowCount = 10;
int colIndex = 5;
for (int i = 1; i <= rowCount; i++)
{
migrantRange.ResetRowColumn(i, colIndex);
migrantRange.NumberFormat = "0.00";
} |
Attachment: Sample_20d3d3b2.zip
Hi Syncfusion Team,
I have a query. I read that Excel. owing to performance issues truncates leading and trailing zeroes if a particular cell is formatted as a number. So I wanted to ask you guys if Syncfusion offers any method to override this default Excel behavior of truncating trailing and leading zeroes.
I will illustrate this with an example.
I have a value like 914.000000. I am using logic to convert this value to a number format with the logic that has been discussed in this ticket. But the value being entered in the downloaded excel sheet is 914.00 or just 914. I understand that this is the default excel behavior of truncating trailing zeroes. So my question is, is there any way that I can retain the value 914.000000 and still have it formatted as a number?
Thanks in advance for your help.
Kind regards,
Ajay Shastry
Hi Sridhar,
Thank you for your help all along the way. I appreciate your quick and sensible responses.
Regards,
Ajay
- 23 Replies
- 2 Participants
-
AS Ajay Shastry
- Mar 22, 2017 04:17 PM UTC
- Jul 13, 2017 04:40 AM UTC