C# 前端無插件打印導(dǎo)出實(shí)現(xiàn)方式詳解
打印
打印導(dǎo)出分布頁
@model List<界面的數(shù)據(jù)模型類> @using WingSoft; @using Newtonsoft.Json; <style type="text/css"> .modal-content { width: 800px; } .modal-body { height: 400px; } </style> <script type="text/javascript"> $(function () { if (@ViewBag.pe== 0) {//打印 let content = '' for (var i = 0; i < $(".boxDiv").length; i++) { content += $(".boxDiv").eq(i).html() + '<div style="page-break-after: always;"></div>' } printFunc(content); } else {//導(dǎo)出(將所有數(shù)據(jù)導(dǎo)出到一個(gè)excel,多個(gè)工作表的形式) let contentArray = []; for (var i = 0; i < $(".boxDiv").length; i++) { contentArray.push({ html: $(".boxDiv").eq(i).html(), name: "xx記錄表" + (i + 1) }); } exportToExcelBatch('xx記錄表',contentArray) } }); </script> <div class="modal fade" id="modal" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> <img src="~/Images/icon_closed.png" /> </button> </div> <div class="modal-body"> @for (var i = 0; i < Model.Count; i++) { <div class="boxDiv"> <style> .boxDiv table { width: 100%; display: flex; flex-direction: column; align-items: center; } .printTable { width: 100%; font-family: "宋體"; border: 0px; color: #333; font-size: 12px; } .printTable th { font-weight: 600; font-size: 14px; } .printTable td, .printTable th { text-align: center; padding: 3px 3px; } .content td { border: 1px solid #4a4a4a; } .tabletitle1 { font-size: 20px; font-weight: bold; } .tabletitle2 { font-size: 18px; font-weight: bold; position: relative; } .contenleft { text-align: left !important; } .contenright { text-align: right !important; } .boxcontent { height: 30px; font-size: 14px; } .footertext { font-size: 14px; } .printTable td, .printTable th { border: 1px solid #E5E5E5; padding:3px; font-size:14px; } .printTable thead th{ text-align:center; } </style> <div class="modal-title" style="font-size:18px;text-align:center;">@Model[i].SchoolName 經(jīng)營成本</div> <div style="margin:10px 0"> <span>食堂名稱:@Model[i].CanteenName</span> <span style="margin-left:30%">登記時(shí)間:@Model[i].ForDate</span> </div> <table class="printTable" border=0 cellspacing=0 cellpadding=0 style="border-collapse:collapse;width:100%;"> <thead> <tr> <th>序號(hào)</th> <th>成本名稱</th> <th>成本金額(元)</th> <th>備注</th> </tr> </thead> <tbody style="width:100%;"> @{ int index = 1; } @foreach (var item in Model[i].CostItemList) { <tr> <td>@(index++)</td> <td>@item.Name</td> <td>@item.TotalMoney</td> <td>@item.Remark</td> </tr> } </tbody> </table> </div> } </div> <div class="modal-footer"> <button type="button" class="btn2 btn_90_28" data-dismiss="modal">關(guān)閉</button> </div> </div> </div> </div>
打印js
// 打印 function printFunc(data) { loadStart(); var iframeHtml = document.createElement("iframe"); iframeHtml.id = "iframePrintBox"; iframeHtml.style.width = "0"; iframeHtml.style.height = "0"; document.body.appendChild(iframeHtml); iframeHtml.srcdoc = '<!DOCTYPE html>' + data + "<script type='text\/javascript'>window.onload = function(){window.print();window.onafterprint = ()=>{window.parent.close();}}<\/script>"; loadEnd() }
導(dǎo)出
//多sheet導(dǎo)出 function exportToExcelBatch(sheetName, exportArr) { var fileName_g, sheetsName_g, mainHtml_g, sheetHtml_g // 導(dǎo)出函數(shù) sheetsName_g = getSheetsName() //獲取到每個(gè)sheet的名稱集合 fileName_g = sheetName + '.xlsx' //外部導(dǎo)出名稱 sheetHtml_g = getSheetXml() //得到每一個(gè)sheet的xml片段 mainHtml_g = getMainXml() //導(dǎo)出的主體xml片段 let XLSData = 'data:application/vnd.ms-excelbase64,' + window.btoa(window.unescape(encodeURIComponent(mainHtml_g))) // 下載 download(XLSData) // 下載 function download(base64data) { let blob if (window.navigator.msSaveBlob) { blob = base64ToBlob(base64data) window.navigator.msSaveBlob(blob, sheetName + '.xlsx') return false } let a = document.createElement("a") if (window.URL.createObjectURL) { blob = base64ToBlob(base64data) a.href = window.URL.createObjectURL(blob) a.download = fileName_g a.click() return } a.href = base64data a.download = fileName_g a.click() } // 獲取sheet名稱 function getSheetsName() { let sheetsName = [] exportArr.forEach((item) => { sheetsName.push(item.name) }) return sheetsName } // 創(chuàng)建文件流 function base64ToBlob(base64Data) { let arr = base64Data.split(',') let mime = arr[0].match(/:(.*?)/)[1] let bstr = atob(arr[1]) let n = bstr.length let u8arr = new Uint8ClampedArray(n) while (n--) { u8arr[n] = bstr.charCodeAt(n) } return new Blob([u8arr], { type: mime }) } /** * 文件下載 * @param {any} url 文件路徑 * @param {any} fileName 文件名稱 */ function downloadFile(url, fileName) { if (!url || !fileName) return false; let aUrl = url; const x = new XMLHttpRequest(); x.open('GET', aUrl, true); x.responseType = 'blob'; x.onload = function (e) { const url = window.URL.createObjectURL(x.response); const elink = document.createElement('a'); elink.href = url; elink.target = '_self'; // 當(dāng)前頁 target打開新頁面 elink.download = `${fileName}`; elink.style.display = 'none'; document.body.appendChild(elink); elink.click(); document.body.removeChild(elink); }; x.send(); } // 獲取所有xml代碼 function getMainXml() { let mainHtml = '' let sheets = '' if (sheetsName_g.length > 0) { for (let i = 0; i < sheetsName_g.length; i++) { let name = sheetsName_g[i] let sheetItem = ` <x:ExcelWorksheet> <x:Name>${name}</x:Name> <x:WorksheetSource HRef=3D"export/sheet${name}.xml"/> </x:ExcelWorksheet>` sheets += sheetItem } } mainHtml = `MIME-Version: 1.0 X-Document-Type: Workbook Content-Type: multipart/related; boundary="----memo----" ------memo---- Content-Location: file:///C:/0E8D990C/export.xml Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset="us-ascii" <html xmlns:o=3D"urn:schemas-microsoft-com:office:office" xmlns:x=3D"urn:schemas-microsoft-com:office:excel" xmlns=3D"http://www.w3.org/TR/REC-html40"> <head> <xml> <o:DocumentProperties> <o:Author>ijovo</o:Author> <o:LastAuthor>ijovo</o:LastAuthor> <o:Company>ijovo</o:Company> <o:Version>1.0</o:Version> </o:DocumentProperties> </xml> <!--[if gte mso 9]> <xml> <x:ExcelWorkbook> <x:ExcelWorksheets>${sheets} </x:ExcelWorksheets> </x:ExcelWorkbook> </xml> <![endif]--> </head> </html>` + sheetHtml_g + ` ------memo------` return mainHtml } // 獲取每個(gè)sheet的xml代碼} function getSheetXml() { let sheetHtml = '' let sheets = '' for (let i = 0; i < exportArr.length; i++) { let name = exportArr[i].name // MIME要求格式必須頂格……所以這里排版比較亂…… let sheetItem = ` ------memo---- Content-Location: file:///C:/0E8D990C/export/sheet${name}.xml Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset="us-ascii" <html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"> <head> <xml> <x:WorksheetOptions> <x:ProtectContents>False</x:ProtectContents> <x:ProtectObjects>False</x:ProtectObjects> <x:ProtectScenarios>False</x:ProtectScenarios> </x:WorksheetOptions> </xml> <style> <!-- @page {mso-footer-data:"&C\\7B2C &P \\9875\\FF0C\\5171 &N \\9875"; margin:0.748in 0.195in 0.748in 0.195in; mso-header-margin:0.51in; mso-footer-margin:0.51in;} --> </style> </head> <body>` let table = `<table border=0 cellspacing=0 cellpadding=0 width="100%" bordercolor="#000000" style="border-collapse:collapse">${exportArr[i].html}</table>` sheetItem += table + ` </body> </html>` sheets += sheetItem } sheetHtml = sheets return sheetHtml } }
單表格導(dǎo)出
// 導(dǎo)出表格 function exportToExcel(sheetName, html) { var uri = 'data:application/vnd.ms-excel;base64,'; var template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"' + 'xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>' + '<x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets>' + '</x:ExcelWorkbook></xml><![endif]-->' + ' <style type="text/css">' + 'table td {' + 'border: 0.5px solid #000000;' + 'width: auto;' + 'height: 25px;' + 'text-align: center;' + 'mso-number-format:\@;' + 'vnd.ms-excel.numberformat:\@;' + /* 'background-color: #4f891e;' +*/ /* 'color: #ffffff;' +*/ ' }' + 'table th {' + 'border: 0.5px solid #000000;' + /* 'width: auto;' +*/ 'height: 35px;' + 'text-align: center;' + 'font-size:20px' + 'mso-number-format:\@;' + 'vnd.ms-excel.numberformat:\@;' + ' }' + '</style>' + '</head><body ><table>{table}</table></body></html>'; //if (!tableid.nodeType) tableid = document.getElementById(tableid); //var ctx = { worksheet: sheetName || 'Worksheet', table: tableid.innerHTML }; var ctx = { worksheet: sheetName || 'Worksheet', table: html }; var a = document.createElement("a"); a.download = sheetName + ".xls"; a.href = uri + this.excelBase64(this.excelFormat(template, ctx)); document.body.appendChild(a); a.click(); document.body.removeChild(a); } // Excel格式 function excelFormat(s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }); } // 純js導(dǎo)出Excel function excelBase64(excelFile) { return window.btoa(unescape(encodeURIComponent(excelFile))); }
到此這篇關(guān)于C# 前端無插件打印導(dǎo)出實(shí)現(xiàn)方式的文章就介紹到這了,更多相關(guān)c#無插件打印導(dǎo)出內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)的UDP收發(fā)請(qǐng)求工具類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的UDP收發(fā)請(qǐng)求工具類,結(jié)合具體實(shí)例形式分析了C#針對(duì)UDP請(qǐng)求的監(jiān)聽、接收、發(fā)送等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06WinForm中變Enter鍵為Tab鍵實(shí)現(xiàn)焦點(diǎn)轉(zhuǎn)移的方法
這篇文章主要介紹了WinForm中變Enter鍵為Tab鍵實(shí)現(xiàn)焦點(diǎn)轉(zhuǎn)移的方法,主要通過一個(gè)ControlTools類來實(shí)現(xiàn)該功能,需要的朋友可以參考下2014-08-08C#基于SQLiteHelper類似SqlHelper類實(shí)現(xiàn)存取Sqlite數(shù)據(jù)庫的方法
這篇文章主要介紹了C#基于SQLiteHelper類似SqlHelper類實(shí)現(xiàn)存取Sqlite數(shù)據(jù)庫的方法,涉及C#操作SQLite數(shù)據(jù)庫的相關(guān)技巧,需要的朋友可以參考下2015-06-06c# OpenCvSharp實(shí)現(xiàn)常見檢測(斑點(diǎn)檢測,輪廓檢測,邊緣檢測)
這篇文章主要為大家詳細(xì)介紹了c#如何使用OpenCvSharp實(shí)現(xiàn)常見檢測(斑點(diǎn)檢測,輪廓檢測,邊緣檢測),文中的示例代碼講解詳細(xì),需要的小伙伴可以參考下2023-12-12C#去掉字符串中所有匹配的字符String.Replace方法
在C#中,如果你想要去掉字符串中所有匹配的字符,你可以使用String.Replace方法,本文主要介紹了C#去掉字符串中所有匹配的字符String.Replace方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04