如何将某些列从DataGridView导出到Excel [英] How to export certain column from datagridview to excel

查看:157
本文介绍了如何将某些列从DataGridView导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.我叫Oktavianus Misro Adrianto.
如果我问一个非常基本的问题,请原谅我,但是我对此并不陌生.

我有一个Datagridview,其中包含来自sql server数据库的学生信息.现在我想将数据导出到Excel文件中.该文件已成功导出,但是我要执行的操作是导出某些列,或者当用户单击导出"按钮时,会出现一个选项表单,用户可以在其中选择要导出的列.

任何人都有关于如何执行此操作的想法吗?

我尝试过的事情:

这是我的代码

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()); }
           }
       }

推荐答案

只需调整两个复制列数据的循环即可.要么调整列数,要么在循环中添加测试以忽略某些列.像这样的东西:
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:
for (int i = 2; i < dataGridViewSiswa.Columns.Count - 1; i++)
{
    // ignore first two columns, and last 1
}


for (int i = 0; i < dataGridViewSiswa.Columns.Count; i++)
{
    if (i == 3)
        continue; // do not export column 4
}


这篇关于如何将某些列从DataGridView导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆