• vue中用xlsx、xlsx-style、file-saver插件实现用表格原始数据打印excel文件


     打印出的excel文件如下: 

     1、需要安装插件(xlsx插件使用后已经有换行功能,如果对导出的excel文件没有样式要求就不用下载xlsx-style插件)

    npm i -S xlsx
    npm i -S file-saver

    xlsx-style依据情况选择使用:如需要换单元格底色,加边框,等

    npm i -S xlsx-style

    安装过程中遇到的问题:

    Can‘t resolve ‘./cptable‘ in ‘xxx\node_modules_xlsx

    解决办法:在vue.config.js文件中加入如下代码

    1. chainWebpack: (config) => {
    2. //忽略的打包文件
    3. config.externals({
    4. './cptable': 'var cptable'
    5. });
    6. },

    2、加入关键js文件,我加在工具类文件夹下了

     js文件中完整内容如下:

    Blob.js

    1. /* eslint-disable */
    2. /* Blob.js
    3. * A Blob implementation.
    4. * 2014-05-27
    5. *
    6. * By Eli Grey, http://eligrey.com
    7. * By Devin Samarin, https://github.com/eboyjr
    8. * License: X11/MIT
    9. * See LICENSE.md
    10. */
    11. /*global self, unescape */
    12. /*jslint bitwise: true, regexp: true, confusion: true, es5: true, vars: true, white: true,
    13. plusplus: true */
    14. /*! @source http://purl.eligrey.com/github/Blob.js/blob/master/Blob.js */
    15. (function (view) {
    16. "use strict";
    17. view.URL = view.URL || view.webkitURL;
    18. if (view.Blob && view.URL) {
    19. try {
    20. new Blob;
    21. return;
    22. } catch (e) {}
    23. }
    24. // Internally we use a BlobBuilder implementation to base Blob off of
    25. // in order to support older browsers that only have BlobBuilder
    26. var BlobBuilder = view.BlobBuilder || view.WebKitBlobBuilder || view.MozBlobBuilder || (function(view) {
    27. var
    28. get_class = function(object) {
    29. return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1];
    30. }
    31. , FakeBlobBuilder = function BlobBuilder() {
    32. this.data = [];
    33. }
    34. , FakeBlob = function Blob(data, type, encoding) {
    35. this.data = data;
    36. this.size = data.length;
    37. this.type = type;
    38. this.encoding = encoding;
    39. }
    40. , FBB_proto = FakeBlobBuilder.prototype
    41. , FB_proto = FakeBlob.prototype
    42. , FileReaderSync = view.FileReaderSync
    43. , FileException = function(type) {
    44. this.code = this[this.name = type];
    45. }
    46. , file_ex_codes = (
    47. "NOT_FOUND_ERR SECURITY_ERR ABORT_ERR NOT_READABLE_ERR ENCODING_ERR "
    48. + "NO_MODIFICATION_ALLOWED_ERR INVALID_STATE_ERR SYNTAX_ERR"
    49. ).split(" ")
    50. , file_ex_code = file_ex_codes.length
    51. , real_URL = view.URL || view.webkitURL || view
    52. , real_create_object_URL = real_URL.createObjectURL
    53. , real_revoke_object_URL = real_URL.revokeObjectURL
    54. , URL = real_URL
    55. , btoa = view.btoa
    56. , atob = view.atob
    57. , ArrayBuffer = view.ArrayBuffer
    58. , Uint8Array = view.Uint8Array
    59. ;
    60. FakeBlob.fake = FB_proto.fake = true;
    61. while (file_ex_code--) {
    62. FileException.prototype[file_ex_codes[file_ex_code]] = file_ex_code + 1;
    63. }
    64. if (!real_URL.createObjectURL) {
    65. URL = view.URL = {};
    66. }
    67. URL.createObjectURL = function(blob) {
    68. var
    69. type = blob.type
    70. , data_URI_header
    71. ;
    72. if (type === null) {
    73. type = "application/octet-stream";
    74. }
    75. if (blob instanceof FakeBlob) {
    76. data_URI_header = "data:" + type;
    77. if (blob.encoding === "base64") {
    78. return data_URI_header + ";base64," + blob.data;
    79. } else if (blob.encoding === "URI") {
    80. return data_URI_header + "," + decodeURIComponent(blob.data);
    81. } if (btoa) {
    82. return data_URI_header + ";base64," + btoa(blob.data);
    83. } else {
    84. return data_URI_header + "," + encodeURIComponent(blob.data);
    85. }
    86. } else if (real_create_object_URL) {
    87. return real_create_object_URL.call(real_URL, blob);
    88. }
    89. };
    90. URL.revokeObjectURL = function(object_URL) {
    91. if (object_URL.substring(0, 5) !== "data:" && real_revoke_object_URL) {
    92. real_revoke_object_URL.call(real_URL, object_URL);
    93. }
    94. };
    95. FBB_proto.append = function(data/*, endings*/) {
    96. var bb = this.data;
    97. // decode data to a binary string
    98. if (Uint8Array && (data instanceof ArrayBuffer || data instanceof Uint8Array)) {
    99. var
    100. str = ""
    101. , buf = new Uint8Array(data)
    102. , i = 0
    103. , buf_len = buf.length
    104. ;
    105. for (; i < buf_len; i++) {
    106. str += String.fromCharCode(buf[i]);
    107. }
    108. bb.push(str);
    109. } else if (get_class(data) === "Blob" || get_class(data) === "File") {
    110. if (FileReaderSync) {
    111. var fr = new FileReaderSync;
    112. bb.push(fr.readAsBinaryString(data));
    113. } else {
    114. // async FileReader won't work as BlobBuilder is sync
    115. throw new FileException("NOT_READABLE_ERR");
    116. }
    117. } else if (data instanceof FakeBlob) {
    118. if (data.encoding === "base64" && atob) {
    119. bb.push(atob(data.data));
    120. } else if (data.encoding === "URI") {
    121. bb.push(decodeURIComponent(data.data));
    122. } else if (data.encoding === "raw") {
    123. bb.push(data.data);
    124. }
    125. } else {
    126. if (typeof data !== "string") {
    127. data += ""; // convert unsupported types to strings
    128. }
    129. // decode UTF-16 to binary string
    130. bb.push(unescape(encodeURIComponent(data)));
    131. }
    132. };
    133. FBB_proto.getBlob = function(type) {
    134. if (!arguments.length) {
    135. type = null;
    136. }
    137. return new FakeBlob(this.data.join(""), type, "raw");
    138. };
    139. FBB_proto.toString = function() {
    140. return "[object BlobBuilder]";
    141. };
    142. FB_proto.slice = function(start, end, type) {
    143. var args = arguments.length;
    144. if (args < 3) {
    145. type = null;
    146. }
    147. return new FakeBlob(
    148. this.data.slice(start, args > 1 ? end : this.data.length)
    149. , type
    150. , this.encoding
    151. );
    152. };
    153. FB_proto.toString = function() {
    154. return "[object Blob]";
    155. };
    156. FB_proto.close = function() {
    157. this.size = this.data.length = 0;
    158. };
    159. return FakeBlobBuilder;
    160. }(view));
    161. view.Blob = function Blob(blobParts, options) {
    162. var type = options ? (options.type || "") : "";
    163. var builder = new BlobBuilder();
    164. if (blobParts) {
    165. for (var i = 0, len = blobParts.length; i < len; i++) {
    166. builder.append(blobParts[i]);
    167. }
    168. }
    169. return builder.getBlob(type);
    170. };
    171. }(typeof self !== "undefined" && self || typeof window !== "undefined" && window || this.content || this));
    '
    运行

     Export2Excel.js,如果对打印出的表格有样式要求,需要好好读一下代码,对excel文件的样式对象属性做了解。推荐读这篇文章JavaScript导出excel文件,并修改文件样式_FEWY的博客-CSDN博客_js冒泡排序

    1. /* eslint-disable */
    2. require('script-loader!file-saver');
    3. require('./Blob.js');
    4. require('script-loader!xlsx/dist/xlsx.core.min');
    5. import XLSXS from "xlsx-style"
    6. function generateArray(table) {
    7. var out = [];
    8. var rows = table.querySelectorAll('tr');
    9. var ranges = [];
    10. for (var R = 0; R < rows.length; ++R) {
    11. var outRow = [];
    12. var row = rows[R];
    13. var columns = row.querySelectorAll('td');
    14. for (var C = 0; C < columns.length; ++C) {
    15. var cell = columns[C];
    16. var colspan = cell.getAttribute('colspan');
    17. var rowspan = cell.getAttribute('rowspan');
    18. var cellValue = cell.innerText;
    19. if (cellValue !== "" && cellValue == +cellValue) cellValue = +cellValue;
    20. //Skip ranges
    21. ranges.forEach(function (range) {
    22. if (R >= range.s.r && R <= range.e.r && outRow.length >= range.s.c && outRow.length <= range.e.c) {
    23. for (var i = 0; i <= range.e.c - range.s.c; ++i) outRow.push(null);
    24. }
    25. });
    26. //Handle Row Span
    27. if (rowspan || colspan) {
    28. rowspan = rowspan || 1;
    29. colspan = colspan || 1;
    30. ranges.push({
    31. s: {
    32. r: R,
    33. c: outRow.length
    34. },
    35. e: {
    36. r: R + rowspan - 1,
    37. c: outRow.length + colspan - 1
    38. }
    39. });
    40. };
    41. //Handle Value
    42. outRow.push(cellValue !== "" ? cellValue : null);
    43. //Handle Colspan
    44. if (colspan)
    45. for (var k = 0; k < colspan - 1; ++k) outRow.push(null);
    46. }
    47. out.push(outRow);
    48. }
    49. return [out, ranges];
    50. };
    51. function datenum(v, date1904) {
    52. if (date1904) v += 1462;
    53. var epoch = Date.parse(v);
    54. return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
    55. }
    56. function sheet_from_array_of_arrays(data, opts) {
    57. var ws = {};
    58. var range = {
    59. s: {
    60. c: 10000000,
    61. r: 10000000
    62. },
    63. e: {
    64. c: 0,
    65. r: 0
    66. }
    67. };
    68. for (var R = 0; R != data.length; ++R) {
    69. for (var C = 0; C != data[R].length; ++C) {
    70. if (range.s.r > R) range.s.r = R;
    71. if (range.s.c > C) range.s.c = C;
    72. if (range.e.r < R) range.e.r = R;
    73. if (range.e.c < C) range.e.c = C;
    74. var cell = {
    75. v: data[R][C]
    76. };
    77. if (cell.v == null) continue;
    78. var cell_ref = XLSXS.utils.encode_cell({
    79. c: C,
    80. r: R
    81. });
    82. if (typeof cell.v === 'number') cell.t = 'n';
    83. else if (typeof cell.v === 'boolean') cell.t = 'b';
    84. else if (cell.v instanceof Date) {
    85. cell.t = 'n';
    86. cell.z = XLSX.SSF._table[14];
    87. cell.v = datenum(cell.v);
    88. }
    89. else cell.t = 's';
    90. ws[cell_ref] = cell;
    91. }
    92. }
    93. if (range.s.c < 10000000) ws['!ref'] = XLSXS.utils.encode_range(range);
    94. return ws;
    95. }
    96. function Workbook() {
    97. if (!(this instanceof Workbook)) return new Workbook();
    98. this.SheetNames = [];
    99. this.Sheets = {};
    100. }
    101. function s2ab(s) {
    102. var buf = new ArrayBuffer(s.length);
    103. var view = new Uint8Array(buf);
    104. for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
    105. return buf;
    106. }
    107. export function export_table_to_excel(id) {
    108. var theTable = document.getElementById(id);
    109. var oo = generateArray(theTable);
    110. var ranges = oo[1];
    111. /* original data */
    112. var data = oo[0];
    113. var ws_name = "SheetJS";
    114. var wb = new Workbook(),
    115. ws = sheet_from_array_of_arrays(data);
    116. /* add ranges to worksheet */
    117. // ws['!cols'] = ['apple', 'banan'];
    118. ws['!merges'] = ranges;
    119. /* add worksheet to workbook */
    120. wb.SheetNames.push(ws_name);
    121. wb.Sheets[ws_name] = ws;
    122. var wbout = XLSX.write(wb, {
    123. bookType: 'xlsx',
    124. bookSST: false,
    125. type: 'binary'
    126. });
    127. saveAs(new Blob([s2ab(wbout)], {
    128. type: "application/octet-stream"
    129. }), "test.xlsx")
    130. }
    131. export function export_json_to_excel({
    132. multiHeader2 = [], // 第一行表头
    133. multiHeader = [], // 第二行表头
    134. header, // 第三行表头
    135. data,//传递的数据
    136. filename, //文件名
    137. merges = [], // 合并
    138. autoWidth = true,//用于设置列宽的
    139. bookType = 'xlsx'
    140. } = {}) {
    141. /* original data */
    142. filename = filename || '列表';
    143. data = [...data]
    144. data.unshift(header);
    145. for (let i = multiHeader2.length - 1; i > -1; i--) {
    146. data.unshift(multiHeader2[i])
    147. }
    148. for (let i = multiHeader.length - 1; i > -1; i--) {
    149. data.unshift(multiHeader[i])
    150. }
    151. var ws_name = "SheetJS";
    152. var wb = new Workbook(),
    153. ws = sheet_from_array_of_arrays(data);
    154. let borderAll = { //单元格外侧框线
    155. top: {
    156. style: 'thin',//表示用细框线
    157. },
    158. bottom: {
    159. style: 'thin'
    160. },
    161. left: {
    162. style: 'thin'
    163. },
    164. right: {
    165. style: 'thin'
    166. }
    167. };
    168. for (let key in ws) {
    169. if (ws[key] instanceof Object) {
    170. ws[key].s = {
    171. name:'宋体',
    172. border: borderAll,
    173. alignment: {
    174. horizontal: 'center', //水平居中对齐
    175. vertical: 'center',//垂直居中
    176. wrapText: 1,//自动换行
    177. },
    178. font: {
    179. sz: 10,//单元格中字体的样式与颜色设置
    180. color: {
    181. rgb: '495060'
    182. }
    183. },
    184. bold: true,
    185. numFmt: 0
    186. }
    187. }
    188. }
    189. let arr = ["A1", "B1", "C1", "D1", "E1", "F1", "G1", "H1", "I1", "J1", "K1", "L1", "M1", "N1", "O1", "P1", "Q1", "R1", "S1", "T1", "U1", "V1", "W1", "X1", "Y1", "Z1"];
    190. let arr1 = ["A2", "B2", "C2", "D2", "E2", "F2", "G2", "H2", "I2", "J2", "K2", "L2", "M2", "N2", "O2", "P2", "Q2", "R2", "S2", "T2", "U2", "V2", "W2", "X2", "Y2", "Z2"];
    191. arr.some(function (v) {//对单独的1 2 行进行样式修改,去掉框线,放大字体
    192. let a = merges[0].split(':')
    193. if (v == a[1]) {
    194. ws[v].s.border = {}
    195. return true;
    196. }else {
    197. ws[v].s.border = {}
    198. }
    199. ws[v].s.font= {
    200. sz: 14,//单元格中字体的样式与颜色设置
    201. color: {
    202. rgb: '495060'
    203. },
    204. bold: true,
    205. }
    206. })
    207. arr1.some(function (v) {
    208. if (ws[v]) {
    209. ws[v].s.border = {}
    210. }
    211. })
    212. if (merges.length > 0) {
    213. if (!ws['!merges']) ws['!merges'] = [];
    214. merges.forEach(item => {
    215. ws['!merges'].push(XLSXS.utils.decode_range(item))
    216. })
    217. }
    218. if (autoWidth) {
    219. let colWidths = [];
    220. // 计算每一列的所有单元格宽度
    221. // 先遍历行
    222. data.forEach((row) => {
    223. // 列序号
    224. let index = 0
    225. // 遍历列
    226. for (const key in row) {
    227. if (colWidths[index] == null) colWidths[index] = []
    228. switch (typeof row[key]) {
    229. case 'string':
    230. case 'number':
    231. case 'boolean': colWidths[index].push(getCellWidth(row[key]))
    232. break
    233. case 'object':
    234. case 'function':
    235. colWidths[index].push(0)
    236. break
    237. }
    238. index++
    239. }
    240. })
    241. ws['!cols'] = [];
    242. colWidths.forEach((widths, index) => {
    243. // 计算列头的宽度
    244. widths.push(getCellWidth(header[index]))
    245. // 设置最大值为列宽
    246. ws['!cols'].push({
    247. wch: Math.max(...widths)
    248. })
    249. })
    250. }
    251. /* add worksheet to workbook */
    252. wb.SheetNames.push(ws_name);
    253. wb.Sheets[ws_name] = ws;
    254. var wbout = XLSXS.write(wb, {
    255. bookType: bookType,
    256. bookSST: false,
    257. type: 'binary'
    258. });
    259. saveAs(new Blob([s2ab(wbout)], {
    260. type: "application/octet-stream"
    261. }), `${filename}.${bookType}`);
    262. }
    263. export function getCellWidth(value) {
    264. if (value == null) {
    265. return 10
    266. } else if (value.toString().charCodeAt(0) > 255) {
    267. // 判断是否包含中文
    268. let length = value.toString().length * 2
    269. if (length > 60) {
    270. length = length - 40
    271. //这里的宽度可以自己设定,在前面设置wrapText: 1可以在单元格内换行
    272. }
    273. return length
    274. } else {
    275. return value.toString().length * 1.2
    276. }
    277. }

    若不需要操作样式,可将Export2Excel.js文件中的方法替换为如下

    1. // 对此方法进行修改,如下:
    2. export function export_json_to_excel({
    3. multiHeader2 = [], // 第一行表头
    4. multiHeader = [], // 第二行表头
    5. header, // 第三行表头
    6. data,
    7. filename, //文件名
    8. merges = [], // 合并
    9. autoWidth = true,
    10. bookType = 'xlsx'
    11. } = {}) {
    12. /* original data */
    13. filename = filename || '列表';
    14. data = [...data]
    15. data.unshift(header);
    16. for (let i = multiHeader2.length - 1; i > -1; i--) {
    17. data.unshift(multiHeader2[i])
    18. }
    19. for (let i = multiHeader.length - 1; i > -1; i--) {
    20. data.unshift(multiHeader[i])
    21. }
    22. var ws_name = "SheetJS";
    23. var wb = new Workbook(),
    24. ws = sheet_from_array_of_arrays(data);
    25. if (merges.length > 0) {
    26. if (!ws['!merges']) ws['!merges'] = [];
    27. merges.forEach(item => {
    28. ws['!merges'].push(XLSX.utils.decode_range(item))
    29. })
    30. }
    31. if (autoWidth) {
    32. /*设置worksheet每列的最大宽度*/
    33. const colWidth = data.map(row => row.map(val => {
    34. /*先判断是否为null/undefined*/
    35. if (val == null) {
    36. return {
    37. 'wch': 10
    38. };
    39. }
    40. /*再判断是否为中文*/
    41. else if (val.toString().charCodeAt(0) > 255) {
    42. return {
    43. 'wch': val.toString().length * 2
    44. };
    45. } else {
    46. return {
    47. 'wch': val.toString().length
    48. };
    49. }
    50. }))
    51. /*以第一行为初始值*/
    52. let result = colWidth[0];
    53. for (let i = 1; i < colWidth.length; i++) {
    54. for (let j = 0; j < colWidth[i].length; j++) {
    55. if (result[j]['wch'] < colWidth[i][j]['wch']) {
    56. result[j]['wch'] = colWidth[i][j]['wch'];
    57. }
    58. }
    59. }
    60. ws['!cols'] = result;
    61. }
    62. /* add worksheet to workbook */
    63. wb.SheetNames.push(ws_name);
    64. wb.Sheets[ws_name] = ws;
    65. var wbout = XLSX.write(wb, {
    66. bookType: bookType,
    67. bookSST: false,
    68. type: 'binary'
    69. });
    70. saveAs(new Blob([s2ab(wbout)], {
    71. type: "application/octet-stream"
    72. }), `${filename}.${bookType}`);
    73. }

    3、页面中的使用,一些关键变量需要的格式贴在文章最后

    1. //html部分
    2. size="small"
    3. icon="el-icon-download"
    4. @click="exportDataEvent"
    5. circle
    6. >
    7. //js部分
    8. exportDataEvent() {
    9. this.handleExportMarketingList(information,tableColumn);},
    10. // 导出方法
    11. handleExportMarketingList(information, optionColumnList) {
    12. import("_b/pages/utils/vendor/Export2Excel.js").then((excel) => {//根据实际路径进行修改
    13. let tHeader = [];
    14. optionColumnList.forEach((ele) => {
    15. tHeader.push(ele.title);
    16. });
    17. let multiHeader = [[]];
    18. tHeader.forEach((ele, index) => {
    19. index === 0
    20. ? multiHeader[0].push("汇总表")
    21. : multiHeader[0].push("");
    22. });
    23. let multiHeader2 = [[]];
    24. tHeader.forEach((ele, index) => {
    25. index === 0
    26. ? multiHeader2[0].push(`名称:${this.proName}`)
    27. : multiHeader2[0].push("");
    28. });
    29. multiHeader2[0].splice(
    30. multiHeader2[0].length - 1,
    31. 1,
    32. `编号:${this.proCode}`
    33. );
    34. const filterVal = optionColumnList;
    35. let data = information.map((v) => filterVal.map((j) => v[j.field]));
    36. const englishWordList = [
    37. "A",
    38. "B",
    39. "C",
    40. "D",
    41. "E",
    42. "F",
    43. "G",
    44. "H",
    45. "I",
    46. "J",
    47. "K",
    48. "L",
    49. "M",
    50. "N",
    51. "O",
    52. "P",
    53. "Q",
    54. "R",
    55. "S",
    56. "T",
    57. "U",
    58. "V",
    59. "W",
    60. "X",
    61. "Y",
    62. "Z",
    63. ];
    64. // 进行所有表头的单元格合并
    65. let typeList = [];
    66. data.forEach((ele) => {
    67. typeList.push(ele[0]);
    68. });
    69. data = data.concat(this.footerDataList);
    70. let merges = [
    71. `A1:${englishWordList[tHeader.length - 1]}1`,
    72. `A2:${englishWordList[tHeader.length - 2]}2`,
    73. ];
    74. for (let i = 1; merges.length < (data.length - 4) / 2 + 2; i += 2) {
    75. merges.push(`A${3 + i}:A${4 + i}`);
    76. }
    77. merges.push(`A${data.length}:B${data.length}`);
    78. merges.push(`A${data.length + 1}:B${data.length + 1}`);
    79. merges.push(`A${data.length + 2}:B${data.length + 2}`);
    80. merges.push(`A${data.length + 3}:B${data.length + 3}`);
    81. excel.export_json_to_excel({
    82. multiHeader,
    83. multiHeader2,
    84. header: tHeader,
    85. data,
    86. filename: "汇总",
    87. merges,
    88. });
    89. });
    90. },

     

     

     

  • 相关阅读:
    Linux的root用户
    C++题解(6) 信息学奥赛一本通:2069:【例2.12 】糖果游戏
    使用JAVA pdf转word
    翻车了,被读者找出 BUG
    Rocky linux8.8系统通过packstack安装OpenStack yoga版本
    文本变成文本路径图 保存txt
    Kubernetes学习大纲
    [elastic 8.x]java客户端连接elasticsearch与操作索引与文档
    Java:SpringBoot实现JDK动态代理和CGLIB动态代理
    蓝桥杯每日一题20223.9.26
  • 原文地址:https://blog.csdn.net/weixin_45294459/article/details/127107399