Diễn đàn    Window Form programming    Làm sao để Export dữ liệu từ Datagrid sang Excel trong windows form của C#

Thành viênTrả lời
khamphait


1  bài
18-6-2010 10:38:11
Em đang làm đề tài quản lý điểm sinh viên. Còn giai đoạn cuối cùng là Export bảng điểm ra file Excel. Rất mong được hỗ trợ của các anh/chị để em có thể hoàn thành đề tai của mình tốt hơn. Chân thành cảm ơn!
 
lyngochung


17  bài
19-6-2010 12:19:44
Bạn thử làm như sau nhé

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=TenFile.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
YourDataGrid.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
 
phuonglt_hhsc


9  bài
20-5-2011 14:22:52
Em có thể làm như sau (Code này a đã làm trong 1 phần mềm Quản lý Winform và khách hàng đã dùng ổn định nên cứ làm như vậy là được)

using Office_12 = Microsoft.Office.Core;
using Excel_12 = Microsoft.Office.Interop.Excel;

DataTable dsExcel =Day là datatale đổ vào Grid


Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Application.Workbooks.Add(true);

Excel_12.Application oExcel_12 = null; //Excel_12 Application
Excel_12.Workbook oBook = null; // Excel_12 Workbook
Excel_12.Sheets oSheetsColl = null; // Excel_12 Worksheets collection
Excel_12.Worksheet oSheet = null; // Excel_12 Worksheet
Excel_12.Range oRange = null; // Cell or Range in worksheet
Object oMissing = System.Reflection.Missing.Value;
oExcel_12 = new Excel_12.Application();

// Make Excel_12 visible to the user.
oExcel_12.Visible = true;
// Set the UserControl property so Excel_12 won't shut down.
oExcel_12.UserControl = true;
// System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
// Add a workbook.
oBook = oExcel_12.Workbooks.Add(oMissing);
// Get worksheets collection
oSheetsColl = oExcel_12.Worksheets;

oSheet = (Excel_12.Worksheet)oSheetsColl.get_Item("Sheet1");

for (int j = 0; j < dsExcel.Columns.Count; j++)
{
//bo qua dong so 7
oRange = (Excel_12.Range)oSheet.Cells[8, j + 1];
oRange.Value2 = dsExcel.Columns[j].ColumnName;
// oRange.ColumnWidth = "60";
oRange.Font.Bold = true;
}

for (int i = 0; i < dsExcel.Rows.Count; i++)
{
for (int j = 0; j < dsExcel.Columns.Count; j++)
{
oRange = (Excel_12.Range)oSheet.Cells[i + 9, j + 1];
oRange.Value2 = dsExcel.Rows[j];
}
}

oBook.Close(null, null, null);
excel.Application.Workbooks.Close();
excel.Quit();
oExcel_12 = null;
oBook = null;
oSheetsColl = null;
oSheet = null;
oRange = null;
---
Nhận thiết kế Web, Module DNN: http://websitegiarevn.com
 

Chủ đề gần đây :

Cùng loại :

Tên file Mô tả chi tiết Ngày
NWeb.zip (1) Module đơn giản Newsweb trên Dotnetnuke v10.x.x.x10/18/2025 8:08:11 AM
vspforum.zip (11) Ma nguon vspforum ngay xua4/18/2023 6:38:37 AM
pdfjs.rar (2) pdfjs 2017 : hiển thị tốt trên iphone 11, 12, 13 không lỗi, bản 2012 sẽ lỗi trên iphone6/21/2022 11:52:48 AM
pdfjs2.rar (2) Xem file pdf bằng viewer.hml cua pdfjs (thư viện chuẩn mozilla) 2012. https://mozilla.github.io/pdf.js/getting_started/#download có thể download bản prebuild tại đây6/21/2022 11:52:04 AM
runner.zip (0) using three.js, orbitcontrol to view an object move random on map. Di chuyển 1 đồ vật ngẫu nhiên trên bản đồ, sử dụng với demo nhân viên di chuyển trong văn phòng. Toàn js download về là chạy12/5/2019 5:55:14 PM
gmap.zip (1) google map + marker7/17/2019 2:25:05 PM
vinsmarthomeservice.zip (1) java post json to api, use AsyncTask, event listener7/9/2019 5:00:10 PM
fblogin.zip (0) Login facebook bang javascript SDK7/9/2019 9:16:37 AM
autocomplete-location.zip (2) autocomplete location geo from google place, html + js7/4/2019 4:37:55 PM
WebAPI.zip (8) api for android access db (v1.0.0)7/4/2019 9:14:17 AM