Show string in excel without single quote

Asked

Viewed 72 times

1

I have a routine that generates an excel. It has a field in BD(Oracle) that is varchar. This means that in the database it is formatted with zeros on the left. Well, what happens is that when it is loaded in excel, excel ignores the zeros on the left and shows only the absolute numeric value. In the mount to display in excel, I tried to include a simple quote, but when it opens in excel, the quote also appears, like this: '0000023654' and not like this 0000023654. This is the code that exports to excel: .....

foreach (var item in listaItensMatMed)
                {
                    StringBuilder itens = new StringBuilder();
                    itens.Append(item.DataInclusao + ";");
                    itens.Append(item.TipoTabela + ";");
                    itens.Append(item.Tabela + ";");
                    itens.Append(item.Codigo + ";");
                    itens.Append(item.TUSS + ";");
                    itens.Append(item.Descricao + ";");
                    itens.Append(item.Fabricante + ";");
                    itens.Append(item.ReferenciaFabricante + ";");
                    itens.Append(item.RegistroAnvisa + ";");
                    itens.Append(item.ClassificacaoSimpro + ";");
                    itens.Append(item.GrupoMatMed + ";");
                    itens.Append(item.GrupoEstatistico + ";");
                    itens.Append(item.AutorizacaoPrevisa + ";");
                    itens.Append(item.UltimaVigencia + ";");
                    itens.Append(item.Valor + ";");
                    itens.Append(item.PrestadorTabelaPropria + ";");

                    Response.Write(itens.ToString());

                    Response.Write("\r");
                    //Response.Flush();
                }
.....
  • Are you sure this generates something connected to Excel? It doesn’t look like it, the problem could be this. Maybe with more context you can know what’s going on.

  • It was solved using a " t", like this: itens.Append( "\t" + item.Codigo + ";");. That solved the problem.

  • @pnet, what happens if in your description you have a semicolon or a quotation mark? to avoid this kind of unexpected behavior, I advise you to use some more mature solution, such as the Filehelpers or the Csvhelper

  • @Tobymosque, nice Toby. I’ll take a look, but it turns out that this solution I put in, I have to send today still. I will take a look at these examples and then suggest an improvement.

  • Responding to colleague @bigown, I think the excel behavior generates this, because even though you put any "number" with zeros on the left, it doesn’t print the zeros, unless it understands to be a string.

  • 1

    I already answered this here: http://answall.com/questions/106395/como-exportar-um-datatable-para-o-excel-sem-que-ele-formatas-informa%C3%A7%C3%b5es

Show 1 more comment

1 answer

0

Try to send the simple quote at the beginning of the string.

Exemplo

Browser other questions tagged

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