Hi folks, need some help here, I would like to print Avery address labels from my webpage, the data is binded to a Gridview, not really sure of the best way to do this, any help would be much appreciated.
Spec
- Using Word 2007
- Using Microsoft Visual Studio Express 2012
- Using Microsoft Sql Express 2008
Tom
Hi brucey,
Based on your description, my understanding is that you would like to export specify gridview column(address) to word.
If so, I suggest you set the others columns visible as false, and then export it, like this:
protected void btnExportWord_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.doc");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-word ";
StringWriter sw= new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.Columns[0].Visible = false;//there is others columns, we can hidden it befor export
GridView1.Columns[1].Visible = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
Hope it helps.
Best Regards,
Terry Guo
Hi Terry, thank you for your answer, I have implemented the code, just one small problem when I click on the command button everything seems to work ok!!
But when I open the word document its blank!!
Is it possible to export the data from the GridView to a formatted word document i.e. have the data pasted into the address labels within the Word document, as I have not formatted the GridView to the correct label size.
Tom
Hi brucey,
First you need to confirm, you have not hidden all columns of the GridView before export it.
Second, if you put the bind gridview code into if(!IsPostBack), you need to get the data source, and then set it as DataSource of GridView again in export function, like this:
protected void btnExportWord_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.doc");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-word ";
StringWriter sw= new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.DataSource = GetData();//the GetData() is your datasource function.
GridView1.AllowPaging = false;
GridView1.Columns[0].Visible = false;//there is others columns, we can hidden it befor export
GridView1.Columns[1].Visible = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
Hope it helps.
Best Regards,
Terry Guo
沒有留言:
張貼留言