将 html 表保存为 excel 时出现错误?
TableRow tr = new TableRow();
TableCell yrs = new TableCell();
yrs.BorderWidth = 1;
yrs.BorderColor = System.Drawing.Color.Black;
yrs.Text = year.Substring(4);
tr.Cells.Add(yrs);
TableCell fname = new TableCell();
fname.BorderWidth = 1;
fname.BorderColor = System.Drawing.Color.Black;
fname.Text = test;
tr.Cells.Add(fname);
TableCell lname = new TableCell();
lname.BorderWidth = 1;
lname.BorderColor = System.Drawing.Color.Black;
lname.Text = test1.ToString();
tr.Cells.Add(lname);
TABULAR.Rows.Add(tr);
Response.Write(TABULAR);
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=test123.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter swWriter = new StringWriter();
HtmlTextWriter htwWriter = new HtmlTextWriter(swWriter);
TABULAR.RenderControl(htwWriter);
Response.Write(swWriter.ToString());
Response.End();
TableRow tr = new TableRow();
TableCell yrs = new TableCell();
yrs.BorderWidth = 1;
yrs.BorderColor = System.Drawing.Color.Black;
yrs.Text = year.Substring(4);
tr.Cells.Add(yrs);
TableCell fname = new TableCell();
fname.BorderWidth = 1;
fname.BorderColor = System.Drawing.Color.Black;
fname.Text = test;
tr.Cells.Add(fname);
TableCell lname = new TableCell();
lname.BorderWidth = 1;
lname.BorderColor = System.Drawing.Color.Black;
lname.Text = test1.ToString();
tr.Cells.Add(lname);
TABULAR.Rows.Add(tr);
Response.Write(TABULAR);
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=test123.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter swWriter = new StringWriter();
HtmlTextWriter htwWriter = new HtmlTextWriter(swWriter);
TABULAR.RenderControl(htwWriter);
Response.Write(swWriter.ToString());
Response.End();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是您正在处理 Table对象具体来说:
并且不以任何方式代表 Excel“表格”或其他任何内容。
通过修改响应标头来声明它是 Excel 文件只会欺骗浏览器,您没有有效的 Excel 文件
The problem here is you're dealing with a Table object which specifically:
and does not in any way represent an Excel 'table' or anything else.
Claiming it is an Excel file by modifying the response headers only tricks the browser, you do not have a valid Excel file