I have a method that generates excel (.xls), but I have a problem: excel is being generated perfectly, but when I try to sum the values of the cells nothing happens, it only adds if I open cell by cell (I do not need change any value).
In the image below you have an example, all values are aligned to the left but when I click on the cell to edit they automatically align to the right and it becomes possible to add them.
ThosethathaveareddashcanbeaddedbecauseIalreadyclickedonthecellasifitweretoedititsvalue.AndtheothervaluesarenotpossibletoaddbecauseIdidnotclicktoeditthemonce.
Thecodethatformatsthecellsisthis:
if(gradeColuna!=null)switch(gradeColuna.type){case"string": excelRange.NumberFormat = "@"; break;
case "date": excelRange.NumberFormat = "dd/mm/aaaa"; break;
case "datetime": excelRange.NumberFormat = "dd/mm/aaaa hh:mm;@"; break;
case "double": excelRange.NumberFormat = "#.##0,00#"; break;
}
else
excelRange.NumberFormat = "@";
And what assigns the values is this:
foreach (DataRow dr in dados.Rows)
{
for (int i = 1; i < dados.Columns.Count; i++)
{
xlWorkSheet.Cells[linha, i] = dr[i].ToString()
.Replace("01/01/1900 00:00:00", "")
.Replace("00:00:00", "")
.Replace("01/01/1900", "")
.Trim()
.Replace("\n", Environment.NewLine);
if (dr[i].ToString().Contains("\n"))
xlWorkSheet.Range[xlWorkSheet.Cells[linha, i], xlWorkSheet.Cells[linha, i]].WrapText = true;
if (dr[i].ToString().Length >= 100)
xlWorkSheet.Columns[i].ColumnWidth = 100;
decimal valorTemp = 0M;
if (decimal.TryParse(dr[i].ToString(), out valorTemp))
{
var teste = xlWorkSheet.Cells[linha, i];
if (dados.Columns[i].DataType.Name == "Decimal" || dados.Columns[i].DataType.Name == "Double")
xlWorkSheet.Cells[linha, i].Value = valorTemp.ToString("N2");
}
}
linha++;
}
xlWorkSheet.Columns.AutoFit();
xlWorkSheet.Rows.AutoFit();
If you have any doubts or anything badly explained, please ask me.
Comments: