C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » .NET »

PLZ small modification in export DataGridview Data into Excel Sheet in vb.net winforms


Posted Date: 01 Nov 2008      Posted By: dilip kumar      Member Level: Silver     Points: 1   Responses: 2




it's working ok but..It always takes same path plz check this once..I want give saving path to user..

Dim excel As Microsoft.Office.Interop.Excel.ApplicationClass = New Microsoft.Office.Interop.Excel.ApplicationClass()
excel.Application.Workbooks.Add(True)

Dim iColIndex As Integer = 0
Dim Datacol As DataColumn
For Each Datacol In dtList.Columns
iColIndex = iColIndex + 1
excel.Cells(1, iColIndex) = Datacol.ColumnName
Next
Dim iRowIndex As Integer = 0
Dim row As DataRow
Dim Datacolo As DataColumn
For Each row In DataGridview1.Rows
iRowIndex += 1
iColIndex = 0
For Each Datacolo In dtList.Columns
iColIndex += 1
excel.Cells(iRowIndex + 1, iColIndex) = row(Datacolo.ColumnName).ToString()
Next
Next
excel.Columns.AutoFit()
excel.Save("Sheet.xls")
excel.Workbooks.Close()





Responses

Author: Gaurav Agrawal    01 Nov 2008Member Level: DiamondRating: 3 out of 53 out of 53 out of 5     Points: 3

use this code for export dataset into excel it is for web application code but i think u can easily convert into desktop code

protected void btnExportXL1_Click(object sender, ImageClickEventArgs e)
{
try
{
GridView grd1 = new GridView();
grd1.DataSource = (DataView)Session["dv"];
grd1.DataBind();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=SalesReport.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
grd1.GridLines = GridLines.Both;
grd1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
catch (Exception ex)
{
Response.Write("Error : " + ex.Message);
}
}

GA

Thanks & Regards,

Gaurav Agrawal
Sr.Software Engineer
gaur1982@yahoo.com
09829373514



Author: dilip kumar    03 Nov 2008Member Level: SilverRating: 2 out of 52 out of 5     Points: 1

i am not using webform ..i want the code in windows forms
plz help



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : c sharp
Previous : repeator and ajax control
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use