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#
Know the answer? Post it. Somebody with the same question will find it here.
Saillesh PawarRank #537 · 4.5K pointsPosted Apr 3, 2016, 4:35 AM EDT
Saillesh PawarRank #537 · 4.5K pointsPosted Apr 3, 2016, 10:35 AM EDT
Saillesh PawarRank #537 · 4.5K pointsPosted Apr 3, 2016, 4:10 AM EDT
O. Developer15 pointsPosted Apr 3, 2016, 8:27 AM EDT
O. Developer15 pointsPosted Apr 3, 2016, 12:00 PM EDT
O. Developer15 pointsPosted Apr 5, 2016, 11:17 AM EDT
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 += "" + 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(""); } } }1 short reply
O. Developer15 pointsPosted Apr 3, 2016, 4:31 AM EDT