none
Formatar colunas no DataGridView e exportar para txt RRS feed

  • Pergunta

  • Estou preenchendo um dataGridView com um select no banco, porém estou com dificuldade em exportar da forma que está no grid.

    o banco retorna assim:

    COD_CONVENIADO    NOME_CONVENIADO         VALOR     VALORES_EXTRAS    VALOR_ONUS     VLR_TOTAL
    35000001008            AKIRA KIKUTE                    473,44     0                             111,52               584,96
    35000001018            TOYOMI KIKUTI                  473,44     0                             0                        473,44

    e eu preciso exportar assim:

    35000001008        ,AKIRA KIKUTE                            ,000473,44,000000,00,000111,52,000584,96
    35000001018        ,TOYOMI KIKUTI                           ,000473,44,000000,00,000000,00,000473,44

    no meu grid mostra isso:

    codigo                    nome                                          valor           valor extra   valor unus    total

    35000001008        ,AKIRA KIKUTE                            000473,44   000000,00   000111,52   000584,96
    35000001018        ,TOYOMI KIKUTI                          000473,44   000000,00   000000,00   000473,44

    e exporta isso:

    COD_CONVENIADO ,NOME_CONVENIADO                            ,VALOR  ,VALORES_EXTRAS ,VALOR_ONUS ,VLR_TOTAL,
    35000001008         ,AKIRA KIKUTE                                       ,473,44 ,3,00                       ,111,52           ,584,96   ,
    35000001018         ,TOYOMI KIKUTI                                     ,473,44 ,0,00                       ,0                    ,473,44   ,

    Meu código para Exportar

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace Faturamento_Nipo
    {
        class Exportar
        {
            public void exportardgvparatxt(DataGridView dgv)
            {
                SaveFileDialog sfg = new SaveFileDialog();
                sfg.FileName = "";
                sfg.Filter = "Documento de Texto|*.txt";
                sfg.Title = "Salvar DatagridView";

                DialogResult result = sfg.ShowDialog();
                if (result == DialogResult.OK)
                {
                    try
                    {
                        List<int> columnSizes = new List<int>();

                        for (int i = 0; i < dgv.Columns.Count; i++)
                            columnSizes.Add(dgv.Columns[i].HeaderText.Length);

                        foreach (DataGridViewRow row in dgv.Rows)
                        {
                            for (int i = 0; i < dgv.Columns.Count; i++)
                            {
                                string valueString = Convert.ToString(row.Cells[i].Value);
                                if (valueString.Length > columnSizes[i])
                                    columnSizes[i] = valueString.Length;
                            }

                        }

                        using (StreamWriter writer = new StreamWriter(sfg.FileName, false))
                        {

                            for (int i = 0; i < dgv.Columns.Count; i++)
                            {
                                string headerText = dgv.Columns[i].HeaderText;

                                writer.Write(headerText);

                                if (headerText.Length < columnSizes[i])
                                    writer.Write(new string(' ', columnSizes[i] - headerText.Length));

                                if (i < dgv.Columns.Count - 1)
                                    writer.Write(" ");
                                    writer.Write(',');
                            }

                           writer.WriteLine();

                            foreach (DataGridViewRow row in dgv.Rows)
                            {
                                for (int i = 0; i < dgv.Columns.Count; i++)
                                {

                                    string valueString = Convert.ToString(row.Cells[i].Value);

                                    writer.Write(valueString);

                                    if (valueString.Length < columnSizes[i])
                                        writer.Write(new string(' ',columnSizes[i] - valueString.Length));

                                    if (i < dgv.Columns.Count - 1)
                                        writer.Write(" ");
                                        writer.Write(',');
                                    
                                }

                                writer.WriteLine();
                            }
                        }

                        MessageBox.Show("Exportado com sucesso", "Exportado com sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error); ;
                    }
                    
                }
            }
        }
    }

    sexta-feira, 12 de julho de 2019 13:52