Skip to content

Excel Export Need steps

O. Developer

Asked Apr 3, 2016, 4:00 AM EDT7 answers
Share
LinkedInX
Dear  All,
 
In Visual Studio 2010, want to know how to export report to excel  -
The excel file should be created  in local user system.
 
 
Please guide me with steps.
 

Thanks a lot !!!
 
Visual Studio 2010
Crytal reprots
SQl Server 2008 
 C#

6 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Saillesh Pawar
    +1
    try it you will find the difference.
    +1
  • Saillesh Pawar
    0
    Do you want to export gridview records to excel or crystal reports?
    0
  • 0
    But what we want is - the output file should not be created on IIS.. Is it possible ?
    0
  • 0
    i gone thru both links.
     
    The first link is windows client based not web based.
     
    In the seocond link - It is created  for PDF/Excel/Word files.
     
    BUt  how i understand  where is phycial locaton -the output file is getting generated ?
     
    SOrry for silly questions 
    0
  • 0
    Below is our code :
     
    The problem is  this code is creating file in IIS  foldoer  Report.
     
    But our system admin is objecting that  - cannot provide write permission on this folder. You should find alternate way to write in local pc/client pc .
     
    Can you help us to get this done from our code:
    ---------------------------
    C#
      try
            {
                SqlConnection conn = new SqlConnection(StrCon);
                string sql = "SELECT ROW_NUMBER() OVER(ORDER BY CUST_NAME) as [Sl.No],PRO_NO AS [PROJECT NO],PRO_NAME AS [PROJECT NAME]," +
                            "CUST_NAME AS [CUSTOMER NAME],CUST_ADD AS [CUST ADD],cc_code AS [COST CENTER],cc_name AS [COST CENTER NAME] " +
                            "FROM TS_PROJECT_INFO ORDER BY CUST_NAME";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "Dept");
                if (ds.Tables[0].Rows.Count < 0)
                    return;
                var fpath = string.Empty;
                //Excel File Creation
                fpath = Server.MapPath("~/report/TimeSheet" + Session["UID"] + ".xls");
                try
                {
                    if (System.IO.File.Exists(fpath) == true)
                    {
                        System.IO.File.Delete(fpath);
                    }
                }
                catch (Exception ex )
                {
                   // Response.Write("");
                    Response.Write(ex.ToString());
                }
                if (fpath.Trim() != string.Empty)
                    DataSetToExcel(ds, fpath, "");
    
            }
            catch (Exception ex )
            {
                //Response.Write("");
                Response.Write(ex.ToString());
            }
        }
    
        void DataSetToExcel(DataSet dsExport, string path, string tableName)
        {
            if (path == string.Empty)
                return;
            StreamWriter SWriter = new StreamWriter(path);
            string str = string.Empty;
            Int32 colspan = dsExport.Tables[0].Columns.Count;
    str += "";
    C#
            str += "" + tableName + "ASIS Projects Report";
            //str += "From Date :" + DatePicker1.CalendarDate.ToString("dd-MM-yyyy") + "      To Date :  " + DatePicker2.CalendarDate.ToString("dd-MM-yyyy") + "";
            foreach (DataColumn DBCol in dsExport.Tables[0].Columns)
            {
                str += "" + DBCol.ColumnName + "";
            }
            str += "";
            foreach (DataRow DBRow in dsExport.Tables[0].Rows)
            {
                str += "";
                foreach (DataColumn DBCol in dsExport.Tables[0].Columns)
                {
                    str += "" + Convert.ToString(DBRow[DBCol.ColumnName]) + "";
                }
                str += "";
            }
            str += "";
            SWriter.WriteLine(str);
            SWriter.Flush();
            SWriter.Close();
            if (path.Length > 5)
                DownloadFile(path);
        }
        void DownloadFile(string FPath)
        {
            String strRequest = Request.QueryString["file"];
            FileInfo file = new FileInfo(FPath);
            if (file.Exists)
            {
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                Response.AddHeader("Content-Length", file.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.WriteFile(file.FullName);
                Response.End();
            }
            else
            {
                Response.Write("This file does not exist.");
            }
        }
        protected void btnDisplay_Click(object sender, EventArgs e)
        {
            BindGrid();
            btnClose1.Visible = true;
        }
        protected void BindGrid()
        {
            try
            {
    
                SqlConnection conn = new SqlConnection(StrCon);
                conn.Open();
                string sql = "SELECT ROW_NUMBER() OVER(ORDER BY CUST_NAME) as [Sl.No],PRO_NO AS [PROJECT NO],PRO_NAME AS [PROJECT NAME],"+
                             "CUST_NAME AS [CUSTOMER NAME],CUST_ADD AS [CUST ADD],cc_code AS [COST CENTER],cc_name AS [COST CENTER NAME] "+
                             "FROM TS_PROJECT_INFO ORDER BY CUST_NAME";
                SqlDataAdapter da1 = new SqlDataAdapter(sql, conn);
                DataSet ds1 = new DataSet();
                da1.Fill(ds1);
                GridView1.DataSource = ds1.Tables[0].DefaultView;
                GridView1.DataBind();
                conn.Close();
    
            }
            catch (Exception)
            {
                Response.Write("");
            }
        }
        protected void btnClose_Click(object sender, EventArgs e)
        {
            try
            {
                Session.Remove("tdate");
                Response.Redirect("~/Rptg");
            }
            catch (Exception)
            {
                Response.Write("");
    
            }
        }
        protected void btnClose1_Click(object sender, EventArgs e)
        {
            try
            {
                Session.Remove("tdate");
                Response.Redirect("~/Rptg");
            }
            catch (Exception)
            {
                Response.Write("");
    
            }
        }
        protected void GridView1_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
        {
            try
            {
                GridView1.PageIndex = e.NewPageIndex;
                BindGrid();
            }
            catch (Exception)
            {
                Response.Write("");
            }
        }
    }
     
    0
  • 1 short reply
    • 0
      Crystal Reports
      0