通过http输出流的方式从将html写入到Excel
private void HtmlResponseToExecl() { //将数据(html)导入到Excel中 Response.Charset = "gb2312"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); string filename = "../Execl/test"; //解决乱码问题 filename = HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8); Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + ".xls"); Response.ContentType = "application/vnd.ms-excel"; Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">"); Response.Write("<body>"); Response.Write("<table border=1px>"); //导入Excel表头信息 Response.Write("<tr style='background-color: gray;'><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td></tr>"); Response.Write("<tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td></tr>"); Response.Write("<tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td></tr>"); Response.Write("<tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td></tr>"); Response.Write("<tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td></tr>"); Response.Write("</table>"); Response.Write("</body></html>"); Response.Flush(); Response.End(); }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步