»Dotnet Ads
»Message Boards
Message Boards
Dotnet Books
»Member Details
Register
Login
LogOut
Submit Code
Submit Jobs
Submit Projects
»Competition
Community
Winners
Prizes
Write For Us
Members
»Other Resources
Links
Dotnet Resources
|
DataGrid to ExcelFunction to convert a Datagrid to a Excel format
public bool DataGridToExcel(DataGrid dgExport, HttpResponse response)
{
bool bolSuccess = false;
try
{
//clean up the response.object
response.Clear();
response.Charset = "";
//set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
//create a string writer
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
//create an htmltextwriter which uses the stringwriter
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
//instantiate a datagrid
DataGrid dg = new DataGrid();
// just set the input datagrid = to the new dg grid
if (dgExport == null)
{
throw new Exception("DataGrid is a Null object");
}
else
{
dg = dgExport;
}
dg.HeaderStyle.Font.Bold = true;
dg.DataBind();
//tell the datagrid to render itself to our htmltextwriter
dg.RenderControl(htmlWrite);
//output the html
response.Write(stringWrite.ToString());
response.End();
//HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (Exception ex)
{
this._ErrorMessage = ex.Message;
bolSuccess = false;
}
return bolSuccess;
} |
|