Programmatically insert data to word file and save in asp.net - c#

i have one asp.net application , and i want to insert header and some text to word file programmatically. then insert gridview data in same word file and then save here is code
protected void btnPrint_Click(object sender, EventArgs e)
{
GridView1.AllowPaging = false;
GridView1.DataBind();
Response.ClearContent();
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.doc"));
Response.Charset = "";
Response.ContentType = "application/ms-word";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
now , gridview data is inserting in that word doc file, but before gridview i want insert some text in to same file,
please help......

Add the following line
Response.Write("enter your text here");
before
GridView1.RenderControl(htw);
Response.Write(sw.ToString());

Related

How to add dropdown list in gridview excel export

Currently my method is use for export excel file from gridview. Now I want to create dropdown list in some column for each row of excel file. It's possible to do with StringWriter?
There is my code of the method...
string exportFileName = Session["corpUser"].ToString() + "_ProjectExport_" + unixTimestamp + ".xls";
//Create a dummy GridView
GridView dummyGridView = new GridView();
dummyGridView.AllowPaging = false;
dummyGridView.DataSource = dt;
dummyGridView.DataBind();
//Write Dummy Gridview to Excel File
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=" + exportFileName);
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.Unicode;
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);
dummyGridView.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();

error exporting griddata to MS excel from Asp.net webform application

I have use Asp.net webform c#.When exporting grid data to excel using the, I receive the following error message
"the file formate and extension of Mode of transport.xls don't match. the file could be corrupted or unsafe. unless you trust its source, don't open it. do you want to open it anyway"
The data appears fine once you click "Yes".
code:
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
Response.ContentType = "application/ms-excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvmodeoftransport.AllowPaging = false;
for (int i = 0; i < gvmodeoftransport.HeaderRow.Cells.Count; i++)
{
gvmodeoftransport.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
}
gvmodeoftransport.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();

Gridview to Excel file - save output instead of showing it

I have this code below which takes my GridView, populates it, and generates and opens an Excel file with the griddata in my browser.
What if I want to save that file directly to my server or attach it to an email message, instead of the display prompt that automatically pops up.
Can I do that with small modifications to the script?
protected void btnExcelExport_Click(object sender, EventArgs e)
{
HtmlForm form = new HtmlForm();
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", "Student.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
BindGridDetails(GridView1);
form.Attributes["runat"] = "server";
form.Controls.Add(GridView1);
this.Controls.Add(form);
form.RenderControl(hw);
string style = #"<!--mce:2-->";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
you can save your grid data as a Comma Separated Value file on the server and email or save it directly on your server.
CSV files can be opened using Excel.

Add table to second sheet of Excel using C#

I am dumping the data from a table to an excel sheet which is created during run time.I have the file path in Session["Fullpath"]. I am able to create the excel with contains the data of table1.Now, I have one more table table2.I want to dump the data from table table2 to the second sheet of the same excel.
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename= " + Session["Fullpath"]);
Response.ContentType = "application/ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
table.RenderControl(htw);
Response.Output.Write(sw.ToString());
System.IO.File.WriteAllText(Session["Fullpath"].ToString(), sw.ToString());
Response.Flush();
Response.End();

Image not visible during table export to MS WORD in asp.net

I have the following code with me to export table present on aspx page with id "UserDetails".
When I export this table to MS Excel file images with absolute path are visible but when I export the table to MS WORD (.doc) file then the same images are not visible.
I searched a lot on net but nothing was helpful.
Please help me out to solve this issue.
protected void btnExport_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
//Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
Response.ContentType = "application/vnd.ms-word";
Response.AddHeader("Content-Disposition", "attachment; filename=WORK_ORDER.doc");
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Charset = "";
EnableViewState = false;
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter html = new System.Web.UI.HtmlTextWriter(writer);
UserDetails.RenderControl(html);
Response.Write(writer);
Response.End();
}

Categories

Resources