xls.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Package xls imports Excel 97-2003 workbooks (.xls, BIFF8 - and BIFF5, the
  3. Excel 5/95 format) by converting them into Excel 2007+ workbooks (.xlsx),
  4. which package xlsx then reads into the Sheets model.
  5. A .xls file is an OLE2 compound file (internal/cfb) holding a "Workbook"
  6. stream (BIFF8; "Book" for BIFF5) of records: the workbook globals, then
  7. one substream per sheet. The conversion follows the layout of
  8. LibreOffice's Excel import (sc/source/filter/excel): globals first
  9. (workbook.go), each sheet's cells and properties (sheet.go), formulas
  10. decompiled from their token form (formula.go), and the result written as
  11. SpreadsheetML (xlsx.go).
  12. Imported: values, shared strings, formulas (shared and array formulas,
  13. 3D references, defined names, add-in and newer functions), cached
  14. formula results, number formats, fonts, fills, borders, alignment,
  15. column widths, row heights, hidden rows and columns, merged cells,
  16. frozen panes, comments, hyperlinks (as HYPERLINK formulas) and sheet tab
  17. colours. Not imported: charts, pictures and other drawing objects,
  18. conditional formats, data validation and VBA. Password-protected
  19. workbooks are refused with an explanation.
  20. */
  21. package xls
  22. import (
  23. "errors"
  24. "imuslab.com/arozos/mod/office/internal/cfb"
  25. "imuslab.com/arozos/mod/office/model"
  26. "imuslab.com/arozos/mod/office/xlsx"
  27. )
  28. // ToXlsx converts an Excel 97-2003 workbook into an .xlsx file
  29. func ToXlsx(data []byte) ([]byte, error) {
  30. b, sheets, index, err := read(data)
  31. if err != nil {
  32. return nil, err
  33. }
  34. return b.writeXlsx(sheets, index)
  35. }
  36. // Parse converts an Excel 97-2003 workbook into the Sheets model
  37. func Parse(data []byte) (*model.Workbook, error) {
  38. x, err := ToXlsx(data)
  39. if err != nil {
  40. return nil, err
  41. }
  42. return xlsx.Parse(x)
  43. }
  44. // read decodes the workbook: the globals, then each worksheet in tab
  45. // order; index holds each sheet's BOUNDSHEET position
  46. func read(data []byte) (*book, []*sheet, []int, error) {
  47. if !cfb.IsCompound(data) {
  48. if len(data) >= 4 && data[0] == 0x09 && (data[1] == 0x00 || data[1] == 0x02 || data[1] == 0x04) {
  49. return nil, nil, nil, errors.New("Excel 2.x - 4.x worksheets are not supported")
  50. }
  51. return nil, nil, nil, errors.New("not an Excel 97-2003 workbook")
  52. }
  53. cf, err := cfb.Open(data)
  54. if err != nil {
  55. return nil, nil, nil, err
  56. }
  57. var stream []byte
  58. switch {
  59. case cf.Has("Workbook"):
  60. stream, err = cf.Stream("Workbook")
  61. case cf.Has("Book"):
  62. stream, err = cf.Stream("Book")
  63. default:
  64. return nil, nil, nil, errors.New("not an Excel workbook (no Workbook stream)")
  65. }
  66. if err != nil {
  67. return nil, nil, nil, err
  68. }
  69. recs, err := readRecords(stream)
  70. if err != nil {
  71. return nil, nil, nil, err
  72. }
  73. if len(recs) == 0 || recs[0].typ != rtBOF || len(recs[0].data) < 4 {
  74. return nil, nil, nil, errors.New("the workbook stream does not start with a BOF record")
  75. }
  76. vers := uint16(recs[0].data[0]) | uint16(recs[0].data[1])<<8
  77. b := newBook(vers >= 0x0600)
  78. b.records = recs
  79. if err := b.parseGlobals(); err != nil {
  80. return nil, nil, nil, err
  81. }
  82. byPos := make(map[int]int, len(recs))
  83. for i, r := range recs {
  84. if r.typ == rtBOF {
  85. byPos[r.pos] = i
  86. }
  87. }
  88. var sheets []*sheet
  89. var index []int
  90. for i, bs := range b.sheets {
  91. if bs.kind != 0 && bs.kind != 1 {
  92. continue // chart sheets and VBA modules
  93. }
  94. start, ok := byPos[bs.pos]
  95. if !ok {
  96. continue
  97. }
  98. sheets = append(sheets, b.parseSheet(start, bs.name))
  99. index = append(index, i)
  100. }
  101. if len(sheets) == 0 {
  102. return nil, nil, nil, errors.New("the workbook has no worksheets")
  103. }
  104. return b, sheets, index, nil
  105. }