接上篇文章,导出html文件,我对这部分代码进行优化,提交到github上,为初始版本,后面根据用户需求与讨论,会不断更新优化功能
导出Html 表格文件,简单易用
https://github.com/CollectBugs/EasyHtml
开发此项目的灵感来源于一次项目开发,发现导出html表格文件需求,比较常见,市面上开源、成熟、免费的方案没有,如果每一家公司,都从零开始开发,耗时又费力,导致开发周期变长,不如大家开源共建此项目,让后来人站在巨人的肩膀上进行开发与维护,共享资源,其乐而不为,希望大家多多给与关注与讨论哦!
每天不断更,精彩不停止,明天见,我是行者
记得留个关注、点赞、评论哟,让我们一起去看星辰大海,品味代码人生
、nuget 引用
Select.HtmlToPdf
2、方法
using SelectPdf;using System.Collections.Specialized;using System.IO;using System.Web;namespace BQoolCommon.Helpers.File{public class WebToPdf{public WebToPdf(){//SelectPdf.GlobalProperties.LicenseKey = "your-license-key";}/// <summary>/// 將 Html 轉成 PDF,並儲存成檔案/// </summary>/// <param name="html">html</param>/// <param name="fileName">絕對路徑</param>public void SaveToFileByHtml(string html, string fileName){var doc = SetPdfDocument(html);doc.Save(fileName);}/// <summary>/// 傳入 Url 轉成 PDF,並儲存成檔案/// </summary>/// <param name="url">url</param>/// <param name="fileName">絕對路徑</param>/// <param name="httpCookies">Cookies</param>public void SaveToFileByUrl(string url, string fileName, NameValueCollection httpCookies){var doc = SetPdfDocument(url, httpCookies);doc.Save(fileName);}/// <summary>/// 將 Html 轉成 PDF,並輸出成 byte[] 格式/// </summary>/// <param name="html">html</param>/// <returns></returns>public byte[] GetFileByteByHtml(string html){var doc = SetPdfDocument(html);return doc.Save();}/// <summary>/// 傳入 Url 轉成 PDF,並輸出成 byte[] 格式/// </summary>/// <param name="url">url</param>/// <param name="httpCookies">Cookies</param>/// <returns></returns>public byte[] GetFileByteByUrl(string url, NameValueCollection httpCookies){var doc = SetPdfDocument(url, httpCookies);return doc.Save();}/// <summary>/// 將 Html 轉成 PDF,並輸出成 Stream 格式/// </summary>/// <param name="html">html</param>/// <returns></returns>public Stream GetFileStreamByHtml(string html){var doc = SetPdfDocument(html);var pdfStream = new MemoryStream();doc.Save(pdfStream);pdfStream.Position = 0;return pdfStream;}/// <summary>/// 傳入 Url 轉成 PDF,並輸出成 Stream 格式/// </summary>/// <param name="html">html</param>/// <returns></returns>public Stream GetFileStreamByUrl(string url, NameValueCollection httpCookies){var doc = SetPdfDocument(url, httpCookies);var pdfStream = new MemoryStream();doc.Save(pdfStream);pdfStream.Position = 0;return pdfStream;}private PdfDocument SetPdfDocument(string html){var converter = new HtmlToPdf();converter.Options.WebPageWidth = 1200;html = HttpUtility.HtmlDecode(html);return converter.ConvertHtmlString(html);}private PdfDocument SetPdfDocument(string url, NameValueCollection httpCookies){var converter = new HtmlToPdf();converter.Options.WebPageWidth = 1200;if (httpCookies != && httpCookies.Count != 0){converter.Options.HttpCookies.Add(httpCookies);}return converter.ConvertUrl(url);}}}
目中用到需要将页面数据导出 就想到了IE的直接导出。
将用到的示例给大家分享一下:
//将表格中的数据导出到excel中
function exportDataToExcel(tid){
var curTbl = $('#tid');
var oXLn;
try{
oXLn = new ActiveXObject("Excel.Application"); //创建对象excel
}catch(e){
alert("无法启动Excel!\n\n如果您确信您的电脑中已经安装了Excel,"+"那么请调整IE的安全级别。\n\n具体操作:\n\n"+"工具 → Internet选项 → 安全 → 自定义级别 → 对没有标记为安全的ActiveX进行初始化和脚本运行 → 启用");
return false;
}
var oWBs = oXLn.Workbooks.Add(); //获取workbook对象
var oSheet1 = oWBs.ActiveSheet;//激活当前sheet
var sel = document.body.createTextRange();
sel.moveToElementText(curTbl); //把表格中的内容移到TextRange中
sel.select(); //全选TextRange中内容
sel.execCommand("Copy");//复制TextRange中内容
oSheet1.Paste();//粘贴到活动的EXCEL中
oXLn.Visible = true; //设置excel可见属性
var fname = oXLn.Application.GetSaveAsFilename("将table导出到excel.xls", "Excel Spreadsheets (*.xls), *.xls");
oWBs.SaveAs(fname);
oWBs.Close();
oXLn.nQuit();
}
注意:1.电脑必须安装微软的excel。
2.需要将浏览器的active控件设置为启用。
*请认真填写需求信息,我们会在24小时内与您取得联系。