Import Export Excel với C# 2005 docx

5 396 0
Import Export Excel với C# 2005 docx

Đang tải... (xem toàn văn)

Thông tin tài liệu

Import Export Excel với C# 2005 Chức năng Import Khi thực hiện chức năng lấy dữ liệu từ excel thì những bước quan trọng nhất là : - Chuỗi kết nối với Excel ( cho phép kết nối với cả excel 2007) strConn="Provider=Microsoft.ACE.OLEDB.12.0; DataSource=D:\\Data\\Excel.xslx; Extended Properties = 12.0"; - Thực hiện việc truy vấn dữ liệu và đổ vào dataset (hoặc đưa thẳng vào database bằng cách thay đổi câu lệnh query) OleDbDataAdapter da=new OleDbDataAdapter("Select ten_column1, ten_column2, from [Sheet$]", strConn) - Từ dataset đưa dữ liệu vào dabase Chức năng Export Để export được dữ liệu ra excel thì trước tiên ta phải khai báo các thư viện sử dụng liên quan đến Excel using Microsoft.Office.Interop.Excel; using VBIDE_12 = Microsoft.Vbe.Interop; using Office_12 = Microsoft.Office.Core; using Excel_12 = Microsoft.Office.Interop.Excel; Tiếp đó export dữ liệu từ database ra excel ApplicationClass excel = new 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.Tables[0].Columns.Count; j++) { oRange = (Excel_12.Range)oSheet.Cells[1, j + 1]; oRange.Value2 = dsExcel.Table[0].Columns[j].ColumnName; } for(int i=0; i < dsExcel.Tables[0].Rows.Count; i++) { for(int j=0; j<dsExcel.Tables[0].Columns.Count; j++) { oRange = (Excel_12.Range)oSheet.Cells[i + 2, j + 1]; oRange.Value2 = dsExcel.Tables[0].Rows[i][j]; } } Giải thích : 1. Excel_12.Application là ứng dụng Excel, nó là đối tượng cao nhất, quản lý các chương trình excel đang chạy trên máy tính 2. Excel_12.WorkBook là 1 ứng dụng excel (đây chính là đối tượng excel ta cần tạo ra để export dữ liệu vào) 3. Excel_12.Sheets là tập hợp tất cả các sheet trong đối tượng Excel mà ta tạo ra 4. Excel_12.WorkSheet là sheet mà hiện tại ta đang làm việc 5. Excel_12.Range là (tập hợp) cell mà ta làm việc 6. Từ các điều trên ta có thứ tự làm việc của đoạn code - Trước tiên chạy ứng dụng Excel - Tạo ra đối tượng Excel làm việc - Tạo ra các sheet của đối tượng Excel đó - Tạo ra sheet làm việc - Điền các giá trị vào các cell trong sheet làm việc (cell có vị trí thấp nhất là hàng 1 cột 1) Download code mẫu tại : ImportExportExcel.rar Theo http://my.opera.com/phihoanghai/blog/import-export-excel-voi-c . Import Export Excel với C# 2005 Chức năng Import Khi thực hiện chức năng lấy dữ liệu từ excel thì những bước quan trọng nhất là : - Chuỗi kết nối với Excel ( cho phép kết nối với cả excel. using Excel_ 12 = Microsoft.Office.Interop .Excel; Tiếp đó export dữ liệu từ database ra excel ApplicationClass excel = new ApplicationClass(); excel. Application.Workbooks.Add(true); Excel_ 12.Application. 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

Ngày đăng: 13/08/2014, 13:21

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan