==================================
In some of the situation you want to generate a excel sheet dynamically from a dataset. This can be easily done as explained below. You need t
//
public void generateExcel(){
//
string myConnectionString="Server=(local);uid=sa;pwd=myPassword;database=myDataBaseName";
string mySql="select * from myTable";
SqlConnection myCon= new SqlConnection(myConnectionString);
//
SqlDataAdapter myDataAdapter = new SqlDataAdapter(mySql, myCon);
myCon.Open();
DataSet myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet);
myDataAdapter.FillSchema(myDataSet,SchemaType.Mapped);
//
DataGrid myDataGrid=new DataGrid();
myDataGrid.DataSource = myDataSet;
myDataGrid.DataBind();
//
ExportToExcel(myDataGrid,Response);
}
//
private void ExportToExcel(DataGrid dgExport, HttpResponse response ) {
//
response.Clear();
response.Charset = "";
response.ContentType ="application/vnd.ms-excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.Html32TextWriter htmlWrite = new System.Web.UI.Html32TextWriter(stringWrite) ;
DataGrid dg=new DataGrid();
dg=dgExport;
//
dg.GridLines =GridLines.None;
dg.HeaderStyle.Font.Bold =true;
dg.HeaderStyle.ForeColor =System.Drawing.Color.Red;
dg.ItemStyle.ForeColor =System.Drawing.Color.Black;
dg.DataBind();
dg.RenderControl(htmlWrite);
response.Write(stringWrite.ToString());
response.End();
}
No comments:
Post a Comment