1. 程式人生 > >js列印、匯出excel

js列印、匯出excel

頁面引入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));