Problem with cells formating


#1

for some reason following code does not change the color of the font for negative/positive numbers.
cells contain the data in the format like "123,456"
is there something wrong with the way I do it?
is there a better way to do it?

in general, what format do the cells have, when the worksheet is just created? Text?

private void download(string type)
{
TopHolderService topHolderService = new TopHolderService();
DataView dv = topHolderService.GetTopHolders(TextBox1.Text, int.Parse(DropDownList1.SelectedValue),“I”,0);
if (dv == null) return;
Excel excel = new Excel();
Worksheet sheet = excel.Worksheets[0];
int count;
count=sheet.Cells.ImportDataTable(dv.Table,true,0,0,65530,256);
if (count <=0)
return;

int i;

int styleIndex = excel.Styles.Add();
int styleIndex2 = excel.Styles.Add();
Aspose.Excel.Style stylePositive ,styleNegative;
stylePositive = excel.Styles[styleIndex];
styleNegative= excel.Styles[styleIndex2];
stylePositive.Font.Color = Color.Green;
styleNegative.Font.Color = Color.Red;
for(i=1;i<count;i++)
{
try {
if (double.Parse(sheet.Cells[i,6].ToString(),System.Globalization.NumberStyles.AllowThousands)>0)
sheet.Cells[i,6].Style = stylePositive;
else if(double.Parse(sheet.Cells[i,6].ToString(),System.Globalization.NumberStyles.AllowThousands)<0)
sheet.Cells[i,6].Style = styleNegative;
}
catch (Exception e)
{
// if something wrong, don’t format the number
}
}
excel.Save(“TopHolders.xls”,SaveType.OpenInExcel,FileFormatType.ExcelXP,this.Response);
}


#2

Hi,

This problem is caused by that Cell.ToString() always returns “Aspose.Excel.Cell”.

So please change your code to:

Cell cell = sheet.Cells[i,6];
switch(cell.Type)
{
case CellValueType.IsNumeric:
if(cell.DoubleValue > 0)
cell.Style = stylePostive;
else if(cell.DoubleValue < 0)
cell.Style = styleNegative;
break;
}