intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

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

Chia sẻ: No Comment | Ngày: | Loại File: DOC | Số trang:4

379
lượt xem
50
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

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

Chủ đề:
Lưu

Nội dung Text: Xuất dữ liệu từ database ra file excel

  1. 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 ,
  2. [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 System.Windows.Forms; using System.Data; using System.Data.SqlClient; using Excel = Microsoft.Office.Interop.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;
  3. Excel.Application xlApp ; Excel.Workbook xlWorkBook ; Excel.Worksheet xlWorkSheet ; object misValue = System.Reflection.Missing.Value; xlApp = new Excel.ApplicationClass(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1 ); connectionString = "data source=servername;initial catalog=data basename;user id=username;password=password;"; cnn = new SqlConnection(connectionString); cnn.Open(); sql = "SELECT * FROM Product"; SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn); DataSet ds = new DataSet(); dscmd.Fill(ds); for (i = 0; i
  4. private void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj ); obj = null; } catch (Exception ex) { obj = null; MessageBox.Show("Exception Occured while releasing object " + ex.ToString()); } finally { GC.Collect(); } } } } You think yellow, I say gold
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2