Incorrect formatting of string to decimal during Excel import

Asked

Viewed 158 times

0

I am making an excel import for a dataGridView and the formatting of the field type Value does not bring the comma, period and decimal place, example:

In the Excel it’s like this: 995.800,00 (certain) and after import into dataGrdiView gets like this: 995800 (wrong)

inserir a descrição da imagem aqui Relevant excerpt from Code c#:

FileInfo File = new FileInfo(txtArquivoOrigem.Text);
ExcelPackage package = new ExcelPackage(new FileInfo(txtArquivoOrigem.Text));
ExcelWorksheet workSheet = package.Workbook.Worksheets.First();

for (int i = workSheet.Dimension.Start.Row + 1; i <= workSheet.Dimension.End.Row; i++)
{
    Cliente cliente = new Cliente();
    for (int j = workSheet.Dimension.Start.Column; j <= workSheet.Dimension.End.Column; j++)
    {       
        if (j == 9)
        {
            decimal faturamento;
            bool resultado = decimal.TryParse(workSheet.Cells[i, j].Value.ToString(), out faturamento);
            if (resultado)
                cliente.Faturamento = Convert.ToDecimal(workSheet.Cells[i, j].Value.ToString());
        }       
    }      
    lCliente.Add(cliente);
}

The value as it is in Excel: inserir a descrição da imagem aqui

I tried to implement the suggestion of Vik , but does not compile, returns the following error: None under load for the "Tostring" method takes 1 arguments. :
inserir a descrição da imagem aqui

2 answers

0

Maybe changing the formatting in the column itself (I named it "Billing"):

MyDataGridView.Columns["Faturamento"].ValueType = typeof(decimal);
MyDataGridView.Columns["Faturamento"].DefaultCellStyle.Format = "#,##0.00";

0

cliente.Faturamento = Convert.ToDecimal(workSheet.Cells[i, j].Value.ToString());

When using Tryparse, you already have the value in the variable faturamento... If the resultado for true:

cliente.Faturamento = faturamento;

use then . Tostring("n") to display in thousands separator format and two decimal places.

Standard Numeric Format Strings

  • tried to implement the suggestion, but neither compile, returns the following error: Nenhuma sobre carga para o método "ToString" leva 1 argumentos., I changed the post and put the error print.

  • Billing = billing; and then when presenting the result use then. Tostring("n"), you cannot use this string format on Object type objects... post the code of how to display/pass the data to the datagridview

  • could make a customer. Billing = Convert.Todecimal(billing.Tostring("n")); but would only take the two decimal places, to also take the thousands separator will have to format the decimal var at "output".

Browser other questions tagged

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