收藏
回答

ios 13 openDocument打开xlsx文件打不开?

json2xls(XMLSheets, header) {

        console.log(XMLSheets)

        console.log(header)

        //XMLSheets 为Json数组, Header为字符串数组

        // excel 头部

        var XMLString = `

        <?xml version="1.0" encoding="UTF-8"?>

        <?mso-application progid= "Excel.Sheet"?>`

        XMLString += `<Workbook

        xmlns="urn:schemas-microsoft-com:office:spreadsheet"

        xmlns:o="urn:schemas-microsoft-com:office:office"

        xmlns:x="urn:schemas-microsoft-com:office:excel"

        xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"

        xmlns:html="http://www.w3.org/TR/REC-html40">`

        XMLString += `<Worksheet ss:Name="sheet 1">`

        // Table 头部

        XMLString += '<Table>'

        //遍历header插入Frist Row, Header

        XMLString += '<Row>'

        header.forEach((hd, i) => {

            XMLString += '<Cell>' + `<Data ss:Type="String">` + hd + '</Data></Cell>'

        })

        XMLString += '</Row>'

        // 遍历Data,插入数据Row

        // 检查是否有数据

        if (XMLSheets{

            console.log(header)

            XMLSheets.forEach((row) => {

                // 遍历数组,插入行

                XMLString += '<Row>'

                // 遍历Json,插入单元格

                for (var p in row{

                    // Cell 头部

                    XMLString += `<Cell><Data ss:Type="String">${row[p]}</Data></Cell>`

                }

                XMLString += '</Row>'


            }// 遍历 Cell

        }

        // Table 尾部

        XMLString += '</Table>'

        // Worksheet 尾部

        XMLString += '</Worksheet></Workbook>'

        return XMLString

    },

    imageBackClick() {

        let header = ["表头1", "表头2", "表头3", "表头4"]; //表头数据

        let jsonData = [{

                a: 0,

                b: 1,

                c: 2,

                d: 3

            }, {

                a: 0,

                b: 1,

                c: 2,

                d: 3

            },

            {

                a: 0,

                b: 1,

                c: 2,

                d: 3

            }, {

                a: 0,

                b: 1,

                c: 2,

                d: 3

            },

        ]; //自定义json数据

        let fieldtable = "名字"; //需要导出的文件名

        let xlsdata = this.json2xls(jsonData, header);

        const fsm = wx.getFileSystemManager();

        fsm.writeFile({

            filePath: wx.env.USER_DATA_PATH + '/' + fieldtable + '.xlsx',

            data: xlsdata,

            encoding: 'utf8',

            success: res => {

                wx.showModal({

                    title: '提示',

                    content: '如遇到文档格式显示问题,建议您使用金山文档或PC端office打开',

                    confirmText: '我知道了',

                    showCancel: false,

                    success: function (res) {

                        if (res.confirm{

                            wx.openDocument({

                                filePath: wx.env.USER_DATA_PATH + '/' + fieldtable + '.xlsx',

                                fileType: 'xlsx',

                                showMenu: true,

                                success: function (res) {

                                    console.log('打开文档成功')

                                },

                                fail: function () {

                                    console.log('打开失败');

                                }

                            })

                        } else if (res.cancel{

                            console.log('用户点击取消');

                        }

                    }

                });

            },

            fail: res => {

                console.info(res)

            }

        })

    },


回答关注问题邀请回答
收藏

2 个回答

  • 社区技术运营专员--许涛
    社区技术运营专员--许涛
    2022-07-07

    你好,请先自查用safari打开xlsx文件呢?

    2022-07-07
    有用
    回复
  • Demons
    Demons
    2022-07-07

    请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。

    2022-07-07
    有用
    回复
登录 后发表内容