高分求HTML的TABLE导出EXCEL,解决立即结贴!

wzqinyfwu 2004-09-06 12:27:03
我将查询结果显示在表格中,要实现点"ToExcel"按钮,表格中的数据以excel的格式显示,但不需要提示保存或下载,只是直接在ie中以excel的格式打开,按后退还可以返回刚才的查询结果页面。
有以下几点要注意,
1.我的表格是在.cs文件中写出来的,即Response.Write("<table id='table1' border=1>");
2.不需要提示保存或下载,直接在ie中以excel的格式打开
3.按后退还可以返回刚才的查询结果页面
...全文
1023 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
lclonger 2010-06-23
  • 打赏
  • 举报
回复
来学习下
joecfan 2004-09-07
  • 打赏
  • 举报
回复
点击 ToExcel,在服务器端生成一个excel文件,然后把他读进来,这样就在ie中打开了,然后可以把它删掉。
不知可否?
guibai 2004-09-07
  • 打赏
  • 举报
回复
Dim sw As New System.IO.StringWriter
sw.Write("<html><body><TABLE border =1><tr><td>aaa</td><td>bbb</td></tr><tr><td colspan=2>afasdfasfasdfsadfsadfasf</td></tr></TABLE></body></html>")
sw.WriteLine()
Dim htw As New HtmlTextWriter(sw)
dtlProducts.RenderControl(htw)
Response.ContentType = "application/vnd.ms-excel"
' Response.Charset = ""

Response.Write(sw.ToString())
Response.End()
wzqinyfwu 2004-09-07
  • 打赏
  • 举报
回复
导出后中文竟然还是乱码,没办法了
nctonny 2004-09-07
  • 打赏
  • 举报
回复
我也是同样的问题!怎么不行?
jamzh 2004-09-06
  • 打赏
  • 举报
回复
学习1~
wzqinyfwu 2004-09-06
  • 打赏
  • 举报
回复
实在不行的话,大家告诉我怎么把tablename传进去,
比如,我的导出的事件中ToExcel(Tablename),但我的Table是在.cs文件出Response出来的,Response.Write("<table id='table1' border=1>");
如果直接ToExcel(table1),编译通不过,大家谁知道有什么办法?谢谢!
wzqinyfwu 2004-09-06
  • 打赏
  • 举报
回复
怎么没有合适的呢,请大家帮帮忙吧,谢谢了
tianwenjie 2004-09-06
  • 打赏
  • 举报
回复
createDataSet(ref report);
DataSet ds =new DataSet();
ds =report;
ds.Namespace="";
//Response.ContentType = "text/html";
Response.Clear();
string httpHeader="attachment;filename=backup.xls";
Response.AppendHeader("Content-Disposition", httpHeader);
Response.Charset = "UTF-8";
Response.BufferOutput = true;
XmlDataDocument xdd =new XmlDataDocument(ds);
System.Xml.Xsl.XslTransform xt =new XslTransform();
if(type=="0")
{
xt.Load(Server.MapPath("1.xslt"));
}
else
{
xt.Load(Server.MapPath("2.xslt"));
}
xt.Transform(xdd,null,Response.OutputStream);
Response.End();
goody9807 2004-09-06
  • 打赏
  • 举报
回复
string path = Server.MapPath(this.xlfile.Text+".xls");

System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString());

// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/ms-excel";

// 把文件流发送到客户端
Response.WriteFile(file.FullName);
// 停止页面的执行

Response.End();
nswhy 2004-09-06
  • 打赏
  • 举报
回复
在服务器段生成后可以直接连接到该文件,然后打开了
2002pine 2004-09-06
  • 打赏
  • 举报
回复
Response.clear();
Response.Write("<table><tr>.....<table">);
Response.ContentType="Application/vnd.ms-excel";
Response.AddHeader("Content-Disposition","attachment;filename=ReqForQuote.xls");
Response.End();
nswhy 2004-09-06
  • 打赏
  • 举报
回复
function toExcel(tablename)
//导出到excel
{
var mysheet=new ActiveXObject("OWC.Spreadsheet");
with(mysheet)
{
DataType = "HTMLData";
HTMLData =tablename.outerHTML;
try{

ActiveSheet.Cells(1,1).value="";
ActiveSheet.Cells(2,1).value="";
// ActiveSheet.Cells(34,1).value="导出完毕";

ActiveSheet.Export("c:\\前6周发货预测系数表.xls", 0);

alert('导出完毕');
window.close();
};
catch (e){
alert('导出Excel表失败,请确定已安装Excel2000(或更高版本),并且没打开同名xls文件');
window.close();
};
}
}
softchao 2004-09-06
  • 打赏
  • 举报
回复
Excel.Application oExcel;
Excel.Workbook oBook;
Object oMissing = System.Reflection.Missing.Value;
oExcel = new Excel.Application();
oBook = oExcel.Workbooks.Add(oMissing);
for (int i=1;i<=4;i++)
{
oExcel.Cells[i,1]=i.ToString();
oExcel.Cells[i,2]="'bbb2";
oExcel.Cells[i,3]="'ccc3";
oExcel.Cells[i,4]="'aaa4";
}
oBook.Saved = true;
oExcel.UserControl = false;
string mm=Server.MapPath(".")+"\\aa.xls";//服务器保存地址
oExcel.ActiveWorkbook.SaveCopyAs (mm);
oExcel.Quit();
Response.Redirect ("aa.xls");
smallMage 2004-09-06
  • 打赏
  • 举报
回复
UP
zl194 2004-09-06
  • 打赏
  • 举报
回复
up
spland 2004-09-06
  • 打赏
  • 举报
回复
Dim dgrdProducts As DataGrid = CType(Session("dgrdProducts"), DataGrid)
If dgrdProducts Is Nothing Then
Response.Write("<br>Error: Session('dgedProducts') Is Nothing!")
Response.End()
End If

Dim sw As New StringWriter()
Dim htw As New HtmlTextWriter(sw)
dgrdProducts.RenderControl(htw)
Response.ContentType = "application/vnd.ms-excel"
Response.Write(sw.ToString())
Response.End()

62,050

社区成员

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

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

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

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