1.html转pdf
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// HtmlToPdf 的摘要说明 /// 使用前需要安装事项 /// 安装软件见 Lib/wkhtmltox-0.12.4_msvc2015-win64.exe /// </summary> public class HtmlToPdf { /// <summary> /// 根据实际安装目录修改 /// </summary> private string exePath = @"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe"; public HtmlToPdf() { // // TODO: 在此处添加构造函数逻辑 // } public bool ToPdf(string url,string outPdfPath) { try { string path = url + " " + outPdfPath; //执行wkhtmltopdf.exe System.Diagnostics.Process p = System.Diagnostics.Process.Start(exePath, path); //若不加这一行,程序就会马上执行下一句而抓不到文件发生意外:System.IO.FileNotFoundException: 找不到文件 ''。 p.WaitForExit(); return true; } catch (Exception) { } return false; //因为Web 是多线程环境,避免甲产生的文件被乙下载去,所以档名都用唯一 // string fileNameWithOutExtention = Guid.NewGuid().ToString(); } }
使用需要下载并安装wkhtmltopdf.exe文件
HtmlToPdf pdf = new HtmlToPdf(); var vl = pdf.ToPdf("要保存的url地址需要包含http://xxxxxx", "PDF要保存的文件路径+文件名称");
2.读取html内容
/// <summary> /// 读取ht.html文件内容 /// </summary> /// <returns></returns> private string GetBidTempStrng() { StringBuilder sbHtml = new StringBuilder(); // 读取模板替换数据 var path = Server.MapPath("~/contractFile/ht.html"); using (Stream inStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read)) using (StreamReader outStream = new StreamReader(inStream, Encoding.Default)) { while (!outStream.EndOfStream) { sbHtml.Append(outStream.ReadLine()); } } var html = sbHtml.ToString(); return html; }
3. webform中将html文件保存为word文件
#region 转换为Word文档样式 //Response.ContentType = "application/msword"; Response.ContentType = "application/msword"; //Response.Write(Encoding.UTF8.GetBytes(sb.ToString())); string fileName = "xxx.doc";//客户端保存的文件名 string filePath = Server.MapPath("~/contractFile/ht.html");//路径 //以字符流的形式下载文件 FileStream fs = new FileStream(filePath, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); //Response.ContentType = "application/octet-stream"; //通知浏览器下载文件而不是打开 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); #endregion
4.ie中将文件保存为word
<script type="text/javascript" language="javascript"> //指定页面区域内容导入Word function exportToWord(controlId) { var control = document.getElementById(controlId); try { var oWD = new ActiveXObject("Word.Application"); var oDC = oWD.Documents.Add("", 0, 1); var oRange = oDC.Range(0, 1); var sel = document.body.createTextRange(); try { sel.moveToElementText(control); } catch (notE) { alert("导出数据失败,没有数据可以导出。"); window.close(); return; } sel.select(); sel.execCommand("Copy"); oRange.Paste(); oWD.Application.Visible = true; //window.close(); } catch (e) { alert("导出数据失败,需要在客户机器安装Microsoft Office Word(不限版本),将当前站点加入信任站点,允许在IE中运行ActiveX控件。"); try { oWD.Quit(); } catch (ex) { } //window.close(); } } </script>
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <input type="button" value="导出到WORD" onclick="return exportToWord('TestDiv')" /> <div id="TestDiv"> <div> <p style="margin-top: 23px; margin-right: 0; margin-bottom: 22px; margin-left: 0; text-align: center; line-height: 27px; page-break-after: avoid"> <strong><span style="font-size: 21px; font-family: 宋体">*采购合同</span></strong> </p> <p style="text-align: right; line-height: 29px"> <span style="font-size: 16px; font-family: 宋体">合同编号:(自动抓取项目编号)@xmbh@</span> </p> <p style="text-indent: 32px; line-height: 150%"> <span style="font-size: 16px; line-height: 150%; font-family: 宋体">采购人(甲方):@cgr@(自动抓取)</span> </p> <p style="text-indent: 32px; line-height: 150%"> <span style="font-size: 16px; line-height: 150%; font-family: 宋体">供应商(乙方):@gys@</span> <span style="font-size: 16px; line-height: 150%; font-family: 宋体">(自动抓取)</span> </p> <div style="height: 27px"> </div> <p style="text-indent: 32px; line-height: 27px"> <span style="font-size: 16px; font-family: 宋体">根据《**采购法》、《*合同法》及</span><span style="font-size: 16px; font-family: ' times new roman' , serif">@xmmc@</span><span style="font-size: 16px; font-family: 宋体">(自动抓取)</span><span style="font-size: 16px; font-family: 宋体">采购项目(项目编号:</span><span style="font-size: 16px; font-family: 宋体">@xmbh@自动抓取</span><span style="font-size: 16px; font-family: 宋体">)的《竞价文件》、乙方的《报价资料》及《成交通知书》,甲、乙双方同意签订本合同。双方同意共同遵守如下条款:</span> </p> <p style="margin-top: 17px; margin-right: 0; margin-bottom: 17px; margin-left: 0; text-indent: 32px; line-height: 27px; page-break-after: avoid"> <a></a><a><strong><span style="font-size: 16px; font-family: 黑体">一、合同货物</span></strong></a><strong><span style="font-size: 16px; font-family: 黑体">(系统自动抓取)</span></strong> </p> <style> .show td { border: 1px solid #ccc; border-top: none; margin: -1px; padding: 10px; border-left: none; margin-top: -1px; } .show tr:first-child td { border-top: 1px solid #ccc; text-align: center; } .show tr:last-child td { border-top: 1px solid #ccc; text-align: center; } .show tr td:first-child { border-left: 1px solid #ccc; } </style> <table cellpadding="0" cellspacing="0" class="show" style="width: 100%"> <tr> <td>货物品名</td> <td>规格型号</td> <td>详细技术参数</td> <td>单位</td> <td>数量</td> <td>单价</td> <td>总价(万元)</td> </tr> <tr> <td>小火车</td> <td>001</td> <td>详细技术参数</td> <td>辆</td> <td>1</td> <td>120</td> <td>0.00120(万元)</td> </tr> <tr> <td>小火车</td> <td>001</td> <td>详细技术参数</td> <td>辆</td> <td>1</td> <td>120</td> <td>0.00120(万元)</td> </tr> <tr> <td>合计</td> <td colspan="6"><span style="margin-left: 5px;">xxxx元(大写)</span></td> </tr> </table> <p style="margin-top: 17px; margin-right: 0; margin-bottom: 17px; margin-left: 0; text-indent: 32px; line-height: 27px; page-break-after: avoid"> <a></a><a><strong><span style="font-size: 16px; font-family: 黑体">二、合同总价</span></strong></a> </p> </div> </div> </body> </html>
5.html table导出excel
var idTmr; function ExportToExcel(tableid) { if (getExplorer() == 'ie') { var curTbl = document.getElementById(tableid); var oXL; try { oXL = new ActiveXObject("Excel.Application"); //创建AX对象excel } catch (e) { alert("无法启动Excel!\n\n如果您确信您的电脑中已经安装了Excel," + "那么请调整IE的安全级别。\n\n具体操作:\n\n" + "工具 → Internet选项 → 安全 → 自定义级别 → 对没有标记为安全的ActiveX进行初始化和脚本运行 → 启用"); return false; } var oWB = oXL.Workbooks.Add(); var xlsheet = oWB.Worksheets(1); var sel = document.body.createTextRange(); sel.moveToElementText(curTbl); sel.select(); sel.execCommand("Copy"); xlsheet.Paste(); oXL.Visible = true; try { var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls"); } catch (e) { print("Nested catch caught " + e); } finally { oWB.SaveAs(fname); oWB.Close(savechanges = false); oXL.Quit(); oXL = null; idTmr = window.setInterval("Cleanup();", 1); } } else { tableToExcel(tableid) } } //获取浏览器种类 function getExplorer() { var explorer = window.navigator.userAgent; //ie if (explorer.indexOf("MSIE") >= 0) { return 'ie'; } //firefox else if (explorer.indexOf("Firefox") >= 0) { return 'Firefox'; } //Chrome else if (explorer.indexOf("Chrome") >= 0) { return 'Chrome'; } //Opera else if (explorer.indexOf("Opera") >= 0) { return 'Opera'; } //Safari else if (explorer.indexOf("Safari") >= 0) { return 'Safari'; } } function Cleanup() { window.clearInterval(idTmr); CollectGarbage(); } //导出Excel var tableToExcel = (function () { var uri = 'data:application/vnd.ms-excel;base64,', template = '<html><head><meta charset="UTF-8"></head><body><table>{table}</table></body></html>', base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }, format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) } return function (table, name) { if (!table.nodeType) table = document.getElementById(table) var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML } window.location.href = uri + base64(format(template, ctx)) } })()
<span class="Excel" id="Excel">导出excel</span> <div id="details"> <form> <table cellpadding="0" cellspacing="0" border="0" class="stdtable"> <thead> <tr> <th class="head0" style="width: 30px">序号</th> <th class="head1">采购项目名称</th> <th class="head1" style="width: 150px">上报时间</th> <th class="head0" style="width: 70px">项目时间</th> <th class="head0" style="width: 70px">提交时间</th> <th class="head0" style="width: 70px">上报人员</th> <th class="head0" style="width: 70px">提交人员</th> <th class="head0" style="width: 70px">错误原因</th> </tr> </thead> <tbody> </tbody> </table> </form> </div>
jQuery("#Excel").click(function () { ExportToExcel("details"); });