xls created through C#, but does not add values

Asked

Viewed 57 times

0

I have a method that generates excel(. xls), but I have a problem: excel is being generated perfectly, but when I try to add up the values of cells nothing happens, it only adds up if I open cell by cell(I don’t need to change any value).

In the image below you have an example, all the values are aligned to the left but when I click on the cell to edit they automatically align to the right and it is possible to add them up.

Os que possuem traço vermelho é possível somar

Those with a red dash can be summed because I already clicked on the cell as if I were editing its value. And the other values are not possible to add up because I have not clicked to edit them at any time.

The code that shapes the cells is this:

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 misexplained things, please ask me.

Remarks:

  1. I am using Microsoft.Office.Interop.Excel and some values are zeroed but this is not the cause of not adding, I have checked this.
  2. It is worth noting that this only occurs with double.
  • Perhaps Excel is not recognizing these values initially as numerical and interpreting them as text. Where is the relevant rest of the code?

  • Tried to reverse the separators? "#,##0.00

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.