当前位置:网站首页>Go exceed API source code reading (III) -- openreader ()

Go exceed API source code reading (III) -- openreader ()

2022-07-19 13:49:00 ReganYue

Go-Excelize API Source code reading ( 3、 ... and )——OpenReader()

Open source star picking program (WeOpen Star) By Tengyuan society 2022 A new project launched in , Designed to provide growth incentives for open source people , Provide growth support for open source projects , Help developers better understand open source , Cross the gap faster , Participate in the specific contribution and practice of open source .

Whether you are open source Mengxin , Or veterans who want to participate more deeply in open source contributions , Follow “ Open source star picking program ” Start your open source journey , From a learning note 、 To the submission of a piece of code , Keep tapping your potential , Eventually grow into an open source community “ Blazing Star ”.

We will be with you , Explore more possibilities !

Project address : WeOpen-Star:https://github.com/weopenprojects/WeOpen-Star

One 、Go-Excelize brief introduction

Excelize yes Go Language written for operation Office Excel Document base library , be based on ECMA-376,ISO/IEC 29500 international standard . You can use it to read 、 Written by Microsoft Excel 2007 Spreadsheet documents created by and above . Support XLAM / XLSM / XLSX / XLTM / XLTX And so on , Highly compatible with styles 、 picture ( surface )、 PivotTable 、 Slicer and other complex components of the document , And provide streaming reading and writing API, For workbooks that contain large amounts of data . It can be applied to various report platforms 、 Cloud computing 、 Edge computing and other systems . Required to use this class library Go The language is 1.15 Or later .

Two 、OpenReader()

func OpenReader(r io.Reader, opt ...Options) (*File, error)OpenReader The function of is from io.Reader Read data stream .

func OpenReader(r io.Reader, opt ...Options) (*File, error) {
    
	b, err := ioutil.ReadAll(r)
	if err != nil {
    
		return nil, err
	}
	f := newFile()
	f.options = parseOptions(opt...)
	if f.options.UnzipSizeLimit == 0 {
    
		f.options.UnzipSizeLimit = UnzipSizeLimit
		if f.options.UnzipXMLSizeLimit > f.options.UnzipSizeLimit {
    
			f.options.UnzipSizeLimit = f.options.UnzipXMLSizeLimit
		}
	}
	if f.options.UnzipXMLSizeLimit == 0 {
    
		f.options.UnzipXMLSizeLimit = StreamChunkSize
		if f.options.UnzipSizeLimit < f.options.UnzipXMLSizeLimit {
    
			f.options.UnzipXMLSizeLimit = f.options.UnzipSizeLimit
		}
	}
	if f.options.UnzipXMLSizeLimit > f.options.UnzipSizeLimit {
    
		return nil, ErrOptionsUnzipSizeLimit
	}
	if bytes.Contains(b, oleIdentifier) {
    
		if b, err = Decrypt(b, f.options); err != nil {
    
			return nil, ErrWorkbookFileFormat
		}
	}
	zr, err := zip.NewReader(bytes.NewReader(b), int64(len(b)))
	if err != nil {
    
		if len(f.options.Password) > 0 {
    
			return nil, ErrWorkbookPassword
		}
		return nil, err
	}
	file, sheetCount, err := f.ReadZipReader(zr)
	if err != nil {
    
		return nil, err
	}
	f.SheetCount = sheetCount
	for k, v := range file {
    
		f.Pkg.Store(k, v)
	}
	f.CalcChain = f.calcChainReader()
	f.sheetMap = f.getSheetMap()
	f.Styles = f.stylesReader()
	f.Theme = f.themeReader()
	return f, nil
}

ioutil.ReadAll First from io.Reader Read the whole data stream ,newFile() Create a Excel Workbooks .

f.options.UnzipSizeLimit: UnzipSizeLimit Specifies the decompression size limit when opening the spreadsheet , In bytes , This value should be greater than or equal to UnzipXMLSizeLimit, The default size limit is 16GB.

f.options.UnzipXMLSizeLimit:UnzipXMLSizeLimit Specify memory limits for decompressing worksheets and shared string tables , The unit is byte , When the file size exceeds this value , On the worksheet XML Will be extracted into the temporary directory of the system , This value should be less than or equal to UnzipSizeLimit, The default value is 16MB.

UnzipSizeLimit = 1000 << 24
StreamChunkSize = 1 << 24

Then judge whether the read data stream contains oleIdentifier = []byte{0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1} This is a OLE Program identifier , See Microsoft documentation for details :https://docs.microsoft.com/zh-cn/office/vba/outlook/concepts/getting-started/ole-programmatic-identifiers-outlook

zip.NewReader Return a return from bytes.NewReader(b) Read the new reader , It is assigned a given byte size int64(len(b)).

f.ReadZipReader(zr) in ReadZipReader Extract the spreadsheet with the given options . The given options are mainly passwords :

type Options struct {
	MaxCalcIterations uint
	Password          string
	RawCellValue      bool
	UnzipSizeLimit    int64
	UnzipXMLSizeLimit int64
}

Later and new file API almost NewFile(), to f Member assignment of structure .

3、 ... and 、 Conclusion
This is Lao Yue , This is a Go Interpretation of language related source code part III , I'll keep trying , Bring you more similar articles , Please give me your advice .

原网站

版权声明
本文为[ReganYue]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207171915019992.html