htM文件如何转成EXCEL文档
展开全部
1、首先把html的内容传到后台,由于是Html的内容应该选择安全的验证关掉
2、绑定一个<a>标签到后面的方法(这样能够保证服务器响应你的提交动作),ajax不能实现这个功能
3、把你的样式放在一个css文件里面,这样当你去实现转换的时候你会有与页面相同的样式
4、在方法中写入如下代码就能够实现下载
Response.ContentType = "application/force-download";
Response.AddHeader("content-disposition",
"attachment; filename=" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
Response.Write("<head>");
Response.Write("<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
#region 样式的读取
string fileCss = Server.MapPath("~/Content/CalCSS/tableCss.css");
string cssText = string.Empty;
StreamReader sr = new StreamReader(fileCss);
var line = string.Empty;
while ((line = sr.ReadLine()) != null)
{
cssText += line;
}
sr.Close();
Response.Write("<style>" + cssText + "</style>");
#endregion
Response.Write("<!--[if gte mso 9]><xml>");
Response.Write("<x:ExcelWorkbook>");
Response.Write("<x:ExcelWorksheets>");
Response.Write("<x:ExcelWorksheet>");
Response.Write("<x:Name>Report Data</x:Name>");
Response.Write("<x:WorksheetOptions>");
Response.Write("<x:Print>");
Response.Write("<x:ValidPrinterInfo/>");
Response.Write("</x:Print>");
Response.Write("</x:WorksheetOptions>");
Response.Write("</x:ExcelWorksheet>");
Response.Write("</x:ExcelWorksheets>");
Response.Write("</x:ExcelWorkbook>");
Response.Write("</xml>");
Response.Write("<![endif]--> ");
Response.Write(strHtml1);//这里是前台页面的HTML
Response.Flush();
Response.End();
不能用Ajax调用来完成,因为Ajax不会刷新页面
2、绑定一个<a>标签到后面的方法(这样能够保证服务器响应你的提交动作),ajax不能实现这个功能
3、把你的样式放在一个css文件里面,这样当你去实现转换的时候你会有与页面相同的样式
4、在方法中写入如下代码就能够实现下载
Response.ContentType = "application/force-download";
Response.AddHeader("content-disposition",
"attachment; filename=" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
Response.Write("<head>");
Response.Write("<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
#region 样式的读取
string fileCss = Server.MapPath("~/Content/CalCSS/tableCss.css");
string cssText = string.Empty;
StreamReader sr = new StreamReader(fileCss);
var line = string.Empty;
while ((line = sr.ReadLine()) != null)
{
cssText += line;
}
sr.Close();
Response.Write("<style>" + cssText + "</style>");
#endregion
Response.Write("<!--[if gte mso 9]><xml>");
Response.Write("<x:ExcelWorkbook>");
Response.Write("<x:ExcelWorksheets>");
Response.Write("<x:ExcelWorksheet>");
Response.Write("<x:Name>Report Data</x:Name>");
Response.Write("<x:WorksheetOptions>");
Response.Write("<x:Print>");
Response.Write("<x:ValidPrinterInfo/>");
Response.Write("</x:Print>");
Response.Write("</x:WorksheetOptions>");
Response.Write("</x:ExcelWorksheet>");
Response.Write("</x:ExcelWorksheets>");
Response.Write("</x:ExcelWorkbook>");
Response.Write("</xml>");
Response.Write("<![endif]--> ");
Response.Write(strHtml1);//这里是前台页面的HTML
Response.Flush();
Response.End();
不能用Ajax调用来完成,因为Ajax不会刷新页面
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |