protected void btnreport_Click(object sender, EventArgs e)
{
try
{
bind();
DataView dv = new DataView(dtt);
System.Text.StringBuilder builder = new System.Text.StringBuilder();
System.Data.DataTable dt = dv.Table;
string headerToExport = string.Empty;
foreach (DataColumn col in dt.Columns)
headerToExport += (char)34 + col.ColumnName + (char)34 + (char)44;
headerToExport.Remove(headerToExport.Length - 1, 1);
headerToExport = headerToExport + Environment.NewLine + Environment.NewLine;
builder.Append(headerToExport);
string body = string.Empty;
foreach (DataRow row in dt.Rows)
{
foreach (object obj in row.ItemArray)
body += (char)34 + obj.ToString() + (char)34 + (char)44;
body.Remove(body.Length - 1, 1);
body = body + Environment.NewLine;
}
builder.Append(body);
builder.Append(Environment.NewLine);
builder.Append(Environment.NewLine);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=Report.csv");
Response.Charset = "";
Response.Write(builder.ToString());
Response.Flush();
Response.End();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally { }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Data;
public partial class TransferedFundExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
protected void BtnGenerateExcel_Click(object sender, EventArgs e)
{
try
{
Bind();
ExportToExcel((DataTable)ViewState["Datatable"]);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally {
ViewState["Datatable"] = null;
}
}
public void ExportToExcel(DataTable dt)
{
if (dt.Rows.Count > 0)
{
string filename = "Test.xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}
public void Bind()
{
using (MySqlConnection ObjCon = new MySqlConnection(new CommonClass().GetConnectionString()))
{
try
{
DataSet ds = new DataSet();
MySqlDataAdapter ada = new MySqlDataAdapter("Select * from CustomerOrder", ObjCon);
ada.Fill(ds);
DataTable dt = new DataTable();
dt.Columns.Add("Cus_orderId");
dt.Columns.Add("Fund_TransId");
dt.Columns.Add("BeneName");
dt.Columns.Add("BeneAcc");
dt.Columns.Add("Bank");
dt.Columns.Add("Branch");
dt.Columns.Add("Amount");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
DataTable dt1 = new DataTable();
MySqlDataAdapter ad1 = new MySqlDataAdapter("SELECT Name,AccountNo,BankName,BranchName FROM Beneficiary Where Id='"+ds.Tables[0].Rows[0][3].ToString()+"'", ObjCon);
ad1.Fill(dt1);
dr[0] = ds.Tables[0].Rows[i][0].ToString();
dr[1] = ds.Tables[0].Rows[i][1].ToString();
dr[2] = dt1.Rows[0][0].ToString();
dr[3] = dt1.Rows[0][1].ToString();
dr[4] = dt1.Rows[0][2].ToString();
dr[5] = dt1.Rows[0][3].ToString();
dr[6] = ds.Tables[0].Rows[i][6].ToString();
}
GridView1.DataSource = dt;
ViewState["Datatable"] = dt;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}
{
try
{
bind();
DataView dv = new DataView(dtt);
System.Text.StringBuilder builder = new System.Text.StringBuilder();
System.Data.DataTable dt = dv.Table;
string headerToExport = string.Empty;
foreach (DataColumn col in dt.Columns)
headerToExport += (char)34 + col.ColumnName + (char)34 + (char)44;
headerToExport.Remove(headerToExport.Length - 1, 1);
headerToExport = headerToExport + Environment.NewLine + Environment.NewLine;
builder.Append(headerToExport);
string body = string.Empty;
foreach (DataRow row in dt.Rows)
{
foreach (object obj in row.ItemArray)
body += (char)34 + obj.ToString() + (char)34 + (char)44;
body.Remove(body.Length - 1, 1);
body = body + Environment.NewLine;
}
builder.Append(body);
builder.Append(Environment.NewLine);
builder.Append(Environment.NewLine);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=Report.csv");
Response.Charset = "";
Response.Write(builder.ToString());
Response.Flush();
Response.End();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally { }
}
}
You Can Also Use This Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Data;
public partial class TransferedFundExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
protected void BtnGenerateExcel_Click(object sender, EventArgs e)
{
try
{
Bind();
ExportToExcel((DataTable)ViewState["Datatable"]);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally {
ViewState["Datatable"] = null;
}
}
public void ExportToExcel(DataTable dt)
{
if (dt.Rows.Count > 0)
{
string filename = "Test.xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}
public void Bind()
{
using (MySqlConnection ObjCon = new MySqlConnection(new CommonClass().GetConnectionString()))
{
try
{
DataSet ds = new DataSet();
MySqlDataAdapter ada = new MySqlDataAdapter("Select * from CustomerOrder", ObjCon);
ada.Fill(ds);
DataTable dt = new DataTable();
dt.Columns.Add("Cus_orderId");
dt.Columns.Add("Fund_TransId");
dt.Columns.Add("BeneName");
dt.Columns.Add("BeneAcc");
dt.Columns.Add("Bank");
dt.Columns.Add("Branch");
dt.Columns.Add("Amount");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
DataTable dt1 = new DataTable();
MySqlDataAdapter ad1 = new MySqlDataAdapter("SELECT Name,AccountNo,BankName,BranchName FROM Beneficiary Where Id='"+ds.Tables[0].Rows[0][3].ToString()+"'", ObjCon);
ad1.Fill(dt1);
dr[0] = ds.Tables[0].Rows[i][0].ToString();
dr[1] = ds.Tables[0].Rows[i][1].ToString();
dr[2] = dt1.Rows[0][0].ToString();
dr[3] = dt1.Rows[0][1].ToString();
dr[4] = dt1.Rows[0][2].ToString();
dr[5] = dt1.Rows[0][3].ToString();
dr[6] = ds.Tables[0].Rows[i][6].ToString();
}
GridView1.DataSource = dt;
ViewState["Datatable"] = dt;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}
No comments:
Post a Comment