Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone. My name is Oktavianus Misro Adrianto.
Pardon me if I ask a very basic question, but I am new to this.

I have a Datagridview which contain students information fromm sql server database. now I want to export the data to an excel file. the file was succesfully exported, but what I am trying to do is exporting certain columns or maybe when user click the export button, an option form appear in which the user can choose which column they want to export.

anyone has an idea about how to do this?

What I have tried:

Here is my code
private void btnExcel_Click(object sender, EventArgs e)
       {
           Cursor.Current = Cursors.WaitCursor;
           {
               try
               {
                   Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
                   Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
                   Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
                   app.Visible = true;
                   worksheet = workbook.Sheets["Sheet1"];
                   worksheet = workbook.ActiveSheet;
                   worksheet.Name = "Database Siswa";
                   //lokasi untuk menyimpan file Excel
                   SaveFileDialog saveDialog = new SaveFileDialog
                   {
                       Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*",
                       FilterIndex = 2
                   };

                   if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                   {
                       workbook.SaveAs(saveDialog.FileName);
                       MessageBox.Show("Data telah berhasil dieksport", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                   }
                   try
                   {
                       for (int i = 0; i < dataGridViewSiswa.Columns.Count; i++)
                       {
                           worksheet.Cells[1, i + 1] = dataGridViewSiswa.Columns[i].HeaderText;
                       }
                       for (int i = 0; i < dataGridViewSiswa.Rows.Count; i++)
                       {
                           for (int j = 0; j < dataGridViewSiswa.Columns.Count; j++)
                           {
                               //milih kolom mana yang mau dieksport
                               if (dataGridViewSiswa.Rows[i].Cells[j].Value != null)
                               {
                                   worksheet.Cells[i + 2, j + 1] = dataGridViewSiswa.Rows[i].Cells[j].FormattedValue.ToString();
                               }
                               else
                               {
                                   worksheet.Cells[i + 2, j + 1] = "";
                               }
                           }
                       }
                   }
                   catch (System.Exception ex)
                   {
                       MessageBox.Show(ex.Message);
                   }

                   finally
                   {
                       app.Quit();
                       workbook = null;
                       worksheet = null;
                   }
               }
               catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
           }
       }
Posted
Updated 22-Feb-18 1:03am

1 solution

It is simply a matter of adjusting the two loops that copy the column data. Either adjust the column counts, or add tests within the loops to ignore certain columns. Something like:
C#
for (int i = 2; i < dataGridViewSiswa.Columns.Count - 1; i++)
{
    // ignore first two columns, and last 1
}

C#
for (int i = 0; i < dataGridViewSiswa.Columns.Count; i++)
{
    if (i == 3)
        continue; // do not export column 4
}
 
Share this answer
 
Comments
Oktavianus Misro 22-Feb-18 14:58pm    
Thank You @Richard MacCuthan
I've tried it, but it doesn't work. the all columns is still exported

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900