Export from datagridview to excel certain columns

1

I have a question.

I have this code for filling out a datagridview, which gets the data from an excel document.

string constr = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + textBox1.Text + ";Extended Properties=Excel 12.0 Xml;");

OleDbConnection con = new OleDbConnection(constr);
//Hacer los comandos

OleDbCommand oconn = new OleDbCommand("Select * From [MPS 
Sheet$A16:AP]", con);


con.Open();

//Hacer los adapters
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);

//hacer los datatables
DataTable data = new DataTable();



ds.Tables.Add(data);
sda.Fill(data);
//Hacer las tablas
dataGridView1.DataSource = data;
//dataGridView7.DataSource = fecha;
con.Close();

What I want to do now, is to obtain only three columns of which this datagridview has, to place them in another new datagridview.

My question is Can it be done? and if you can, how would it be done?

Thanks

    
asked by oscar ramirez 18.04.2017 в 16:23
source

1 answer

1

Yes, it is possible. You just have to specify the columns you need. This is an example that a companion gave me, I hope it serves you.

 using System;
 using System.Collections.Generic;
 using System.Text;
 using System.Data;   


 namespace BDPROG_CSharp
 {
     class classExcel
     {
         public int columnas;
         DataTable dt;
         Microsoft.Office.Interop.Excel.Application oXL;
         Microsoft.Office.Interop.Excel._Worksheet oSheet;
         Microsoft.Office.Interop.Excel.Range oRng;           

         public classExcel(int columnas, DataTable dt)
         {
             this.columnas = columnas;
             this.dt = new DataTable();
             this.dt = dt;
         }
         public void crearexcel()
         {

             oXL = new Microsoft.Office.Interop.Excel.Application();
             oXL.Visible = true;
             //Creamos un nuevo libro de Excel
             oXL.Workbooks.Add();
             oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oXL.ActiveSheet;
             //Le asignamos a la hoja 1 el nombre de "Datos"
             oSheet.Name = "Datos";
             //Pegamos el contenido
             oSheet.get_Range(oSheet.Cells[1, 1], oSheet.Cells[1, 1]).Select();
             oXL.ActiveCell.PasteSpecial();
             oSheet.get_Range(oSheet.Cells[1, 1], oSheet.Cells[1, 1]).EntireColumn.Delete();

             ////Creamos los encabezados                                     
             for (int z = 0; z <= columnas; z++)
             {
                 oSheet.Cells[1, z + 1] = Convert.ToString(dt.Columns[z].ColumnName);

                 //  oSheet.get_Range("A1", "D1").Font.Bold = true;
                 oSheet.get_Range(oSheet.Cells[1, z + 1], oSheet.Cells[1, z + 1]).Font.ColorIndex = 2;
                 oSheet.get_Range(oSheet.Cells[1, z + 1], oSheet.Cells[1, z + 1]).Interior.ColorIndex = 5;
                 oSheet.get_Range(oSheet.Cells[1, z + 1], oSheet.Cells[1, z + 1]).Font.Bold = true;
                 oSheet.get_Range(oSheet.Cells[1, z + 1], oSheet.Cells[1, z + 1]).Font.Italic = true;
                 oSheet.get_Range(oSheet.Cells[1, z + 1], oSheet.Cells[1, z + 1]).VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
                 // oRng = (oSheet.Cells[1, z], oSheet.Cells[1, z]);
                 oRng = oSheet.get_Range(oSheet.Cells[1, z + 1], oSheet.Cells[1, z + 1]);
                 oRng.EntireColumn.AutoFit();

             }






         }
     }
 }
    
answered by 18.04.2017 / 17:35
source