Free Support Forum - aspose.com

Exception when accessing StringValue of a cell

I (intermittently) get a ThreadAbortException when trying to access StringValue of a cell by the following line:

cells[i, 1].StringValue.ToLower().Contains("back to index")

The stack trace is like this

at System.ModuleHandle._GetModuleTypeHandle()
at System.ModuleHandle.GetModuleTypeHandle()
at System.RuntimeType.RuntimeTypeCache..ctor(RuntimeType runtimeType)
at System.RuntimeType.get_Cache() at System.RuntimeType.GetTypeCodeInternal()
at Aspose.Cells.Cell.get_StringValue()

And the exception is

Thread was being aborted.

Could you help please?

Hi,

Could you give us some more details, post your template file with complete sample code here. We will check the issue soon.

Thank you.

This is part of a very large project. The method within the file that is causing the issue (and not everytime) is the following. The main purpose of this part of the project is to produce a report and this particular method applies report formating for worksheets of the report.

The culprit code is (!cells[i, 1].StringValue.ToLower().Contains("back to index")))

///


/// Sets the print borders and style.
///
/// The worksheet.
/// Index of the row.
private void SetPrintBordersAndStyle(Worksheet worksheet, int rowIndex)
{
Cells cells = worksheet.Cells;

int maxColumns = 0;

int spaceAllowance = 5;
for (int row = 0; row < rowIndex; row++)
{
int counter = 0;
for (int column = 0; column < 255; column++)
{
if (string.IsNullOrEmpty(cells[row, column].StringValue))
{
counter++;
}
else
{
counter = 0;
}

if (counter == spaceAllowance)
{
maxColumns = Math.Max(maxColumns, column - spaceAllowance + 1);
break;
}
}
}
worksheet.PageSetup.CenterHorizontally = false;
worksheet.PageSetup.CenterVertically = false;
worksheet.IsPageBreakPreview = true;
worksheet.PageSetup.PaperSize = PaperSizeType.PaperA4;
worksheet.PageSetup.BottomMargin = 0.75;
worksheet.PageSetup.TopMargin = 0.75;
worksheet.PageSetup.LeftMargin = 0.75;
worksheet.PageSetup.RightMargin = 0.75;
worksheet.PageSetup.PrintArea = string.Format("{0}:{1}", cells[0, 0].Name, cells[rowIndex-2, maxColumns-1].Name);

worksheet.HPageBreaks.Clear();
worksheet.VPageBreaks.Clear();

int zoom = 71 - (3 * maxColumns);
worksheet.PageSetup.Zoom = zoom;

int maxRows;

if (zoom > 53)
{
maxRows = 107;
}
else if (zoom > 51)
{
maxRows = 118;
}
else if (zoom > 48)
{
maxRows = 129;
}
else if (zoom > 45)
{
maxRows = 140;
}
else if (zoom > 42)
{
maxRows = 151;
}
else if (zoom > 39)
{
maxRows = 162;
}
else if (zoom > 37)
{
maxRows = 173;
}
else
{
maxRows = 184;
}

int indexCount = 1;
for (int i = 0; i < rowIndex; i++)
{
if (indexCount % maxRows == 0)
{
while ((i >= 0) && (!cells[i, 1].StringValue.ToLower().Contains("back to index")))
{
i--;
}

worksheet.HPageBreaks.Add(string.Format("{0}", cells[i + 1, 0].Name));
indexCount = 0;
}
indexCount++;
}
}

Hi,

Thanks for your code,

We will check and get back to you soon.

Thank you.

Hi,

Could you post a template file and which row index the method will use?

If you want to get max column ,please use cells.MaxDataColumn property.

If you want to know the cell is null ,please use following codes. The will impove the performace.

int index = worksheet.Cells.CheckExistence(1,1);
if (index != -1)
{
Cell cell = worksheet.Cells[index];
if (cell.Type == CellValueType.IsNull)
{
//Empty
}
}
else
{
//Empty
}

I think there is a problem of adding PageBreaks.If the cell never contains "back to index", the application will be a loop and never be ended. But if your file have strong restriction,it will not happen.

The following codes are my opition.

int indexCount = 1;
int row = 0;
int preRow = 0;
for (int i = 0; i < rowIndex; i++)
{
if (indexCount % maxRows == 0)
{
row = i;
while ((row >= preRow) && (!cells[row, 1].StringValue.ToLower().Contains("back to index")))
{
row--;
}

worksheet.HPageBreaks.Add(string.Format("{0}", cells[row + 1, 0].Name));
indexCount = 0;
preRow = i;
}
indexCount++;
}