To delimit a txt file, that is, the line size should be 700 characters for all subsequent lines

Asked

Viewed 395 times

1

I created a routine that reads some tables and generates lines as read. It’s all right at first. but the generated txt exceeds the size of the fields, for example, a field has size 10 and another size 20 and another 15, the total would be 45, but when I edit the txt it shows more vacant spaces? What can it be? In the example below should not appear the vacant spaces I placed with interrogation. ex: texto01texto02texto03 xxxxxxxxxxhhhhhhhhhhhhhhhhhhhhkkkkkkkkkkkkkkk???????????????????? xxxxxxxxxxhhhhhhhhhhhhhhhhhhhhkkkkkkkkkkkkkkk????????????????????

I have developed the code below
Static public string generatorTXTDETIM(List records) {

        StringBuilder TIMXml = new StringBuilder();

        StringBuilder sb = new StringBuilder();

        foreach (var item in registros)
        {

            string testeID = Convert.ToString(item.Cells["ID"].Value.ToString());
            string testecodigo = Convert.ToString(item.Cells["CODIGO"].Value.ToString());

            string verificaConsultaAdquirente = VerificaConsulta.VerificaEscrituraAdquirente(testeID, testecodigo);

            if (verificaConsultaAdquirente == "V")
            {

                continue;


            }

            string verificaConsultaRequerente = VerificaConsulta.VerificaEscrituraRequerente(testeID, testecodigo);

            if (verificaConsultaRequerente == "V")
            {

                continue;
            }
            areaNaoConstaRecebe = " ";
            folhaRecebe = Convert.ToString(item.Cells["FOLHA"].Value.ToString());
            folhaRecebe = folhaRecebe.PadRight(5, ' ');
            inscricaoNirfrecebe = "";                   //item.Cells["FOLHA"].Value.ToString(),
            string valor01 = "01";
            dataLavratiraRecebe = String.Concat(Convert.ToDateTime(item.Cells["DATA"].Value.ToString()).ToString("dd/MM/yyyy").PadRight(10, ' '), livroRecebe = Convert.ToString(item.Cells["LIVRO"].Value.ToString()).PadRight(7, ' '));


            valornaoConstaDocrecebe = DETIM.valornaoConstaDocrecebe(item.Cells["valorvenal"].Value.ToString());

            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("pt-BR");
            baseCalduloItbiNTCDRecebe = item.Cells["TIPOTRANSACAO"].Value.ToString().PadRight(15, ' ');
            valornaoConstaDocrecebe = valornaoConstaDocrecebe.ToString().PadRight(1, ' ');
            valorAlienacaoAquisicao = DETIM.ValidavalorAlienacaoAquisicao(item.Cells["valorvenal"].Value.ToString()).PadRight(15, ' ');
            baseCalduloItbiNTCDRecebe = item.Cells["TIPOTRANSACAO"].Value.ToString().PadRight(15, ' ');
            tipoImovelrecebe = DETIM.ValidaTipotrans(item.Cells["TIPOIMOVEL"].Value.ToString()).PadRight(2, ' ');
            descricaoTipoImovelOutrosrecebe = item.Cells["TipoTransDescr"].Value.ToString().PadRight(30, ' ');
            situacaoConstrucaoRecebe = item.Cells["SituacaoTrib"].Value.ToString().PadRight(1, ' ');
            localizacaoRecebe = item.Cells["LOCALIZACAO"].Value.ToString().PadRight(1, ' ');
            valornaoConstaDocrecebe = string.Format("{0:C}", Convert.ToDouble(valornaoConstaDocrecebe));

            retificacaoAtorecebe = String.Concat(item.Cells["RETIF_ATO"].Value.ToString().PadRight(1, ' '), Convert.ToDateTime(item.Cells["DATA"].Value.ToString()).ToString("dd/MM/yyyy").PadRight(10, ' '));

            valornaoConstaDocrecebe = String.Concat(valornaoConstaDocrecebe.ToString().PadRight(1, '0'), item.Cells["valorvenal"].Value.ToString().PadRight(15, ' '));


                                        areaRecebe = DETIM.valida_area(item.Cells["AREA"].Value.ToString());
                                        M_Numero_Imv = DETIM.VNulo(item.Cells["NUMERO"].Value.ToString());
                                        M_complemento_imv = DETIM.ValidaComplemento(item.Cells["COMPLEMENTO"].Value.ToString());
                                     //   M_TIPO_TRANS = IIf(IsNull(gRS!Cod_Doi) Or Trim(gRS!Cod_Doi) = "", "39", gRS!Cod_Doi)


                                        M_bairro_imv = DETIM.VNulo(item.Cells["BAIRRO"].Value.ToString());
                                        M_municipio = DETIM.VNulo(item.Cells["MUNICIPIO"].Value.ToString());

                                        m_cep = DETIM.VNulo(item.Cells["CEP"].Value.ToString());
                                        m_cep = m_cep.Replace( "-", "");
                                        m_cep =  m_cep.Replace( ".", "");
                                        m_cep = String.Format(m_cep, "00000000");
                                        M_SituacaoConstr = DETIM.ValidaSITUACAOcONSTR(DETIM.VNulo(item.Cells["SITUACAOCONSTR"].Value.ToString()));
                                        M_Local = DETIM.ValidalOCAL(DETIM.VNulo(item.Cells["LOCALIZACAO"].Value.ToString()));
                                        M_UF =  DETIM.Validauf(DETIM.VNulo(item.Cells["UF"].Value.ToString()));
                                        m_nirf = DETIM.formataM_Nirf(DETIM.VNulo(item.Cells["REGNUM"].Value.ToString()));
                                        m_situacaoDoi = DETIM.VNulo(item.Cells["SITUACAODOI"].Value.ToString());
                                        m_retifAto = DETIM.VNulo(item.Cells["RETIF_ATO"].Value.ToString());
                                        m_natureza = DETIM.VNulo(item.Cells["NATUREZA"].Value.ToString());
                                        m_ncontrole = DETIM.VNulo(item.Cells["NCONTROLE"].Value.ToString());

                                        int n = 30;

                                        Tamanho01 = new String(' ', n); // creates a string of n spaces.
                                        //    varArq = space(365)

                                        string valor1 = "1";

                                        sb.AppendFormat("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}{17}{18}{19}{20}{21}{22}{23}{24}{25}{26}{27}{28}{29}{30}{31}{32}{33}{34}{35}{36}{37}{38}{39}{40}{41}{42}{43}{44}{45}{46}{47}",


                                         idRegistro = "1", //
                                         numeroControle = "          ",
                                         dataLavratiraRecebe = String.Concat(Convert.ToDateTime(item.Cells["DATA"].Value.ToString()).ToString("dd/MM/yyyy").PadRight(10, ' '), livroRecebe = Convert.ToString(item.Cells["LIVRO"].Value.ToString()).PadRight(7, ' ')),
                                         folhaRecebe = Convert.ToString(item.Cells["FOLHA"].Value.ToString()).PadRight(5, ' '),
                                         matricularecebe = item.Cells["MATRICULA"].Value.ToString().PadRight(15, ' ').Replace(".", ""),
                                         registrosRecebe = item.Cells["REGISTRO"].Value.ToString().PadRight(15, ' ').Replace(".", ""), //r4
                                         SITUACAODOIRecebe = item.Cells["SITUACAODOI"].Value.ToString().PadRight(1, ' '),  //ok    // item.Cells["SITUACAODOI"].Value.ToString().PadRight(5, ' '),
                                         AtribuicaoDoiRecebe = String.Concat("1", item.Cells["COD_DOI"].Value.ToString().PadRight(1, ' '), item.Cells["ESPECIE"].Value.ToString().PadRight(2, ' ')),// item.Cells["COD_DOI"].Value.ToString().PadRight(2, ' '),//ok
                                         descri_tipoTransacaoOutrosRecebe =    item.Cells["OUTRANATUREZA"].Value.ToString().PadRight(30, ' '),                                                        
                                         retificacaoAtorecebe = String.Concat(item.Cells["RETIF_ATO"].Value.ToString().PadRight(1, ' '), Convert.ToDateTime(item.Cells["DATA"].Value.ToString()).ToString("dd/MM/yyyy").PadRight(10, ' ')),
                                         formaAlienacaorecebe = String.Concat(item.Cells["FORMAALIEN"].Value.ToString().PadRight(1, ' '), '0'), (valorAlienacaoAquisicao.ToString().PadRight(15, ' ')),
                                         valornaoConstaDocrecebe = String.Concat(valornaoConstaDocrecebe.ToString().PadRight(1, '0')),
                                         valorAlienacaoAquisicao = (valorAlienacaoAquisicao.ToString().PadRight(15, ' ')),
                                         baseCalduloItbiNTCDRecebe =  (valorAlienacaoAquisicao.ToString().PadRight(15, ' ')),
                                         tipoImovelrecebe = tipoImovelrecebe.PadRight(2, ' ').Trim(),
                                         //Fim da folha 01
                                         //***************************************************************************************************************************************************
                                         descricaoTipoImovelOutrosrecebe = item.Cells["DESCIMOVEL"].Value.ToString().PadRight(30, ' '),
                                         situacaoConstrucaoRecebe = item.Cells["SituacaoTrib"].Value.ToString().PadRight(1, ' '),
                                         localizacaoRecebe =  item.Cells["LOCALIZACAO"].Value.ToString().PadRight(1, ' '),
                                         areaNaoConstaRecebe = areaNaoConstaRecebe.PadRight(1, ' '),
                                         areaRecebe = areaRecebe.PadRight(17, ' '),
                                         enderecoImovelRecebe = item.Cells["RUA"].Value.ToString().PadRight(40, ' '),
                                         numeroRecebe =  M_Numero_Imv.PadRight(6, ' '),
                                         complementoRecebe = M_complemento_imv.PadRight(21, ' '),
                                         bairroRecebe = M_bairro_imv.PadRight(20, ' '),
                                         cepImovelRecebe = m_cep.PadRight(8, ' '),
                                         municipioRecebe =  M_municipio.PadRight(30, ' '),
                                         ufRecebe = M_UF.PadLeft(2, ' '),
                                         inscricaoNirfrecebe = m_nirf.PadRight(15, ' '),
                                         valorITBInaoConstaDocRecebe =valorAlienacaoAquisicao.PadRight(1, ' '),
                                         TIA01 = item.Cells["tia"].Value.ToString().PadRight(1, ' ').Substring(0, 1).Trim(),
                                         ITE01 = item.Cells["ite"].Value.ToString().PadRight(1, ' ').Substring(0, 1).Trim(),
                                         NGI01 = item.Cells["ngi"].Value.ToString().PadRight(10, ' ').Trim(),
                                         TCII01 = item.Cells["tcii"].Value.ToString().PadRight(1, ' ').Substring(0, 1).Trim(),
                                         NCNI01 = item.Cells["ncni"].Value.ToString().PadRight(11, ' ').Trim(),
                                         NPCNI01 = item.Cells["npcni"].Value.ToString().PadRight(14, ' ').Trim(),
                                         CIRCUNSCRICAO01 =item.Cells["circunscricao"].Value.ToString().PadRight(1, ' ').Substring(0, 1).Trim(),
                                         OPERACAO01 = item.Cells["operacao"].Value.ToString().PadRight(2, ' ').Substring(0, 2).Trim(),
                                         OI01 = item.Cells["oi"].Value.ToString().PadRight(1, ' ').Substring(0, 1).Trim(),
                                         CC01 =item.Cells["cc"].Value.ToString().PadRight(21, ' ').Trim(),
                                         CPFISICO01 = item.Cells["cpfisico"].Value.ToString().PadRight(7, ' ').Trim(),
                                         TI01 = item.Cells["ti"].Value.ToString().PadRight(2, ' ').Substring(0, 2).Trim(),
                                         TIC01 = item.Cells["Tic"].Value.ToString().PadRight(2, ' ').Substring(0, 2).Trim(),
                                         DTFI01 = item.Cells["dti"].Value.ToString().PadRight(300, ' '),
                                         NCGA01 = item.Cells["ncgai"].Value.ToString().PadRight(10, ' ').Trim(),
                                         AT01 = item.Cells["at"].Value.ToString().PadRight(17, ' ').Trim(),

                                         AC01 = item.Cells["ac"].Value.ToString().PadRight(17, ' ').Trim(),

                                         ATC01 = item.Cells["atc"].Value.ToString().PadRight(17, ' ').Trim(),
                                         ATT01 = item.Cells["att"].Value.ToString().PadRight(17, ' ').Trim(),
                                         Filler1 =Tamanho01.PadRight(2, ' ').Trim(),
                                           Environment.NewLine)
                                        .AppendLine();
            File.WriteAllText(@"c:\Clientes_Formatado.txt", sb.ToString());

                    }

                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }



        }





        return TIMXml.ToString();
    }

1 answer

1

I advise you to create a function to handle the fields. Ex:

public static string Limitar (this string str, int tamanho) {
  return (str == null || str.Length <= tamanho)   
    ? str
    : str.Substring(0, tamanho);
}

Ai would be used more or less like this: (field with size of 10)

 baseCalduloItbiNTCDRecebe = (item.Cells["TIPOTRANSACAO"].Value.ToString().PadRight(15, ' ')).Limitar(10);

Browser other questions tagged

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