c# 导出datagridview
时间: 2023-08-26 22:04:19 浏览: 224
要在C#中导出DataGridView的数据,可以使用以下方法之一:
方法一:利用微软的Excel操作类
首先,需要引用Microsoft.Office.Interop.Excel命名空间。然后,在按钮的点击事件中,判断DataGridView中是否有数据,如果有数据则调用ExportDataToExcel方法导出数据。
```csharp
using Excel = Microsoft.Office.Interop.Excel;
private void buttonX26_Click(object sender, EventArgs e)
{
if (dataGridViewX8.Rows.Count == 0)
{
MessageBox.Show("当前无数据可导出!");
}
else
{
ExportDataToExcel(dataGridViewX8);
}
}
private void ExportDataToExcel(DataGridView dataGridView)
{
Excel.Application excelApp = new Excel.Application();
Excel.Workbook workbook = excelApp.Workbooks.Add();
Excel.Worksheet worksheet = workbook.ActiveSheet;
// 将DataGridView中的数据写入Excel表格
for (int i = 0; i < dataGridView.Rows.Count; i++)
{
for (int j = 0; j < dataGridView.Columns.Count; j++)
{
worksheet.Cells\[i + 1, j + 1\] = dataGridView.Rows\[i\].Cells\[j\].Value.ToString();
}
}
// 保存Excel文件
workbook.SaveAs("导出数据.xlsx");
workbook.Close();
excelApp.Quit();
MessageBox.Show("数据导出成功!");
}
```
方法二:利用OleDb和Excel操作类
首先,需要引用System.Data.OleDb和Microsoft.Office.Interop.Excel命名空间。然后,可以使用OleDbConnection建立与本地数据库的连接。在按钮的点击事件中,调用ExportDataToExcel方法导出数据。
```csharp
using System.Data.OleDb;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data;
private void buttonX26_Click(object sender, EventArgs e)
{
if (dataGridViewX8.Rows.Count == 0)
{
MessageBox.Show("当前无数据可导出!");
}
else
{
ExportDataToExcel(dataGridViewX8);
}
}
private void ExportDataToExcel(DataGridView dataGridView)
{
OleDbConnection connection = Getconnection();
connection.Open();
// 查询DataGridView中的数据
string query = "SELECT * FROM YourTableName";
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connection);
DataTable dataTable = new DataTable();
dataAdapter.Fill(dataTable);
// 创建Excel应用程序
Excel.Application excelApp = new Excel.Application();
Excel.Workbook workbook = excelApp.Workbooks.Add();
Excel.Worksheet worksheet = workbook.ActiveSheet;
// 将DataTable中的数据写入Excel表格
for (int i = 0; i < dataTable.Rows.Count; i++)
{
for (int j = 0; j < dataTable.Columns.Count; j++)
{
worksheet.Cells\[i + 1, j + 1\] = dataTable.Rows\[i\]\[j\].ToString();
}
}
// 保存Excel文件
workbook.SaveAs("导出数据.xlsx");
workbook.Close();
excelApp.Quit();
connection.Close();
MessageBox.Show("数据导出成功!");
}
private OleDbConnection Getconnection()
{
string path_startup = System.Windows.Forms.Application.StartupPath;
string Constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path_startup + @"\Data\测试.accdb";
OleDbConnection myCon = new OleDbConnection(Constr);
return myCon;
}
```
以上是两种常用的方法来导出DataGridView的数据到Excel表格。你可以根据自己的需求选择其中一种方法来实现。
#### 引用[.reference_title]
- *1* [C# winform 直接导出DataGridView数据到Excel表格的两种方法](https://blog.csdn.net/wwq0813/article/details/93294416)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* *3* [C#中导出datagridview并保存本地](https://blog.csdn.net/u010719791/article/details/119920425)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文
相关推荐



















