Showing posts with label Attachement. Show all posts
Export to excel, word in .net
Hi,
Here is the code to export the grid data to excel or word:
// create an enum
public enum ExportType
{
EXCEL,
PDF,
WORD
}
//source can be DataSet, DataTable and etc
public void ExportDataGrid(object source, ExportType type)
{
try
{
// setting temporarily datagrid
DataGrid dg = new DataGrid();
dg.ID = "GridView1";
dg.DataSource = source;
dg.AutoGenerateColumns = true;
dg.AllowPaging = false;
dg.AllowSorting = false;
dg.DataBind();
// export selected format
Response.Clear();
Response.Buffer = true;
switch ((type))
{
case ExportType.EXCEL:
Response.ContentType = "application/ms-excel";
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", "ExportedGridData"));
break;
case ExportType.PDF:
break;
// TODO
case ExportType.WORD:
Response.ContentType = "application/ms-word";
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.doc", "ExportedGridData"));
break;
}
Response.Charset = "";
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
dg.RenderControl(htmlWriter);
Response.Write(stringWriter.ToString());
Response.End();
}
catch (Exception ex)
{
if ((ex.Message != "Thread was being aborted."))
{
Response.Write(("Error occur while exporting excel. For more information:" + ex.Message));
}
}
}
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds = (DataSet)Session["ds"];
if (ds.Tables[0].Rows.Count > 0)
{
ExportDataGrid(ds, ExportType.EXCEL);
}
}
protected void btnExportToWord_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds = (DataSet)Session["ds"];
if (ds.Tables[0].Rows.Count > 0)
{
ExportDataGrid(ds, ExportType.WORD);
}
}
protected void btnExportToPDF_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds = (DataSet)Session["ds"];
if (ds.Tables[0].Rows.Count > 0)
{
ExportDataGrid(ds, ExportType.PDF);
}
}
Here is the code to export the grid data to excel or word:
// create an enum
public enum ExportType
{
EXCEL,
PDF,
WORD
}
//source can be DataSet, DataTable and etc
public void ExportDataGrid(object source, ExportType type)
{
try
{
// setting temporarily datagrid
DataGrid dg = new DataGrid();
dg.ID = "GridView1";
dg.DataSource = source;
dg.AutoGenerateColumns = true;
dg.AllowPaging = false;
dg.AllowSorting = false;
dg.DataBind();
// export selected format
Response.Clear();
Response.Buffer = true;
switch ((type))
{
case ExportType.EXCEL:
Response.ContentType = "application/ms-excel";
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", "ExportedGridData"));
break;
case ExportType.PDF:
break;
// TODO
case ExportType.WORD:
Response.ContentType = "application/ms-word";
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.doc", "ExportedGridData"));
break;
}
Response.Charset = "";
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
dg.RenderControl(htmlWriter);
Response.Write(stringWriter.ToString());
Response.End();
}
catch (Exception ex)
{
if ((ex.Message != "Thread was being aborted."))
{
Response.Write(("Error occur while exporting excel. For more information:" + ex.Message));
}
}
}
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds = (DataSet)Session["ds"];
if (ds.Tables[0].Rows.Count > 0)
{
ExportDataGrid(ds, ExportType.EXCEL);
}
}
protected void btnExportToWord_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds = (DataSet)Session["ds"];
if (ds.Tables[0].Rows.Count > 0)
{
ExportDataGrid(ds, ExportType.WORD);
}
}
protected void btnExportToPDF_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds = (DataSet)Session["ds"];
if (ds.Tables[0].Rows.Count > 0)
{
ExportDataGrid(ds, ExportType.PDF);
}
}
Sending mail containing attachements using CDONTS in .Net
Hi,
Chalo aaj attachement wali mail send karte hai Using CDONTS and asp.net.
Here are the steps to send mail using CDONTS:
1) Add refrence to the CDONTS.dll.
2) Register the dll using:
Start + Run, regsvr32 c:\cdonts.dll
3) Code to sned email:
s1 = FileUpload1.PostedFile.FileName;
Response.Write(s1+” s1
”);
Response.Write(s2 + “ s2
”);
Response.Write(s3 + “ s3
”);
NewMailClass Obj = new NewMailClass();
Obj.MailFormat = 0;
Obj.BodyFormat = 0;
// we can use physical path of any file on the place of FileUpload1.PostedFile.FileName. And ‘my file’ will be the name of the file sent.
Obj.AttachFile(FileUpload1.PostedFile.FileName, “my file”, 1);
Obj.Send(”nitin.sender@anyfakeid.com”, “nitin.reciever@anyrealid.com”, “mail subject”, “mail message body”, 1);
thanks,
Nitin Dhiman.
Chalo aaj attachement wali mail send karte hai Using CDONTS and asp.net.
Here are the steps to send mail using CDONTS:
1) Add refrence to the CDONTS.dll.
2) Register the dll using:
Start + Run, regsvr32 c:\cdonts.dll
3) Code to sned email:
s1 = FileUpload1.PostedFile.FileName;
Response.Write(s1+” s1
”);
Response.Write(s2 + “ s2
”);
Response.Write(s3 + “ s3
”);
NewMailClass Obj = new NewMailClass();
Obj.MailFormat = 0;
Obj.BodyFormat = 0;
// we can use physical path of any file on the place of FileUpload1.PostedFile.FileName. And ‘my file’ will be the name of the file sent.
Obj.AttachFile(FileUpload1.PostedFile.FileName, “my file”, 1);
Obj.Send(”nitin.sender@anyfakeid.com”, “nitin.reciever@anyrealid.com”, “mail subject”, “mail message body”, 1);
thanks,
Nitin Dhiman.
Subscribe to:
Posts
(
Atom
)