请问:数据导出到excel的问题,excel中包含控件
數據導出到excel的問題,excel中顯示的是RadioButton,需要根據頁面中dropdownlist的選中情況使excel中的對應RadioButton選中。怎么办?我现在会将数据导出到excel,但如何操作excel中的radiobutton呢? --------------------编程问答-------------------- //使用GridView 导出excel表格,导出excel对特殊的控件进行模式化处理
public static void PrepareGridViewForExport(Control gv)//模式化特殊元素的函数
{
HyperLink lb = new HyperLink();
LinkButton bt = new LinkButton();
Literal l = new Literal();
string name = String.Empty;
RadioButton rb = new RadioButton();
for (int i = 0; i < gv.Controls.Count; i++)
{
if (gv.Controls[i].GetType() == typeof(HyperLink))
{
l.Text = (gv.Controls[i] as HyperLink).Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(DropDownList))
{
DropDownList dl = gv.Controls[i] as DropDownList;
for (int j= 0; j< this.dl.Items.Count; j++)
{
rb.Text = dl.Items[j].Text;
if (rb.Text == dl.SelectedItem.Text)
rb.Checked = true;
gv.Controls.AddAt(i, l);
}
gv.Controls.Remove(dl); //移除DropDownlist
}
else if (gv.Controls[i].GetType() == typeof(CheckBox))
{
l.Text = (gv.Controls[i] as CheckBox).Checked ? "" : "";
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(ImageButton))
{
l.Text = "图片";
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
if (gv.Controls[i].HasControls())
{
PrepareGridViewForExport(gv.Controls[i]);
}
}
}
//GridView 导出excel数据源
/// <summary>
/// 从GridView 导出excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ibtnExport_Click(object sender, ImageClickEventArgs e)
{
getgvDataSource();//绑定GridView控件gv
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlPathEncode("人员信息表.xls"));
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
PrepareGridViewForExport(gv);//调用模式化特殊元素
gv.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
--------------------编程问答-------------------- 楼上的好像不太试用啊!! --------------------编程问答-------------------- else if (gv.Controls[i].GetType() == typeof(DropDownList))
{
DropDownList dl = gv.Controls[i] as DropDownList;
for (int j= 0; j < this.dl.Items.Count; j++)
{
rb.Text = dl.Items[j].Text;
if (rb.Text == dl.SelectedItem.Text)
rb.Checked = true;
gv.Controls.AddAt(i, rb); //这里错了 应该是rb
}
gv.Controls.Remove(dl); //移除DropDownlist
}
我曾经遇到也是这样的问题,就是这样解决的。。。
--------------------编程问答-------------------- 我的意思是radiobutton(在excel中的名字是OptionButton)已经存在与excel模板中了,而不是将gridview导出到excel,需要更具页面的dropdownlist的不同选项使excel模板中的不同OptionButton选中!!
麻烦再帮忙想想
补充:.NET技术 , ASP.NET