How to get the previously filled cell index?


#1

Hi,

Is it possible to get the index of the cell that was bound previously for a given row index. I have a scenario where i'll know the row number and the max data columns, but the data has to be bound one by one to the cell and this happens by callin a method for each data that is to be bound. My problem is that every time before i bind i have to use a forloop and and condition check like this -

int maxdatacolumns = cells.MaxDataColumn;

for (int j = 0; j <= maxdatacolumns - 1; j++)
{
//check for the next empty cell and insert the value
if (cells[row.RowNumber + 1, j].Value == null)
{

// put value and return

}

}

But this method will cause an over head when the number of columns are more as it had to execute the entire for loop. Is there any way in aspose that helps me reduce this piece of code?

Regards,

Soumya


#2

I am not clear about your need. What do you want to do and what do you want to avoid? Could you please post more of your code here?

Do you only need to put values to blank cells?


#3

ok, i'll try and explaine. In a perticular row i need to get to know which is that cell that has been populated previously. That is, before i put the value into a cell i first need to find out as to which cell is empty. For this purpose i have a for loop that iterats through the column(cells) count. For each column(cell) it checks if the value is 'null', if null it puts the value in to that column(cell). Can i do this kind of check in aspose? Using a for loop to traverse through the cells and then checking each cell value to find an empty cell and put the new value, makes my application very slow as its a reporting application(the number columns dislpayed may vary and data will be huge)...

Code : This below method is called for every data that needs to be bound to the cell

public void Textbox(Textbox tb, string t, Row row)

{

cells = worksheet.Cells;

if (t == "")

t = " ";

if (_celltype == true)

{

//bind table headers to the cells

//for(int i=0;i<=intcol;i++)

//{

//check for the next empty cell and insert the value

if (cells[0, intcol].Value == null)

{

cells[0, intcol].PutValue(t);

setStyles(tb.Style, cells[0, intcol]);//set header style

}

//}

intcol++; // keeps the column count

}

else

{

if (row != null)

{

for (int j = 0; j <= intcol - 1; j++)

{

//check for the next empty cell and insert the value

if (cells[row.RowNumber + 1, j].Value == null)

{

//call method to format the string and bind

stringformat(t, cells[row.RowNumber + 1, j]);

worksheet.AutoFitColumn(j);

//set data column style

setStyles(tb.Style, cells[row.RowNumber + 1, j]);

return;

}

}

}

else

{

if (t != "")

{

//put footers here

int maxRows = worksheet.Cells.MaxDataRow;

//remove all tabs, newline, carriage return chars

t = Regex.Replace(t,"[\t\n\r\f\v]", "");

cells[maxRows + 1, 0].PutValue(t);

}

}

}

return;

}

Regards,

Soumya


#4

I think this problem is not caused by checking if a cell is null. It must be caused by too many calls of AutoFitColumn method. AutoFitColumn method is a time-consuming method. Please try to call it as less as possible.

In your program, you can call this method after all data are populated. Then call it for all columns you want to autofit.


#5

Thank you, I'll remove the AutoFitColumn method and check.