Bạn đang xem trang 1 / 1 trang

Xuất dữ liệu từ GridView ra Excel

Đã gửi: Thứ 3 22/11/2011 3:14 pm
by NoBi
Tên bài viết: Xuất dữ liệu từ GridView ra Excel
Tác giả: Sưu tầm
Cấp độ bài viết: Chưa đánh giá
Tóm tắt: Xuất dữ liệu từ GridView ra Excel
C#
  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3.     Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
  4.     Response.Charset = String.Empty;
  5.     Response.ContentType = "application/vnd.xls";
  6.  
  7.     var sw = new System.IO.StringWriter();
  8.     var hw = new HtmlTextWriter(sw);
  9.     GridView1.RenderControl(hw);
  10.  
  11.     Response.Write(sw.ToString());
  12.     Response.End();
  13. }


VB.NET
  1.     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
  2.         Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")
  3.         Response.Charset = String.Empty
  4.         Response.ContentType = "application/vnd.xls"
  5.         Dim sw As System.IO.StringWriter = New System.IO.StringWriter()
  6.  
  7.         Dim hw As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(sw)
  8.         GridView1.RenderControl(hw)
  9.         Response.Write(sw.ToString())
  10.         Response.End()
  11.     End Sub

http://dev.meotom.net/2011/9/19/Xuat-du ... -Excel-492

Re: Xuất dữ liệu từ GridView ra Excel

Đã gửi: Chủ nhật 27/11/2011 7:29 pm
by truongphu
  1. Imports System.Data
  2. Imports System.Data.SqlClient
  3. Imports Excel = Microsoft.Office.Interop.Excel
  4. Public Class Form1
  5.     Private Sub Button1_Click(ByVal sender As System.Object, _
  6.         ByVal e As System.EventArgs) Handles Button1.Click
  7.  
  8.         Dim cnn As SqlConnection
  9.         Dim connectionString As String
  10.         Dim sql As String
  11.  
  12.         connectionString = "data source=servername;" & _
  13.         "initial catalog=databasename;user id=username;password=password;"
  14.         cnn = New SqlConnection(connectionString)
  15.         cnn.Open()
  16.         sql = "SELECT * FROM Product"
  17.         Dim dscmd As New SqlDataAdapter(sql, cnn)
  18.         Dim ds As New DataSet
  19.         dscmd.Fill(ds)
  20.         DataGridView1.DataSource = ds.Tables(0)
  21.         cnn.Close()
  22.     End Sub
  23.  
  24.     Private Sub Button2_Click(ByVal sender As System.Object, _
  25.     ByVal e As System.EventArgs) Handles Button2.Click
  26.  
  27.  
  28.         Dim xlApp As Excel.Application
  29.         Dim xlWorkBook As Excel.Workbook
  30.         Dim xlWorkSheet As Excel.Worksheet
  31.         Dim misValue As Object = System.Reflection.Missing.Value
  32.         Dim i As Integer
  33.         Dim j As Integer
  34.  
  35.         xlApp = New Excel.ApplicationClass
  36.         xlWorkBook = xlApp.Workbooks.Add(misValue)
  37.         xlWorkSheet = xlWorkBook.Sheets("sheet1")
  38.  
  39.         For i = 0 To DataGridView1.RowCount - 2
  40.             For j = 0 To DataGridView1.ColumnCount - 1
  41.                 xlWorkSheet.Cells(i + 1, j + 1) = _
  42.                     DataGridView1(j, i).Value.ToString()
  43.             Next
  44.         Next
  45.  
  46.         xlWorkSheet.SaveAs("C\vbexcel.xlsx")
  47.         xlWorkBook.Close()
  48.         xlApp.Quit()
  49.  
  50.         releaseObject(xlApp)
  51.         releaseObject(xlWorkBook)
  52.         releaseObject(xlWorkSheet)
  53.  
  54.         MsgBox("You can find the file C\vbexcel.xlsx")
  55.     End Sub
  56.  
  57.     Private Sub releaseObject(ByVal obj As Object)
  58.         Try
  59.             System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
  60.             obj = Nothing
  61.         Catch ex As Exception
  62.             obj = Nothing
  63.         Finally
  64.             GC.Collect()
  65.         End Try
  66.     End Sub
  67. End Class

Re: Xuất dữ liệu từ GridView ra Excel

Đã gửi: Thứ 3 20/01/2015 2:45 pm
by TanPhi
NoBi đã viết:Tên bài viết: Xuất dữ liệu từ GridView ra Excel
Tác giả: Sưu tầm
Cấp độ bài viết: Chưa đánh giá
Tóm tắt: Xuất dữ liệu từ GridView ra Excel
C#
  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3.     Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
  4.     Response.Charset = String.Empty;
  5.     Response.ContentType = "application/vnd.xls";
  6.  
  7.     var sw = new System.IO.StringWriter();
  8.     var hw = new HtmlTextWriter(sw);
  9.     GridView1.RenderControl(hw);
  10.  
  11.     Response.Write(sw.ToString());
  12.     Response.End();
  13. }


VB.NET
  1.     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
  2.         Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")
  3.         Response.Charset = String.Empty
  4.         Response.ContentType = "application/vnd.xls"
  5.         Dim sw As System.IO.StringWriter = New System.IO.StringWriter()
  6.  
  7.         Dim hw As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(sw)
  8.         GridView1.RenderControl(hw)
  9.         Response.Write(sw.ToString())
  10.         Response.End()
  11.     End Sub

http://dev.meotom.net/2011/9/19/Xuat-du ... -Excel-492

File xuất ra lưu ở đâu vậy ạ ?

Re: Xuất dữ liệu từ GridView ra Excel

Đã gửi: Thứ 5 22/01/2015 1:44 pm
by NoBi
Download về chứ ở đâu?!