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

4 1.7K 4
Xuất dữ liệu từ database ra file excel ppsx

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

Thông tin tài liệu

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 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; 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 <= ds.Tables[0].Rows.Count - 1; i++) { for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++) { data = ds.Tables[0].Rows[i].ItemArray[j].ToString(); xlWorkSheet.Cells[i + 1, j + 1] = data; } } xlWorkBook.SaveAs("sieuthiIT.xls", Excel.XlFileFormat.xlWorkboo kNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.x lExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); MessageBox.Show("Excel file created , you can find the file c:\ \sieuthiIT.com.xls"); } 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 . 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. 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. 0; int j = 0; Excel. Application xlApp ; Excel. Workbook xlWorkBook ; Excel. Worksheet xlWorkSheet ; object misValue = System.Reflection.Missing.Value; xlApp = new Excel. ApplicationClass();

Ngày đăng: 12/07/2014, 02:20

Từ khóa liên quan

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

Tài liệu liên quan