C# Tutorials and offshore development in India
Tutorials Resources Forum Reviews Communities Interview Jobs Projects Training Your Ad Here


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Automation »

Create excel file from datagrid


Posted Date: 21 Jun 2007    Resource Type: Code Snippets    Category: Automation
Author: ProgrammerMember Level: Diamond    
Rating: 1 out of 5Points: 10



First create a dataset and datatable.The columns in the datatable will be columns in the excel sheet.Re-define the columns of the datatable as
objDT.Columns("Employee_Name").ColumnName = "Employee"



Dim objDS As New DataSet
Dim objDT As New DataTable

'Create a dataset as required
objDT = objDS.Tables(0)
objDT.Columns("Employee_Name").ColumnName = "Employee"
objDT.Columns("Customer_Name").ColumnName = "Customer"
objDT.Columns("Visit_Count").ColumnName = "Visit Count"
Dim stwWriter As System.IO.StringWriter = New System.IO.StringWriter
Dim htwWriter As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(stwWriter)
Dim dgGrid As DataGrid = New DataGrid
dgGrid.DataSource = objDS
dgGrid.HeaderStyle.Font.Bold = True
dgGrid.Width.Percentage(100)
dgGrid.DataBind()
dgGrid.RenderControl(htwWriter)
Response.ContentType = "application/vnd.ms-excel"
Me.EnableViewState = False
Response.Write(stwWriter.ToString)
Response.End()






Responses to the resource: "Create excel file from datagrid"
Author: Anu    06 Jul 2007Member Level: Gold   Points : 0
As in code the data is exporting from DataGrid not directly from DataSet. The given title for the code is wrong. It title should be "Export Grid Data To Excel file" not "Create excel file from dataset "



Author: Anil panwar    09 Dec 2009Member Level: Bronze   Points : 2
protected void Button1_Click1(object sender, EventArgs e)
{
int min=0;
String strsql1;
DateTime Sdate;
DateTime Edate;
try
{
lobj = new UIRptServer.CommonFunctions();
String Strsql = "select datediff(day,'12/08/2009','12/08/2009')+1 as cnt";
idatatable = lobj.ReturnDataTable(Strsql);
if (idatatable.Rows.Count > 0)
{
String _min = idatatable.Rows[0]["cnt"].ToString();
min = Int32.Parse(_min) * 24 * 60 - 1;
}
strsql1 = "select * from ViewMobileKeywordDetails where keywordmasterid=3702 and applydate between '12/08/2009' and dateadd(minute," + min + ",'12/08/2009') order by Applydate";
idatatable1 = lobj.ReturnDataTable(strsql1);

System.Web.UI.WebControls.DataGrid grid =new System.Web.UI.WebControls.DataGrid();
grid.HeaderStyle.Font.Bold = true;
grid.DataSource = idatatable1;
grid.DataBind();
// render the DataGrid control to a file
_strpath = Server.MapPath(".") + "/Reports/" + "Anil" + "MSG" + "test.xls";
using (StreamWriter sw = new StreamWriter(_strpath))
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
grid.RenderControl(hw);
}
}

}
catch (Exception ex)
{
throw;
}

}


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to read an Excel file with OleDb and a simple SQL query?
Previous Resource: Disabling CheckBoxes in a CheckBoxList in Asp.net 2.0
Return to Discussion Resource Index
Post New Resource
Category: Automation


Post resources and earn money!
 
More Resources




About Us    Contact Us    Privacy Policy    Terms Of Use