简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM