Share export datagridview to excel vb.net

beautifuldog188

New member
## export datagridview sang excel trong vb.net

### 1. Giới thiệu

Trong hướng dẫn này, bạn sẽ tìm hiểu cách xuất DataGridView sang bảng tính Excel trong VB.NET.Chúng tôi sẽ sử dụng [Microsoft.office.Interop.Excel] (Microsoft.Office.Interop.Excel Namespace) để lập trình tạo sổ làm việc và bảng tính Excel,và sau đó thêm dữ liệu từ datagridview vào bảng tính.

### 2. Điều kiện tiên quyết

Để làm theo hướng dẫn này, bạn sẽ cần những điều sau đây:

* Visual Studio 2019 trở lên
* [Microsoft.office.Interop.excel] (Microsoft.Office.Interop.Excel Namespace) Thư viện
* Điều khiển DataGridView trên biểu mẫu của bạn

### 3. Các bước

Để xuất một DataGridView sang Excel trong VB.NET, hãy làm theo các bước sau:

1. Trong Visual Studio, hãy tạo một dự án Windows Forms mới.
2. Thêm điều khiển DataGridView vào biểu mẫu của bạn.
3. Thêm các tài liệu tham khảo sau vào dự án của bạn:

`` `
Microsoft.office.Interop.excel
`` `

4. Trong mã phía sau cho biểu mẫu của bạn, thêm mã sau:

`` `
Nhập Microsoft.Office.Interop.Excel

Nút phụ riêng1_click (người gửi dưới dạng đối tượng, e là EventArgs) Nút tay cầm1.Click

'Tạo một cuốn sách bài tập và bảng tính Excel.
Sổ làm việc mờ như Excel.Workbook mới
Bảng tính mờ như New Excel.WorkSheet
Workbook.worksheet.add (bảng tính)

'Thêm dữ liệu từ datagridview vào bảng tính.
Đối với mỗi hàng như Datarow trong DataGridView1.Rows
bảng tính.cells (Row.index + 1, 1) .Value = Row ("Tên đầu tiên")
bảng tính.cells (Row.index + 1, 2) .Value = Row ("Tên cuối cùng")
Kế tiếp

'Lưu sổ làm việc vào một tập tin.
Workbook.saveas ("C: \ Temp \ ExportedData.xlsx")

Kết thúc phụ
`` `

5. Chạy dự án của bạn.
6. Nhấp vào nút ** Xuất **.
7. Dữ liệu từ DataGridView sẽ được xuất sang bảng tính Excel.

### 4. Kết luận

Trong hướng dẫn này, bạn đã học cách xuất một DataGridView sang bảng tính Excel trong VB.NET.Bạn có thể sử dụng kỹ thuật này để xuất dữ liệu từ các ứng dụng của mình để vượt trội để phân tích hoặc báo cáo thêm.

### 5. Hashtags

* #Datagridview
* #Excel
* #vb.net
* #Xuất khẩu
* #SpreadSheet
=======================================
## Export Datagridview to Excel in VB.NET

### 1. Introduction

In this tutorial, you will learn how to export a datagridview to an Excel spreadsheet in VB.NET. We will use the [Microsoft.Office.Interop.Excel](https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel) library to programmatically create an Excel workbook and worksheet, and then add the data from the datagridview to the worksheet.

### 2. Prerequisites

To follow this tutorial, you will need the following:

* Visual Studio 2019 or later
* The [Microsoft.Office.Interop.Excel](https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel) library
* A datagridview control on your form

### 3. Steps

To export a datagridview to Excel in VB.NET, follow these steps:

1. In Visual Studio, create a new Windows Forms project.
2. Add a datagridview control to your form.
3. Add the following references to your project:

```
Microsoft.Office.Interop.Excel
```

4. In the code behind for your form, add the following code:

```
Imports Microsoft.Office.Interop.Excel

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

' Create an Excel workbook and worksheet.
Dim workbook As New Excel.Workbook
Dim worksheet As New Excel.Worksheet
workbook.Worksheets.Add(worksheet)

' Add the data from the datagridview to the worksheet.
For Each row As DataRow In dataGridView1.Rows
worksheet.Cells(row.Index + 1, 1).Value = row("First Name")
worksheet.Cells(row.Index + 1, 2).Value = row("Last Name")
Next

' Save the workbook to a file.
workbook.SaveAs("C:\Temp\ExportedData.xlsx")

End Sub
```

5. Run your project.
6. Click the **Export** button.
7. The data from the datagridview will be exported to an Excel spreadsheet.

### 4. Conclusion

In this tutorial, you learned how to export a datagridview to an Excel spreadsheet in VB.NET. You can use this technique to export data from your applications to Excel for further analysis or reporting.

### 5. Hashtags

* #Datagridview
* #Excel
* #vb.net
* #Export
* #SpreadSheet
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top