js打印、导出excel

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34169240/article/details/86291484

页面引入js文件

<script type="text/javascript" src="${pageContext.request.contextPath}/JScript/jquery.PrintArea.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/JScript/jquery.tableToExcel.js"></script>

用法:

//打印
$('#MainFirst_ID').printArea();
//导出
$('#MainFirst_tableID').first().tableToExcel();
$('#MainFirst_tableID').first().tableToExcelNoBorder();
    

jquery.PrintArea.js内容:

// JavaScript Document
(function($) {
var printAreaCount = 0;
$.fn.printArea = function()
{
var ele = $(this);
var idPrefix = "printArea_";
removePrintArea( idPrefix + printAreaCount );
printAreaCount++;
var iframeId = idPrefix + printAreaCount;
var iframeStyle = 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;';
iframe = document.createElement('IFRAME');

document.body.appendChild(iframe);
iframe.contentWindow.document.write("<html>");  
iframe.contentWindow.document.write('<style media="print">@page {size:  auto; margin: 0mm; } html{margin:0px;} body{margin :0mm 0mm 0mm 0mm;}</style>');  
iframe.contentWindow.document.write("<BODY BGCOLOR=#ffffff>");
iframe.contentWindow.document.write($('#MainFirst_ZHDiagnosisReport_dlg_text').html());
iframe.contentWindow.document.write("</BODY>");
iframe.contentWindow.document.write("</HTML>");
iframe.contentWindow.document.write("<script type='text/javascript'> window.print();<//script>");
iframe.contentWindow.print();
iframe.contentWindow.document.close();

$(iframe).attr({ style : iframeStyle,
id    : iframeId
});
document.body.appendChild(iframe);
var doc = iframe.contentWindow.document;
$(document).find("link")
.filter(function(){
return $(this).attr("rel").toLowerCase() == "stylesheet";
})
.each(function(){
	
//doc.write('<style media="print">@page {size:  auto; margin: 0mm; } html{margin:0px;} body{margin :10mm 15mm 10mm 15mm;}</style>');

doc.write('<link type="text/css" rel="stylesheet" href="' +
$(this).attr("href") + '" >');
});
doc.write($(ele).prop('outerHTML'));
doc.write('<div class="' + $(ele).attr("class") + '">' + $(ele).html() + '</div>');
doc.close();
var frameWindow = iframe.contentWindow;
frameWindow.close();
frameWindow.focus();
//window.print();
//frameWindow.onload = function() {
//    frameWindow.print();
//  };
}
var removePrintArea = function(id)
{
$( "iframe#" + id ).remove();
};
})(jQuery);

jquery.tableToExcel.js内容:

/* ===========================================================
 * jquery.tableToExcel.js v1.0
 * ===========================================================
 * jQuery Table to Excel Plugin
 * Copyright 2012 Gabriel Dromard
 *
 * @see https://github.com/gdromard/jquery.tableToExcel for more details
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================================================== */
(function ($) {
  $.tableToExcel = {
		version: "1.0",
		uri: 'data:application/vnd.ms-excel;base64,',
		template: '<html xmlns:v="urn:schemas-microsoft-com:vml"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><meta http-equiv=Content-Type content="text/html; charset=UTF-8"><meta name=ProgId content=Excel.Sheet><!--[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]--></head><body><table border=1>{table}</table></body></html>',
		templateNoBorder: '<html xmlns:v="urn:schemas-microsoft-com:vml"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><meta http-equiv=Content-Type content="text/html; charset=UTF-8"><meta name=ProgId content=Excel.Sheet><!--[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]--></head><body><table border=0>{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]; }); },
	};

	$.fn.extend({
		tableToExcel: function(options) {
			options = $.extend({ name: 'Worksheet' }, options);
			return $(this).each(function() {
				var ctx = { worksheet: options.name, table: $(this).html() };
				window.location.href = $.tableToExcel.uri + $.tableToExcel.base64($.tableToExcel.format($.tableToExcel.template, ctx));
			});
		}
	});
	$.fn.extend({
		tableToExcelNoBorder: function(options) {
			options = $.extend({ name: 'Worksheet' }, options);
			return $(this).each(function() {
				var ctx = { worksheet: options.name, table: $(this).html() };
				window.location.href = $.tableToExcel.uri + $.tableToExcel.base64($.tableToExcel.format($.tableToExcel.templateNoBorder, ctx));
			});
		}
	});
}(window.jQuery));

猜你喜欢

转载自blog.csdn.net/qq_34169240/article/details/86291484