How do I tie the data to gridview and then populate excel

  • 2020-12-05 17:09:20
  • OfStack

 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data .SqlClient ; 
using System.Data ; 

public partial class _Default : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
String sqlconn = "Server=.; DataBase=db; Integrated Security=SSPI "; 
string sql = "select top 10 * from table"; 

SqlConnection myConnection = new SqlConnection(sqlconn);//  Create a database connection instance  

myConnection.Open(); // Open the database  

SqlCommand myCommand = new SqlCommand(sql, myConnection);// create sql The instance of, execute 1 a sql 


SqlDataAdapter Adapter = new SqlDataAdapter();// create 1 a sql Data adapter  
Adapter.SelectCommand = myCommand;// Properties are set to   Retrieve a record from a data source  


DataSet myDs = new DataSet(); // Create an instance of the dataset  
Adapter.Fill(myDs);// Populating data set  

GridView1.DataSource = myDs.Tables[0].DefaultView;// 
GridView1.DataBind(); 

// DataToExcel(" The test of cxcel", GridView1); 
myConnection.Close();// Close the database connection  
} 
public void DataToExcel(string fileName, GridView myGridView) 
{ 
// Define document type and character encoding  
Response.Clear(); 
Response.Buffer = false; 
//Response.Charset = "utf-8"; 
Response.Charset = "GB2312"; 
// The next line is very important,  attachment  The parameter representation is downloaded as an attachment, which you can change  online Online open  
//filename=FileFlow.xls  Specify the name of the output file. Note that the extension matches the specified file type. It can be: .doc || .xls || .txt ||.htm 
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls"); 
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); 
//Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); 
//Response.ContentType Specify file type   Can I do for application/ms-excel || application/ms-word || application/ms-txt || application/ms-html ||  Or other browsers can directly support documents  
Response.ContentType = "application/ms-excel"; 
this.EnableViewState = false; 
//System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true); 
// define 1 An input stream  
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); 
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); 
// Bind the target data to the input stream output  
myGridView.RenderControl(oHtmlTextWriter); 
Response.Write(oStringWriter.ToString()); 
Response.End(); 
} 

// The following empty statement 1 Must be added, otherwise "must be placed with.  runat=server  Within the form tag." The error  
public override void VerifyRenderingInServerForm(Control control) 
{ 
} 

// Click on the event to generate excel 
protected void Button1_Click(object sender, EventArgs e) 
{ 
DataToExcel(" The test of cxcel", GridView1); 
} 
} 

Related articles: