4

I use SheetJS to export data to excel and looks like excel itself repair data. Data download good, but is it ok to receive this warning? I've got and file.xlsx from UI, and when I open it via excel I received some warning was produced by Excel itself. screenshot_5 screenshot_4

Code:

const download = (url, name) => {
    let a = document.createElement('a')
    a.href = url
    a.download = name
    a.click()

    window.URL.revokeObjectURL(url)
}

function Workbook() {
    if (!(this instanceof Workbook))
        return new Workbook()

    this.SheetNames = []

    this.Sheets = {}
}

function s2ab(s) {
    const buf = new ArrayBuffer(s.length)

    const view = new Uint8Array(buf)

    for (let i=0; i !== s.length; ++i)
        view[i] = s.charCodeAt(i) & 0xFF

    return view
}

export default data => {
    import('xlsx').then(XLSX => {
        const wb = new Workbook()
        const ws = XLSX.utils.json_to_sheet(data)

        wb.SheetNames.push('s')
        wb.Sheets[''] = ws

        const wbout = XLSX.write(wb, {bookType:'xlsx', bookSST:true, type: 'binary'})

        let url = window.URL.createObjectURL(new Blob([s2ab(wbout)], {type:'application/octet-stream'}))

        download(url, 'import.xlsx')
    })
}
Palaniichuk Dmytro
  • 2,943
  • 12
  • 36
  • 66

3 Answers3

8

Sheet name length should not be more than 31. Microsoft Excel gives error if Sheet Name's length is more than 31.

snjha
  • 81
  • 1
  • 1
2

I it was obvious issue, to fix this, I set:

 const wbSheetNames = 'Custom Name'
        wb.SheetNames.push(wbSheetNames)
        wb.Sheets[wbSheetNames] = ws
Palaniichuk Dmytro
  • 2,943
  • 12
  • 36
  • 66
0

Excel will report the same error if you use invalid characters when naming sheets: \ / ? * [ ] and :

MetalMikester
  • 1,067
  • 1
  • 13
  • 15