How to format data extracted from datagridview to excel

1
    private void button1_Click_1(object sender, EventArgs e)
    {
        Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
        Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
        Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
        app.Visible = true;
        worksheet = workbook.Sheets["Plan1"];
        worksheet = workbook.ActiveSheet;
        for (int i = 1; i < dGVPosicaoEstoque.Columns.Count + 1; i++)
        {
            worksheet.Cells[1, i] = dGVPosicaoEstoque.Columns[i - 1].HeaderText;
        }
        for (int i = 0; i < dGVPosicaoEstoque.Rows.Count - 1; i++)
        {
            for (int j = 0; j < dGVPosicaoEstoque.Columns.Count; j++)
            {
                if (dGVPosicaoEstoque.Rows[i].Cells[j].Value != null)
                {
                    worksheet.Cells[i + 2, j + 1] = dGVPosicaoEstoque.Rows[i].Cells[j].Value.ToString();
                }
                else
                {
                    worksheet.Cells[i + 2, j + 1] = "";
                }
            }
        }
    }

I need to format the data being extracted to excel because it is not differentiating (.) point from (,) comma. Example: The value extracted is this way (109.390.759) and the correct value would be (109,390,759).

    
asked by anonymous 26.10.2017 / 20:07

1 answer

1

So I understand, you want to convert a value from the dGVPosicaoEstoque list in the format 109,390,759.

Assuming this is a double and is registered as 109390.759 . In this case you can use the String.Format method (documentation here ) to format it the way you want before inserting it into the worksheet.

See an example:

double valor = 109390.759;

string valorFormatado = string.Format("{0:0,0.000}", valor);

Console.WriteLine(valorFormatado);    // valor impresso: 109.390,759
    
28.10.2017 / 19:38