Skip to content

Low-order non-printable ASCII characters in worksheet name should be escaped or removed #654

@khiav223577

Description

@khiav223577

The worksheet name was escaped as HTML since #112.
But It seems to be not working with some special characters, "\b" character for example.
I think it should be escaped as XML.

See: https://support.microsoft.com/en-us/help/315580/prb-error-message-when-an-xml-document-contains-low-order-ascii-charac

The following are the character ranges for low-order non-printable ASCII characters that are rejected by MSXML versions 3.0 and later:
#x0 - #x8 (ASCII 0 - 8)
#xB - #xC (ASCII 11 - 12)
#xE - #x1F (ASCII 14 - 31)

Steps to reproduce

  1. Export an excel file by aslsx.
require 'axlsx'

Axlsx::Package.new do |p|
  p.workbook.add_worksheet(:name => "My sheet\b") do |sheet|
    sheet.add_row ["Data1", "Data2"]
  end
  p.serialize('simple1.xlsx')
end
  1. Double click simple1.xlsx to open it.

It will say that the excel is corrupted.
2020-08-24_154115

Possible fixes

We can use Builder::XChar.encode to escape the worksheet name.

require 'axlsx'

Axlsx::Package.new do |p|
  p.workbook.add_worksheet(:name => Builder::XChar.encode("My sheet\b")) do |sheet|
    sheet.add_row ["Data1", "Data2"]
  end
  p.serialize('simple2.xlsx')
end

The produced excel file simple2.xlsx works fine.
2020-08-24_154436

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions