When saving html table as excel is giving error?
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();
The problem here is you're dealing with a Table object which specifically:
Displays a table on a Web page.
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
精彩评论