您的位置:首页 > 编程语言 > PHP开发

php 导出成csv或是excel格式的js文件(带序号)

2017-03-20 11:01 1081 查看
直接复制保存,命名为 like_export.js 即可。

/**
*各种导出
*/

/**
* 类的定义
* @param options
*/
var like_export = function (options) {
var _default = {
'header': [],
'body': [],
'need_foot': false,//bool
'filename': 'like_export',
'number': '序号',//string|bool
'sheet': 'like_export',//string

};
this.config = $.extend({}, _default, options);
this.rand = Math.floor(Math.random() * 1000);//随机数
}
like_export.prototype.tips = function (callback) {
win_obj.layer.msg('正在导出,请耐心等待...', {
shade: [0.8, '#393D49'],
icon: 0,
time: 2000 //2秒关闭(如果不配置,默认是3秒)
}, callback);
}
like_export.prototype.close_tips = function () {
win_obj.layer.closeAll();
}

/**
* 获取导出数据
* @param header
* @param body
* @returns {{head_arr: Array, body_arr: Array, all_arr: Array, head_string: string, body_string: string, all_string: string}}
*/
like_export.prototype.get_export_data = function (header, body) {
header = header ? header : this.config.header;
body = body ? body : this.config.body;
var _all_string = '';
var _all_arr = [];
var _header_arr = [];
if (this.config.number) {
_header_arr.push(this.config.number);
_all_arr.push(this.config.number);
}
var _header_string = '';
var _fields = [];
for (var i = 0, l = header.length; i < l; i++) {
_fields.push(header[i].field_key);
_header_arr.push(header[i].field_value);
_all_arr.push(header[i].field_value);
}
_header_string += '"' + _header_arr.join('","') + '"' + "\n";
var _body_string = '';
var _txt_string = '';
var _body_arr = [];
for (var i = 0; i < body.length; i++) {
var _body_tds_arr = [];
var _body_tds_txt_arr = [];
if (this.config.number) {
_body_tds_arr.push((i + 1));
}
for (var j = 0; j < _fields.length; j++) {
var _field = _fields[j];
var value = body[i][_field];
_body_tds_arr.push(value);
_body_tds_txt_arr.push(value);
_all_arr.push(value);
}
_body_string += '"' + _body_tds_arr.join('","') + '"' + "\n";
_txt_string += _body_tds_txt_arr.join('","') + "\r\n\r\n";
_body_arr.push(_body_tds_arr);
}
_all_string += _header_string + _body_string;
if (this.config.need_foot) {
//开启页脚
_all_arr.push(_header_arr);
_all_string += _header_string;
}
return {
'head_arr': _header_arr,
'body_arr': _body_arr,
'all_arr': _all_arr,
'head_string': _header_string,
'body_string': _body_string,
'all_string': _all_string,
'txt_string': _txt_string,
}
}

/**
* 导出CSV
* @param header
* @param body
* @param filename
*/
like_export.prototype.csv = function (header, body, filename) {
var export_data = this.get_export_data(header, body);
var blob = new Blob([export_data.all_string], {type: "text/csv;charset=utf-8"});
if (!filename) {
filename = this.config.filename;
}
filename += ".csv";
this.tips(function () {
var fr = saveAs(blob, filename);
});
}

like_export.prototype.txt = function (header, body, filename) {
var export_data = this.get_export_data(header, body);
var blob = new Blob([export_data.txt_string], {type: "text/csv;charset=utf-8"});
if (!filename) {
filename = this.config.filename;
}
filename += ".txt";
this.tips(function () {
var fr = saveAs(blob, filename);
});
}

/**
* 导出EXCEL
* @param header
* @param body
* @param filename
*/
like_export.prototype.excel = function (header, body, filename, top_value) {
if (!filename) {
filename = this.config.filename;
}
filename += '.xls';
var export_data = this.get_export_data(header, body);

if(top_value == '' || top_value == undefined){
var data = {
'body': export_data.body_arr,
'header': export_data.head_arr
};
}else{
var _topp_arr = [];
_topp_arr.push(top_value);

var data = {
'topp': _topp_arr,
'body': export_data.body_arr,
'header': export_data.head_arr
};
}

/*******/
var view = window;
var doc = view.document
// only get URL when necessary in case Blob.js hasn't overridden it yet
, get_URL = function () {
return view.URL || view.webkitURL || view;
}
, save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a")
, can_use_save_link = "download" in save_link,
click = function (node) {
var event = new MouseEvent("click");
node.dispatchEvent(event);
}
, dispatch = function (filesaver, event_types, event) {
event_types = [].concat(event_types);
var i = event_types.length;
while (i--) {
var listener = filesaver["on" + event_types[i]];
if (typeof listener === "function") {
try {
listener.call(filesaver, event || filesaver);
} catch (ex) {
throw_outside(ex);
}
}
}
}
, click = function (node) {
var event = new MouseEvent("click");
node.dispatchEvent(event);
}
, is_safari = /Version\/[\d\.]+.*Safari/.test(navigator.userAgent)
, webkit_req_fs = view.webkitRequestFileSystem
, req_fs = view.requestFileSystem || webkit_req_fs || view.mozRequestFileSystem
, throw_outside = function (ex) {
(view.setImmediate || view.setTimeout)(function () {
throw ex;
}, 0);
}
, force_saveable_type = "application/octet-stream"
, fs_min_size = 0
// the Blob API is fundamentally broken as there is no "downloadfinished" event to subscribe to
, arbitrary_revoke_timeout = 1000 * 40 // in ms
, revoke = function (file) {
var revoker = function () {
if (typeof file === "string") { // file is an object URL
get_URL().revokeObjectURL(file);
} else { // file is a File
file.remove();
}
};
/* // Take note W3C:
var
uri = typeof file === "string" ? file : file.toURL()
, revoker = function(evt) {
// idealy DownloadFinishedEvent.data would be the URL requested
if (evt.data === uri) {
if (typeof file === "string") { // file is an object URL
get_URL().revokeObjectURL(file);
} else { // file is a File
file.remove();
}
}
}
;
view.addEventListener("downloadfinished", revoker);
*/
setTimeout(revoker, arbitrary_revoke_timeout);
}
, dispatch = function (filesaver, event_types, event) {
event_types = [].concat(event_types);
var i = event_types.length;
while (i--) {
var listener = filesaver["on" + event_types[i]];
if (typeof listener === "function") {
try {
listener.call(filesaver, event || filesaver);
} catch (ex) {
throw_outside(ex);
}
}
}
}
, auto_bom = function (blob) {
// prepend BOM for UTF-8 XML and text/* types (including HTML)
if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
return new Blob(["\ufeff", blob], {type: blob.type});
}
return blob;
};
var auto_bom = function (blob) {
// prepend BOM for UTF-8 XML and text/* types (including HTML)
if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
return new Blob(["\ufeff", blob], {type: blob.type});
}
return blob;
}
var FileSaver = function (blob, name, no_auto_bom) {
if (!no_auto_bom) {
blob = auto_bom(blob);
}
// First try a.download, then web filesystem, then object URLs
var
filesaver = this
, type = blob.type
, blob_changed = false
, object_url
, target_view
, dispatch_all = function () {
dispatch(filesaver, "writestart progress write writeend".split(" "));
}
// on any filesys errors revert to saving with object URLs
, fs_error = function () {
if (target_view && is_safari && typeof FileReader !== "undefined") {
// Safari doesn't allow downloading of blob urls
var reader = new FileReader();
reader.onloadend = function () {
var base64Data = reader.result;
target_view.location.href = "data:attachment/file" + base64Data.slice(base64Data.search(/[,;]/));
filesaver.readyState = filesaver.DONE;
dispatch_all();
};
reader.readAsDataURL(blob);
filesaver.readyState = filesaver.INIT;
return;
}
// don't create more object URLs than needed
if (blob_changed || !object_url) {
object_url = get_URL().createObjectURL(blob);
}
if (target_view) {
target_view.location.href = object_url;
} else {
var new_tab = view.open(object_url, "_blank");
if (new_tab === undefined && is_safari) {
//Apple do not allow window.open, see http://bit.ly/1kZffRI view.location.href = object_url
}
}
filesaver.readyState = filesaver.DONE;
dispatch_all();
revoke(object_url);
}
, abortable = function (func) {
return function () {
if (filesaver.readyState !== filesaver.DONE) {
return func.apply(this, arguments);
}
};
}
, create_if_not_found = {create: true, exclusive: false}
, slice
;
filesaver.readyState = filesaver.INIT;
if (!name) {
name = "download";
}
if (can_use_save_link) {
object_url = get_URL().createObjectURL(blob);
setTimeout(function () {
save_link.href = object_url;
save_link.download = name;
click(save_link);
dispatch_all();
revoke(object_url);
filesaver.readyState = filesaver.DONE;
});
return;
}
// Object and web filesystem URLs have a problem saving in Google Chrome when
// viewed in a tab, so I force save with application/octet-stream
// http://code.google.com/p/chromium/issues/detail?id=91158 // Update: Google errantly closed 91158, I submitted it again:
// https://code.google.com/p/chromium/issues/detail?id=389642 if (view.chrome && type && type !== force_saveable_type) {
slice = blob.slice || blob.webkitSlice;
blob = slice.call(blob, 0, blob.size, force_saveable_type);
blob_changed = true;
}
// Since I can't be sure that the guessed media type will trigger a download
// in WebKit, I append .download to the filename.
// https://bugs.webkit.org/show_bug.cgi?id=65440 if (webkit_req_fs && name !== "download") {
name += ".download";
}
if (type === force_saveable_type || webkit_req_fs) {
target_view = view;
}
if (!req_fs) {
fs_error();
return;
}
fs_min_size += blob.size;
req_fs(view.TEMPORARY, fs_min_size, abortable(function (fs) {
fs.root.getDirectory("saved", create_if_not_found, abortable(function (dir) {
var save = function () {
dir.getFile(name, create_if_not_found, abortable(function (file) {
file.createWriter(abortable(function (writer) {
writer.onwriteend = function (event) {
target_view.location.href = file.toURL();
filesaver.readyState = filesaver.DONE;
dispatch(filesaver, "writeend", event);
revoke(file);
};
writer.onerror = function () {
var error = writer.error;
if (error.code !== error.ABORT_ERR) {
fs_error();
}
};
"writestart progress write abort".split(" ").forEach(function (event) {
writer["on" + event] = filesaver["on" + event];
});
writer.write(blob);
filesaver.abort = function () {
writer.abort();
filesaver.readyState = filesaver.DONE;
};
filesaver.readyState = filesaver.WRITING;
}), fs_error);
}), fs_error);
};
dir.getFile(name, {create: false}, abortable(function (file) {
// delete file if it already exists
file.remove();
save();
}), abortable(function (ex) {
if (ex.code === ex.NOT_FOUND_ERR) {
save();
} else {
fs_error();
}
}));
}), fs_error);
}), fs_error);
}
try {
var _serialiser = new XMLSerializer();
var _ieExcel;
} catch (e) {
}
function createCellPos(n) {
var ordA = 'A'.charCodeAt(0);
var ordZ = 'Z'.charCodeAt(0);
var len = ordZ - ordA + 1;
var s = "";

while (n >= 0) {
s = String.fromCharCode(n % len + ordA) + s;
n = Math.floor(n / len) - 1;
}

return s;
}

function _addToZip(zip, obj) {
if (_ieExcel === undefined) {
// Detect if we are dealing with IE's _awful_ serialiser by seeing if it
// drop attributes
_ieExcel = _serialiser
.serializeToString(
$.parseXML(excelStrings['xl/worksheets/sheet1.xml'])
)
.indexOf('xmlns:r') === -1;
}

$.each(obj, function (name, val) {
if ($.isPlainObject(val)) {
var newDir = zip.folder(name);
_addToZip(newDir, val);
}
else {
if (_ieExcel) {
// IE's XML serialiser will drop some name space attributes from
// from the root node, so we need to save them. Do this by
// replacing the namespace nodes with a regular attribute that
// we convert back when serialised. Edge does not have this
// issue
var worksheet = val.childNodes[0];
var i, ien;
var attrs = [];

for (i = worksheet.attributes.length - 1; i >= 0; i--) {
var attrName = worksheet.attributes[i].nodeName;
var attrValue = worksheet.attributes[i].nodeValue;

if (attrName.indexOf(':') !== -1) {
attrs.push({name: attrName, value: attrValue});

worksheet.removeAttribute(attrName);
}
}

for (i = 0, ien = attrs.length; i < ien; i++) {
var attr = val.createAttribute(attrs[i].name.replace(':', '_dt_b_namespace_token_'));
attr.value = attrs[i].value;
worksheet.setAttributeNode(attr);
}
}

var str = _serialiser.serializeToString(val);

// Fix IE's XML
if (_ieExcel) {
// IE doesn't include the XML declaration
if (str.indexOf('<?xml') === -1) {
str = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + str;
}

// Return namespace attributes to being as such
str = str.replace(/_dt_b_namespace_token_/g, ':');
}

// Both IE and Edge will put empty name space attributes onto the
// rows and columns making them useless
str = str
.replace(/<row xmlns="" /g, '<row ')
.replace(/<cols xmlns="">/g, '<cols>');

zip.file(name, str);
}
});
}

function _createNode(doc, nodeName, opts) {
var tempNode = doc.createElement(nodeName);

if (opts) {
if (opts.attr) {
$(tempNode).attr(opts.attr);
}

if (opts.children) {
$.each(opts.children, function (key, value) {
tempNode.appendChild(value);
});
}

if (opts.text) {
tempNode.appendChild(doc.createTextNode(opts.text));
}
}

return tempNode;
}

function _excelColWidth(data, col) {
var max = data.header[col].length;
var len;

if (data.footer && data.footer[col].length > max) {
max = data.footer[col].length;
}

for (var i = 0, ien = data.body.length; i < ien; i++) {
len = data.body[i][col].toString().length;

if (len > max) {
max = len;
}

// Max width rather than having potentially massive column widths
if (max > 40) {
break;
}
}

// And a min width
return max > 5 ? max : 5;
}

var excelStrings = {
"_rels/.rels": '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">' +
'<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>' +
'</Relationships>',

"xl/_rels/workbook.xml.rels": '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">' +
'<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>' +
'<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>' +
'</Relationships>',

"[Content_Types].xml": '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">' +
'<Default Extension="xml" ContentType="application/xml" />' +
'<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" />' +
'<Default Extension="jpeg" ContentType="image/jpeg" />' +
'<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml" />' +
'<Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" />' +
'<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml" />' +
'</Types>',

"xl/workbook.xml": '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">' +
'<fileVersion appName="xl" lastEdited="5" lowestEdited="5" rupBuild="24816"/>' +
'<workbookPr showInkAnnotation="0" autoCompressPictures="0"/>' +
'<bookViews>' +
'<workbookView xWindow="0" yWindow="0" windowWidth="25600" windowHeight="19020" tabRatio="500"/>' +
'</bookViews>' +
'<sheets>' +
'<sheet name="" sheetId="1" r:id="rId1"/>' +
'</sheets>' +
'</workbook>',

"xl/worksheets/sheet1.xml": '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">' +
'<sheetData/>' +
'</worksheet>',

"xl/styles.xml": '<?xml version="1.0" encoding="UTF-8"?>' +
'<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">' +
'<fonts count="5" x14ac:knownFonts="1">' +
'<font>' +
'<sz val="11" />' +
'<name val="Calibri" />' +
'</font>' +
'<font>' +
'<sz val="11" />' +
'<name val="Calibri" />' +
'<color rgb="FFFFFFFF" />' +
'</font>' +
'<font>' +
'<sz val="11" />' +
'<name val="Calibri" />' +
'<b />' +
'</font>' +
'<font>' +
'<sz val="11" />' +
'<name val="Calibri" />' +
'<i />' +
'</font>' +
'<font>' +
'<sz val="11" />' +
'<name val="Calibri" />' +
'<u />' +
'</font>' +
'</fonts>' +
'<fills count="6">' +
'<fill>' +
'<patternFill patternType="none" />' +
'</fill>' +
'<fill/>' + // Excel appears to use this as a dotted background regardless of values
'<fill>' +
'<patternFill patternType="solid">' +
'<fgColor rgb="FFD9D9D9" />' +
'<bgColor indexed="64" />' +
'</patternFill>' +
'</fill>' +
'<fill>' +
'<patternFill patternType="solid">' +
'<fgColor rgb="FFD99795" />' +
'<bgColor indexed="64" />' +
'</patternFill>' +
'</fill>' +
'<fill>' +
'<patternFill patternType="solid">' +
'<fgColor rgb="ffc6efce" />' +
'<bgColor indexed="64" />' +
'</patternFill>' +
'</fill>' +
'<fill>' +
'<patternFill patternType="solid">' +
'<fgColor rgb="ffc6cfef" />' +
'<bgColor indexed="64" />' +
'</patternFill>' +
'</fill>' +
'</fills>' +
'<borders count="2">' +
'<border>' +
'<left />' +
'<right />' +
'<top />' +
'<bottom />' +
'<diagonal />' +
'</border>' +
'<border diagonalUp="false" diagonalDown="false">' +
'<left style="thin">' +
'<color auto="1" />' +
'</left>' +
'<right style="thin">' +
'<color auto="1" />' +
'</right>' +
'<top style="thin">' +
'<color auto="1" />' +
'</top>' +
'<bottom style="thin">' +
'<color auto="1" />' +
'</bottom>' +
'<diagonal />' +
'</border>' +
'</borders>' +
'<cellStyleXfs count="1">' +
'<xf numFmtId="0" fontId="0" fillId="0" borderId="0" />' +
'</cellStyleXfs>' +
'<cellXfs count="2">' +
'<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="1" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="2" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="3" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="4" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="0" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="1" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="2" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="3" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="4" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="0" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="1" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="2" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="3" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="4" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="0" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="1" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="2" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="3" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="4" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="0" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="1" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="2" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="3" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="4" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="0" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="1" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="2" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="3" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="4" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="0" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="1" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="2" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="3" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="4" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="0" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="1" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="2" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="3" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="4" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="0" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="1" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="2" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="3" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="4" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="0" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="1" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="2" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="3" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'<xf numFmtId="0" fontId="4" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>' +
'</cellXfs>' +
'<cellStyles count="1">' +
'<cellStyle name="Normal" xfId="0" builtinId="0" />' +
'</cellStyles>' +
'<dxfs count="0" />' +
'<tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleMedium4" />' +
'</styleSheet>'
};
var rowPos = 0;
var getXml = function (type) {
var str = excelStrings[type];
return $.parseXML(str);
};
var rels = getXml('xl/worksheets/sheet1.xml');
var relsGet = rels.getElementsByTagName("sheetData")[0];
var xls = {
_rels: {
".rels": getXml('_rels/.rels')
},
xl: {
_rels: {
"workbook.xml.rels": getXml('xl/_rels/workbook.xml.rels')
},
"workbook.xml": getXml('xl/workbook.xml'),
"styles.xml": getXml('xl/styles.xml'),
"worksheets": {
"sheet1.xml": rels
}

},
"[Content_Types].xml": getXml('[Content_Types].xml')
};
var currentRow, rowNode;
var addRow = function (row) {
currentRow = rowPos + 1;
rowNode = _createNode(rels, "row", {attr: {r: currentRow}});

for (var i = 0, ien = row.length; i < ien; i++) {
// Concat both the Cell Columns as a letter and the Row of the cell.
var cellId = createCellPos(i) + '' + currentRow;
var cell;

if (row[i] === null || row[i] === undefined) {
row[i] = '';
}

// Detect numbers - don't match numbers with leading zeros or a negative
// anywhere but the start
if (typeof row[i] === 'number' || (
row[i].match &&
$.trim(row[i]).match(/^-?\d+(\.\d+)?$/) && !$.trim(row[i]).match(/^0\d+/) )
) {
cell = _createNode(rels, 'c', {
attr: {
t: 'n',
r: cellId
},
children: [
_createNode(rels, 'v', {text: row[i]})
]
});
}
else {
// Replace non standard characters for text output
var text = !row[i].replace ?
row[i] :
row[i]
.replace(/&(?!amp;)/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F-\x9F]/g, '');

cell = _createNode(rels, 'c', {
attr: {
t: 'inlineStr',
r: cellId
},
children: {
row: _createNode(rels, 'is', {
children: {
row: _createNode(rels, 't', {
text: text
})
}
})
}
});
}

rowNode.appendChild(cell);
}
relsGet.appendChild(rowNode);
rowPos++;
};
$('sheets sheet', xls.xl['workbook.xml']).attr('name', this.config.sheet);
if (data.topp) {
addRow(data.topp, rowPos);
// $('row c', rels).attr('s', '2'); // bold
}

if (data.header) {
addRow(data.header, rowPos);
$('row c', rels).attr('s', '2'); // bold
}
for (var n = 0, ie = data.body.length; n < ie; n++) {
addRow(data.body
, rowPos);
}
if (data.header && this.config.need_foot) {
addRow(data.header, rowPos);
$('row:last c', rels).attr('s', '2'); // bold
}
var cols = _createNode(rels, 'cols');
$('worksheet', rels).prepend(cols);
for (var i = 0, ien = data.header.length; i < ien; i++) {
cols.appendChild(_createNode(rels, 'col', {
attr: {
min: i + 1,
max: i + 1,
width: _excelColWidth(data, i),
customWidth: 1
}
}));
}
var zip = new JSZip();
var zipConfig = {
type: 'blob',
mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
};
_addToZip(zip, xls);
this.tips(function () {
if (zip.generateAsync) {
// JSZip 3+
zip
.generateAsync(zipConfig)
.then(function (blob) {
fr = new FileSaver(blob, filename);
});
}
else {
// JSZip 2.5
fr = new FileSaver(zip.generate(zipConfig), filename);
}
});
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php