包含这些文件
<script src="~/Scripts/jquery.base64.js"></script>
<script src="~/Scripts/tableExport.js"></script>
<script src="~/Scripts/jspdf/libs/base64.js"></script>
<script src="~/Scripts/jspdf/jspdf.js"></script>
<script src="~/Scripts/jspdf/FileSaver.js"></script>
<script src="~/Scripts/jspdf/jspdf.plugin.cell.js"></script>
,然后添加这些功能到脚本
function ExportTpGridtoPDF(divid) {
var table1 =
tableToJson($('#' + divid + ' .grid-table').get(0)),
cellWidth = 35,
rowCount = 0,
cellContents,
//leftMargin = 2,
//topMargin = 12,
//topMarginTable = 55,
//headerRowHeight = 13,
//rowHeight = 9,
leftMargin = 10,
topMargin = 15,
topMarginTable = 5,
headerRowHeight = 13,
rowHeight = 13,
l = {
orientation: 'l',
unit: 'mm',
format: 'a3',
compress: true,
fontSize: 8,
lineHeight: 1,
autoSize: false,
printHeaders: true
};
var doc = new jsPDF(l, '', '', '');
doc.setProperties({
title: 'Test PDF Document',
subject: 'This is the subject',
author: 'author',
keywords: 'generated, javascript, web 2.0, ajax',
creator: 'author'
});
doc.cellInitialize();
$.each(table1, function (i, row) {
rowCount++;
$.each(row, function (j, cellContent) {
if (rowCount == 1) {
doc.margins = 1;
doc.setFontSize(12);
doc.cell(leftMargin, topMargin, cellWidth, headerRowHeight, cellContent, i)
}
else if (rowCount == 2) {
doc.margins = 1;
doc.setFontSize(12);
doc.cell(leftMargin, topMargin, cellWidth, rowHeight, cellContent, i);
}
else {
doc.margins = 1;
doc.setFontSize(12);
doc.cell(leftMargin, topMargin, cellWidth, rowHeight, cellContent, i);
}
})
})
doc.save('sample Report.pdf');
}
function tableToJson(table) {
var data = [];
// first row needs to be headers
var headers = [];
for (var i = 0; i < table.rows[0].cells.length; i++) {
if (table.rows[0].cells[i].innerHTML != "") {
headers[i] = table.rows[0].cells[i].innerText.toLowerCase().replace(/ /gi, '');
}
}
// go through cells
for (var i = 1; i < table.rows.length; i++) {
var tableRow = table.rows[i];
var rowData = {};
for (var j = 1; j < tableRow.cells.length; j++) {
rowData[headers[j]] = tableRow.cells[j].innerText;
}
data.push(rowData);
}
return data;
}
循环每一行的网格并找到行内的每个控制,并将其添加到表 - 工具转换将网格数据导出到表格并将该表格导出到json –