我在页面上有一个GridView。用户可以在GridView中输入数据,然后我在提交事件上下载Word文件。
现在的问题是:如何将特定的列数据下载到该word文件?该word文件还包含来自同一页面的不同字段的其他数据,这些数据正常工作。
以下是GridView的代码:(网格在页面加载时绑定)
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox box1 = (TextBox)GridViewData11.Rows[rowIndex].Cells[1].FindControl("Data1");
TextBox box2 = (TextBox)GridViewData11.Rows[rowIndex].Cells[2].FindControl("Data2");
TextBox box3 = (TextBox)GridViewData11.Rows[rowIndex].Cells[3].FindControl("Data3");
TextBox box4 = (TextBox)GridViewData11.Rows[rowIndex].Cells[4].FindControl("Data4");
box1.Text = dt.Rows[i]["Column1"].ToString();
box2.Text = dt.Rows[i]["Column2"].ToString();
box3.Text = dt.Rows[i]["Column3"].ToString();
box4.Text = dt.Rows[i]["Column4"].ToString();
rowIndex++;
}
}
以下代码用于将数据下载到Word文件:
public void GenerateWordDoc()
{
string strBody = "<html>" +
"<body>" +
"<div> <b> Details </b></div> <br>" +
"<table MAX width=\"701\" height=\"646\" border=\"1\"> <tr><td width=\"191\" height=\"26\" >Name of user </td> <td height=\"86\" > Comments</td><td colspan=\"3\"> </td></tr> </table>" +
"</body>" +
"</html>";
string fileName = “wordfile.doc”;
Response.AppendHeader("Content-Type", "application/msword");
Response.AppendHeader("Content-disposition", "attachment; filename=" + fileName);
Response.Write(strBody);
}