github.com/shakinm/xlsReader@v0.9.12/xls/xls.go (about) 1 package xls 2 3 import ( 4 "encoding/binary" 5 "github.com/shakinm/xlsReader/cfb" 6 "io" 7 ) 8 9 // OpenFile - Open document from the file 10 func OpenFile(fileName string) (workbook Workbook, err error) { 11 12 adaptor, err := cfb.OpenFile(fileName) 13 14 defer adaptor.CloseFile() 15 16 if err != nil { 17 return workbook, err 18 } 19 20 return openCfb(adaptor) 21 } 22 23 // OpenReader - Open document from the file reader 24 func OpenReader(fileReader io.ReadSeeker) (workbook Workbook, err error) { 25 26 adaptor, err := cfb.OpenReader(fileReader) 27 28 if err != nil { 29 return workbook, err 30 } 31 return openCfb(adaptor) 32 } 33 34 // OpenFile - Open document from the file 35 func openCfb(adaptor cfb.Cfb) (workbook Workbook, err error) { 36 var book *cfb.Directory 37 var root *cfb.Directory 38 for _, dir := range adaptor.GetDirs() { 39 fn := dir.Name() 40 41 if fn == "Workbook" { 42 if book == nil { 43 book = dir 44 } 45 } 46 if fn == "Book" { 47 book = dir 48 49 } 50 if fn == "Root Entry" { 51 root = dir 52 } 53 54 } 55 56 if book != nil { 57 size := binary.LittleEndian.Uint32(book.StreamSize[:]) 58 59 reader, err := adaptor.OpenObject(book, root) 60 61 if err != nil { 62 return workbook, err 63 } 64 65 return readStream(reader, size) 66 67 } 68 69 return workbook, err 70 } 71 72 func readStream(reader io.ReadSeeker, streamSize uint32) (workbook Workbook, err error) { 73 74 stream := make([]byte, streamSize) 75 76 _, err = reader.Read(stream) 77 78 if err != nil { 79 return workbook, nil 80 } 81 82 if err != nil { 83 return workbook, nil 84 } 85 86 err = workbook.read(stream) 87 88 if err != nil { 89 return workbook, nil 90 } 91 92 for k := range workbook.sheets { 93 sheet, err := workbook.GetSheet(k) 94 95 if err != nil { 96 return workbook, nil 97 } 98 99 err = sheet.read(stream) 100 101 if err != nil { 102 return workbook, nil 103 } 104 } 105 106 return 107 }