tailieunhanh - Xuất dữ liệu từ database ra file excel

Tài liệu tham khảo dành cho giáo viên, sinh viên chuyên ngành công nghệ thông tin - Xuất dữ liệu từ database ra file excel | Xuất dữ liệu từ database ra file excel Đầu tiên các bạn tạo một database như hình dưới Để làm như trên các bạn chạy các đoạn code sau trong SQLServer Mã: CREATE TABLE [dbo].[OrderMaster] ( [OrderMaster_id] [int] NOT NULL , [OrderMaster_date] [datetime] NULL , [OrderMaster_customername] [varchar] (50), [OrderMaster_createduser] [varchar] (50) ) ON [PRIMARY] CREATE TABLE [dbo].[OrderDetails] ( [OrderDetails_id] [int] NOT NULL , [OrderDetails_masterid] [int] NULL , [OrderDetails_productid] [int] NULL , [OrderDetails_qty] [int] NULL ) ON [PRIMARY] CREATE TABLE [dbo].[Product] ( [Product_id] [int] NOT NULL , [Product_name] [varchar] (50) , [Product_price] [numeric](18, 0) NULL ) ON [PRIMARY] Sau đó các bạn mở từng table ra và có thể nhập liệu Sau đó muốn xuất ra file excel thì code như sau Mã PHP: using System; using ; using ; using ; using Excel = ; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { SqlConnection cnn ; string connectionString = null; string sql = null; string data = null; int i = 0; int j = 0; xlApp ; xlWorkBook ; xlWorkSheet ; object misValue = ; xlApp = new (); xlWorkBook = (misValue); xlWorkSheet = ()(1); connectionString = "data source=servername;initial catalog=databasename;user id=username;password=password;"; cnn = new SqlConnection(connectionString); (); sql = "SELECT * FROM Product"; SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn); DataSet ds = new DataSet(); (ds); for (i = 0; i <= [0]. - 1; i++) { for (j = 0; j <= [0]. - 1; j++) { data = [0].Rows[i].ItemArray[j].ToString(); [i + 1, j + 1] = data; } } ("", , misValue, misValue, misValue, misValue, , misValue, misValue, misValue, misValue, misValue); (true, misValue, misValue); (); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); ("Excel file created , you can find the file c:\\"); } private void releaseObject(object obj) { try { (obj); obj = null; } catch (Exception ex) { obj = null; ("Exception Occured while releasing object " + ()); } finally { (); } } } } You think yellow, I say gold

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.