导出excel问题

Z_Lacey 2012-04-11 02:23:57
导出excel,数据全是代码。 我检查过了,里面的代码跟源文件(页面右击-->查看源文件)的代码一模一样。
请问有谁知道这个问题怎么解决?
...全文
97 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mirror然 2012-04-11
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20080728/10/DD7D9AF8-0751-474B-A34C-147D71E28777.html
参考
feiying102743 2012-04-11
  • 打赏
  • 举报
回复
protected void Button1_Click(object sender, EventArgs e)
{
if (this.GridView3.Rows.Count == 0)
{
Response.Write("<script>alert('没有查找到数据,无法导出!')</script>");
}
else
{
//this.GridView3.AllowPaging = false; // 将有分页的GridView中的数据全部导出到Excel
//GridView3Bind();
export("application/ms-excel", "工作人员.xls");
// 换成 export("application/ms-word", "工作人员.doc"); 那么导出的就是Word格式的了.
//this.GridView3.AllowPaging = true;
//GridView3Bind();
}
}
public void export(string FileType, string FileName)
{
string style = "<style>td{mso-number-format:\"\\@\";}</style>";//导入到excel时,保存表里数字列中前面存在的 0 .
for (int i = 0; i < GridView3.Columns.Count; i++)
{
if (GridView3.Columns[i].GetType() == typeof(CommandField))
GridView3.Columns[i].Visible = false;
}
PrepareGridViewForExport(GridView3);
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
this.GridView3.AllowPaging = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
this.GridView3.RenderControl(htw);
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
//在后台中重载VerifyRenderingInServerForm()方法,否则报错为“类型"GridView"的控件"GridView1"必须放在具有 runat=server 的窗体标记内“
}
private void PrepareGridViewForExport(Control gv)//模式化特殊元素 flashcong
{
Literal l = new Literal();
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);
}
if (gv.Controls[i].GetType() == typeof(LinkButton))
{
l.Text = (gv.Controls[i] as LinkButton).Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(DropDownList))
{
l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(CheckBox))
{
l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
if (gv.Controls[i].HasControls())
{
PrepareGridViewForExport(gv.Controls[i]);
}
}
}
这方法能用 我昨天还用的
Z_Lacey 2012-04-11
  • 打赏
  • 举报
回复

你们都误会我的意思了。 不过没关系,问题已经解决了。
tangserver 2012-04-11
  • 打赏
  • 举报
回复
gridview导出EXCEL百度大把的啊
zly_x 2012-04-11
  • 打赏
  • 举报
回复
网上多的是,下个呗。。。。。。
chen_ya_ping 2012-04-11
  • 打赏
  • 举报
回复
npoi
epplus

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧