0

这是布局,我有一个 FormView 控件,其中包含来自 SQL 服务器的动态数据。我正在使用 FormView 的只读模式。如何调整 PrepareFormViewForExport 方法以删除文本框并将 FormView 中的值留在后面?现在输出显示标签,但忽略了文本框中的值。

这是单击导出按钮时的 C# 代码。

protected void Bexport_Click(object sender, EventArgs e)
{
    //Clear the Response object
    Response.Clear();
    //set Response header to the Excel filename required (.xls for excel, .doc for word)
    Response.AddHeader("content-disposition", "attachment;filename=ReportOuput.xls");
    Response.Charset = "";
    // If you want the option to open the Excel 
    // file without the save option then un-comment out the line below
    //Response.Cache.SetCacheability(HttpCacheability.NoCache);
    //FOR EXCEL 
    Response.ContentType = "application/vnd.xls";
    //FOR WORD
    //Response.ContentType = "application/vnd.doc";
    //Declare new stringwriter and html writer
    StringWriter stringWrite = new System.IO.StringWriter();
    HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    //Strip out controls from FormView ?takes data too?
    PrepareFormViewForExport(FormView2);
    //render controls data as HTML
    FormView2.RenderControl(htmlWrite);      
    //Clean out the Javascript postbacks etc.
    string html = stringWrite.ToString();
    html = Regex.Replace(html, "</?(a|A).*?>", "");
    StringReader reader = new StringReader(html);
    //Write xls document
    Response.Write(html);
    //end current Response
    HttpContext.Current.Response.End();

}

这是在渲染它以供导出之前从 FormView 中删除控件的方法。

private void PrepareFormViewForExport(Control fv)
{
    LinkButton lb = new LinkButton();
    Literal l = new Literal();
    string name = String.Empty;
    TextBox TB = new TextBox();

    for (int i = 0; i < fv.Controls.Count; i++)
    {
        if (fv.Controls[i].GetType() == typeof(LinkButton))
        {
            l.Text = (fv.Controls[i] as LinkButton).Text;
            fv.Controls.Remove(fv.Controls[i]);
            fv.Controls.AddAt(i, l);
        }
        else if (fv.Controls[i].GetType() == typeof(TextBox))
        {
            l.Text = (fv.Controls[i] as TextBox).Text;
            fv.Controls.Remove(fv.Controls[i]);
            fv.Controls.AddAt(i, l);
        }
        else if (fv.Controls[i].GetType() == typeof(DropDownList))
        {
            l.Text = (fv.Controls[i] as DropDownList).SelectedItem.Text;
            fv.Controls.Remove(fv.Controls[i]);
            fv.Controls.AddAt(i, l);
        }
        else if (fv.Controls[i].GetType() == typeof(CheckBox))
        {
            l.Text = (fv.Controls[i] as CheckBox).Checked ? "True" : "False";
            fv.Controls.Remove(fv.Controls[i]);
            fv.Controls.AddAt(i, l);
        }
        if (fv.Controls[i].HasControls())
        {
            PrepareFormViewForExport(fv.Controls[i]);
        }
    }
}
4

2 回答 2

1

尝试电子表格齿轮第三方组件 http://www.spreadsheetgear.com/

于 2009-09-24T16:24:25.083 回答
1

好吧,我找到了一种解决方法,而不是真正的解决方案。由于我的表单视图处于只读状态,因此我将控件从文本框更改为标签。这使得 html 编写器将数据转储到 Excel 中,而不是尝试复制文本框。我仍然很想知道是否有人知道如何回答原始问题。

于 2009-09-25T15:14:01.763 回答