Git Product home page Git Product logo

xlsreader's Introduction

xlsReader

Библиотека для чтения xls файлов / Golang read xls library

Установка / Installation

$ go get github.com/shakinm/xlsReader

Использование / Using

package main

import (
	"fmt"
	"github.com/shakinm/xlsReader/xls"
	"log"
)

func main() {

	workbook, err := xls.OpenFile("small_1_sheet.xls")

	if err!=nil {
		log.Panic(err.Error())
	}

	// Кол-во листов в книге
	// Number of sheets in the workbook
	//
	// for i := 0; i <= workbook.GetNumberSheets()-1; i++ {}

	fmt.Println(workbook.GetNumberSheets())

	sheet, err := workbook.GetSheet(0)

	if err!=nil {
		log.Panic(err.Error())
	}

	// Имя листа
	// Print sheet name
	println(sheet.GetName())

	// Вывести кол-во строк в листе
	// Print the number of rows in the sheet
	println(sheet.GetNumberRows())

	for i := 0; i <= sheet.GetNumberRows(); i++ {
		if row, err := sheet.GetRow(i); err == nil {
			if cell, err := row.GetCol(1); err == nil {

				// Значение ячейки, тип строка
				// Cell value, string type
				fmt.Println(cell.GetString())

				//fmt.Println(cell.GetInt64())
				//fmt.Println(cell.GetFloat64())

				// Тип ячейки (записи)
				// Cell type (records)
				fmt.Println(cell.GetType())

				// Получение отформатированной строки, например для ячеек с датой или проценты
				// Receiving a formatted string, for example, for cells with a date or a percentage
				xfIndex:=cell.GetXFIndex()
				formatIndex:=workbook.GetXFbyIndex(xfIndex)
				format:=workbook.GetFormatByIndex(formatIndex.GetFormatIndex())
				fmt.Println(format.GetFormatString(cell))

			}

		}
	}
}

xlsreader's People

Contributors

devxpro avatar kleeon avatar larii75 avatar max107 avatar shakinm avatar shengdoushi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

xlsreader's Issues

Consider exporting `sheet` structure

The sheet structure is not exported and so unavailable for our own use. GetSheet() returns a sheet, but it would be helpful if we could save that type in our own structs.

type foo struct {
  sheet xls.Sheet
}

Slice bounds out of range

Version: 0.9.10
Stack trace:

panic: runtime error: slice bounds out of range [:17861] with capacity 3993

goroutine 1 [running]:
github.com/shakinm/xlsReader/xls.(*Workbook).read(0xc000110240, 0xc00012b000, 0xf99, 0xf99, 0xf99, 0x0)
	/Users/juris/Work/golang/pkg/mod/github.com/shakinm/[email protected]/xls/workbook.go:87 +0x159f
github.com/shakinm/xlsReader/xls.readStream(0x1147ae8, 0xc00007a360, 0xc000000f99, 0x1147ae8, 0xc00007a360, 0x0, 0x0, 0x30, 0x28, 0x110b340, ...)
	/Users/juris/Work/golang/pkg/mod/github.com/shakinm/[email protected]/xls/xls.go:84 +0xc8
github.com/shakinm/xlsReader/xls.openCfb(0xe11ab1a1e011cfd0, 0x0, 0x0, 0x9fffe0003003e, 0x6, 0x100000000, 0x100000000, 0x1100001000, 0xffffffff00000001, 0x1200000000, ...)
	/Users/juris/Work/golang/pkg/mod/github.com/shakinm/[email protected]/xls/xls.go:62 +0x22c
github.com/shakinm/xlsReader/xls.OpenFile(0x7ffeefbff919, 0x3c, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
	/Users/juris/Work/golang/pkg/mod/github.com/shakinm/[email protected]/xls/xls.go:17 +0x17c
main.main()
	main2.go:28 +0x8f
exit status 2

Died on reading file workbook, err := xls.OpenFile(*filename)

Can read the same file with PHP package phpoffice/phpspreadsheet
PHP found 0x31 4 symbols, this package find only 3 and next is 0x6d, but should be 0x31

Input file:
Untitled 5.xls.zip

Index out of range

Version 0.9.7

Stack trace

panic: runtime error: index out of range [18963] with length 17388
goroutine 75 [running]:
github.com/shakinm/xlsReader/xls/record.(*LabelSSt).GetString(0xc0004033a0, 0xc15b6c, 0x10)
	/gocode/pkg/mod/github.com/shakinm/[email protected]/xls/record/labelSst.go:43 +0x70
github.com/shakinm/xlsReader/xls/record.(*Format).GetFormatString(0xc0000aaa20, 0x1043a20, 0xc0004033a0, 0x16d6740, 0x1)
	/gocode/pkg/mod/github.com/shakinm/[email protected]/xls/record/format.go:125 +0xa85
github.com/dfurmanov/myapp/officetotxt.XLStoCSV(0x102dee0, 0xc0003025a0, 0xc0003c6630, 0x29, 0x0, 0xc0003c664f, 0x6)
	/gocode/src/github.com/dfurmanov/myapp/officetotxt/xls2csv.go:30 +0x30a

My code (sheetIndex = 0)

func XLStoCSV(w io.Writer, excelFileName string, sheetIndex int) error {
	workbook, err := xls.OpenFile(excelFileName)
	if err != nil {
		return err
	}

	sheet, err := workbook.GetSheet(sheetIndex)
	if err != nil {
		return err
	}

	cw := csv.NewWriter(w)

	for i := 0; i <= sheet.GetNumberRows(); i++ {
		if row, err := sheet.GetRow(i); err == nil {
			cols := row.GetCols()
			values := make([]string, len(cols))
			for i, cell := range cols {
				xfIndex := cell.GetXFIndex()
				formatIndex := workbook.GetXFbyIndex(xfIndex)
				format := workbook.GetFormatByIndex(formatIndex.GetFormatIndex())
				values[i] = format.GetFormatString(cell)
			}
			trimmedValues := TrimLatterEmptyValues(values)
			if len(trimmedValues) > 0 {
				err = cw.Write(trimmedValues)
				if err != nil {
					return err
				}
			}
		}
	}

	cw.Flush()
	return cw.Error()
}

Input file
error.xls.zip

Issue reading MulBlank record

OpenFile panics for some documents with this message:

panic: runtime error: slice bounds out of range [:960] with capacity 958

goroutine 1 [running]:
github.com/shakinm/xlsReader/xls/record.(*MulBlank).Read(0xc000076ce0, {0xc000a92c8d, 0xc00705eac8?, 0x4?})
C:/Users/####/go/pkg/mod/github.com/shakinm/[email protected]/xls/record/mulblank.go:61 +0x1fc
github.com/shakinm/xlsReader/xls.(*Sheet).read(0xc0000b81b0, {0xc000820000, 0x27304b?, 0x27304b})
C:/Users/####/go/pkg/mod/github.com/shakinm/[email protected]/xls/sheet.go:194 +0xf9e
github.com/shakinm/xlsReader/xls.readStream({0x10f28c8, 0xc0000704b0}, 0x27304b)
C:/Users/####/go/pkg/mod/github.com/shakinm/[email protected]/xls/xls.go:99 +0x1a5
github.com/shakinm/xlsReader/xls.openCfb({{{0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1}, {0x0, 0x0, ...}, ...}, ...})
C:/Users/####/go/pkg/mod/github.com/shakinm/[email protected]/xls/xls.go:65 +0x1df
github.com/shakinm/xlsReader/xls.OpenFile({0x10ce961?, 0xc000036000?})
C:/Users/####/go/pkg/mod/github.com/shakinm/[email protected]/xls/xls.go:20 +0x185
main.main()
C:/Users/####/Desktop/projects/excel_xml/main.go:98 +0x33
exit status 2

I tracked down the issue to this line:

sPoint := 4 + (i * 6)

According to Microsoft docs, IXFCell structure is 2 bytes long, not 6. So the indexing seems to be off

rk-num might be a negative integer

rk-num is a signed int32 or float64
in xlsReader/xls/structure/RKNum.go line: 14
it shout be

var val int32
binary.Read(bytes.NewBuffer(r[:]), binary.LittleEndian, &val)
val >>= 2

slice bounds out of range

panic: runtime error: slice bounds out of range [:240] with capacity 238

goroutine 23119 [running]:
github.com/shakinm/xlsReader/xls/record.(*MulBlank).Read(0xc00d474fc0, 0xc05841db25, 0x5e, 0xee)
C:/Users/Administrator/go/pkg/mod/github.com/shakinm/[email protected]/xls/record/mulblank.go:61 +0x256
github.com/shakinm/xlsReader/xls.(*Sheet).read(0xc00d8a9440, 0xc0570e0000, 0x133dc13, 0x133dc13, 0x0, 0x0)
C:/Users/Administrator/go/pkg/mod/github.com/shakinm/[email protected]/xls/sheet.go:193 +0x11ac
github.com/shakinm/xlsReader/xls.readStream(0x13b2e20, 0xc00d8a93e0, 0xc00133dc13, 0x13b2e20, 0xc00d8a93e0, 0x0, 0x0, 0x10215a0, 0x1, 0x1d20660, ...)
C:/Users/Administrator/go/pkg/mod/github.com/shakinm/[email protected]/xls/xls.go:97 +0x195
github.com/shakinm/xlsReader/xls.openCfb(0xe11ab1a1e011cfd0, 0x0, 0x0, 0x9fffe0003003e, 0x6, 0x13700000000, 0x1, 0x9b2900001000, 0x6ee00000001, 0x2, ...)
C:/Users/Administrator/go/pkg/mod/github.com/shakinm/[email protected]/xls/xls.go:62 +0x200
github.com/shakinm/xlsReader/xls.OpenFile(0xc01525e040, 0x20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
C:/Users/Administrator/go/pkg/mod/github.com/shakinm/[email protected]/xls/xls.go:17 +0x108
elk/xlsread.xlsnread(0xc01525e040, 0x20, 0x1, 0x20, 0x8, 0xc0224b53c0)
C:/Users/Administrator/go/src/elk/xlsread/xlsnew.go:20 +0x8e

Memory leak occurred while reading a corrupted file

When the program parses some corrupt Excel files, a memory overflow occurs, causing an error in the program。

Error like:

goroutine 68 [running]:
runtime.systemstack_switch()
        runtime/asm_amd64.s:459 fp=0xc0008136e0 sp=0xc0008136d8 pc=0x468140
runtime.(*mheap).alloc(0x1fd980000?, 0xfecc0?, 0x80?)
        runtime/mheap.go:904 +0x65 fp=0xc000813728 sp=0xc0008136e0 pc=0x427805
runtime.(*mcache).allocLarge(0x200?, 0x1fd980000, 0x1)
        runtime/mcache.go:233 +0x85 fp=0xc000813778 sp=0xc000813728 pc=0x416b45
runtime.mallocgc(0x1fd980000, 0x0, 0x0)
        runtime/malloc.go:1029 +0x57e fp=0xc0008137f0 sp=0xc000813778 pc=0x40cf7e
runtime.growslice(0xc000318000?, {0xc0994fe000?, 0xc0008138c8?, 0x82daec?}, 0xc000813978?)
        runtime/slice.go:284 +0x4ac fp=0xc000813858 sp=0xc0008137f0 pc=0x44ea2c
github.com/shakinm/xlsReader/cfb.(*Cfb).getDataFromFatChain(0xc000318000, 0x80?)
        github.com/shakinm/[email protected]/cfb/cfb.go:263 +0x105 fp=0xc0008138f0 sp=0xc000813858 pc=0x82e265
github.com/shakinm/xlsReader/cfb.(*Cfb).getDirectories(0xc000318000)
        github.com/shakinm/[email protected]/cfb/cfb.go:108 +0x36 fp=0xc0008139a8 sp=0xc0008138f0 pc=0x82d316
github.com/shakinm/xlsReader/cfb.open(0xe11ab1a1e011cfd0?)
        github.com/shakinm/[email protected]/cfb/cfb.go:80 +0x59 fp=0xc0008139c8 sp=0xc0008139a8 pc=0x82d179
github.com/shakinm/xlsReader/cfb.OpenReader(...)

You can downlaod example file here:
corrupt.xls

this new truct , have a panic: EOF

// NewMiniFatSector - Create new Sector struct for MiniFat
func NewMiniFatSector(header *Header) Sector {
return Sector{
SectorSize: 64,
Data: make([]byte, 64),
}
}

读取xls格式异常

2022/07/16 17:03:48 Identification signature for the compound file structure, and MUST be set to the value 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1
--- FAIL: TestReadXls (0.01s)
panic: Identification signature for the compound file structure, and MUST be set to the value 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1 [recovered]

m³乱码

读取xls时,单元格如果是m³会出现乱码

Getstring error Of index out of range

when read a xls with rgb in row.GetCol(i)
i=0 is ok but when i>0 is error
when I change the xls resave., is all ok

panic: runtime error: index out of range

goroutine 1 [running]:
github.com/shakinm/xlsReader/xls/record.(*LabelSSt).GetString(0xc0000045a0, 0xc000108100, 0x5)
d:/go/SOURCE/pkg/mod/github.com/shakinm/[email protected]/xls/record/labelSst.go:46 +0x75
main.xlsread(0x4f9e86, 0x19, 0x4b6f3f, 0xc000081f88, 0xc00001a0b8)
d:/go/source/src/git.intra/vivi_zhu/mail/dataelk/xlst1/main.go:57 +0xadc

read xls error

panic: runtime error: index out of range [6425] with length 2354
goroutine 28853 [running]:
runtime/debug.Stack()
D:/sdk/go/src/runtime/debug/stack.go:24 +0x65
runtime/debug.PrintStack()
D:/sdk/go/src/runtime/debug/stack.go:16 +0x19
github.com/xxl-job/xxl-job-executor-go.(*Task).Run.func1(0xc00216bc30)
D:/sdk/go/work/pkg/mod/github.com/xxl-job/[email protected]/task.go:31 +0x1a5
panic({0x1326f20, 0xc00f351c08})
D:/sdk/go/src/runtime/panic.go:838 +0x207
github.com/shakinm/xlsReader/xls/record.(*LabelSSt).GetString(0x12a4ac0?)
D:/sdk/go/work/pkg/mod/github.com/shakinm/[email protected]/xls/record/labelSst.go:43 +0x4b

Incorrect decoding of compressed characters

Decoding of compressed characters is not handled correclty:

func (r *LabelBIFF8) GetString() string {
	if int(r.grbit[0]) == 1 {
		name := helpers.BytesToUints16(r.rgb[:])
		runes := utf16.Decode(name)
		return string(runes)
	} else {
		return string(decodeWindows1251(r.rgb[:]))
	}
}

The code outputs incorrect result: Hinterfьllmaterial Ш 6 mm
Expected output Hinterfüllmaterial Ø 6 mm

Each compressed character(byte) has to be converted into unit16 and then decoded.
The line return string(decodeWindows1251(r.rgb[:])) should be replaced with:

name := []uint16{}
for _, b := range r.rgb[:] {
   name = append(name, uint16(b))
}
runes := utf16.Decode(name)
return string(runes)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.