import FileSaver from "file-saver";
import XLSX from "xlsx";

const htmlToExcel = {
  getExcel(dom, title = "表格") {
    // 设置当前日期
    const time = new Date();
    const year = time.getFullYear();
    const month = time.getMonth() + 1;
    const day = time.getDate();
    const name = year + "-" + month + "-" + day;
    var xlsxParam = { raw: true }; //转换成excel时，使用原始的格式
    const wb = XLSX.utils.table_to_book(document.querySelector(dom), xlsxParam);
    /* 获取二进制字符串作为输出 */
    const wbout = XLSX.write(wb, {
      bookType: "xlsx",
      bookSST: true,
      type: "array",
    });
    try {
      FileSaver.saveAs(
        new Blob([wbout], { type: "application/octet-stream" }),
        name + title + ".xlsx"
      );
    } catch (e) {
      if (typeof console !== "undefined") console.log(e, wbout);
    }
    return wbout;
  },
};

export default htmlToExcel;
// import FileSaver from "file-saver";
// import XLSX from "xlsx";

// const htmlToExcel = {
//   getExcel(dom, title = "表格") {
//     var excelTitle = title;
//     var wb = XLSX.utils.table_to_book(document.querySelector(dom));
//     /* 获取二进制字符串作为输出 */
//     var wbout = XLSX.write(wb, {
//       bookType: "xlsx",
//       bookSST: true,
//       type: "array",
//     });
//     try {
//       FileSaver.saveAs(
//         new Blob([wbout], { type: "application/octet-stream" }),
//         excelTitle + ".xlsx"
//       );
//     } catch (e) {
//       if (typeof console !== "undefined") console.log(e, wbout);
//     }
//     return wbout;
//   },
// };

// export default htmlToExcel;
