当前位置:网站首页>Go exceed API source code reading (IV) -- save (), SaveAs (name string)
Go exceed API source code reading (IV) -- save (), SaveAs (name string)
2022-07-27 00:15:00 【ReganYue】
Go-Excelize API Source code reading ( Four )——Save()
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 、Save()
func (f *File) Save() error
Use Save Save pairs Excel Editing of documents .
func (f *File) Save() error {
if f.Path == "" {
return ErrSave
}
if f.options != nil {
return f.SaveAs(f.Path, *f.options)
}
return f.SaveAs(f.Path)
}
Save It can be used to overwrite the spreadsheet in the source path .
first if Judge , Is to judge whether the path to save is empty , If it is empty, return ErrSave.
// ErrSave defined the error message for saving file.
ErrSave = errors.New("no path defined for file, consider File.WriteTo or File.Write")
This item places errors in errors.go in .ErrSave Is the error message of an error when saving the file .
Next if Judge :
if f.options != nil {
return f.SaveAs(f.Path, *f.options)
}
When the file structure options When the member is not empty , Saving files requires consideration options Information about .
If it is empty, there is no need to consider .
3、 ... and 、SaveAs(name string)
as for SaveAs(name string), Let's see :
Use SaveAs preservation Excel The document is the specified file .
func (f *File) SaveAs(name string, opt ...Options) error {
if len(name) > MaxFilePathLength {
return ErrMaxFilePathLength
}
f.Path = name
contentType, ok := map[string]string{
".xlam": ContentTypeAddinMacro,
".xlsm": ContentTypeMacro,
".xlsx": ContentTypeSheetML,
".xltm": ContentTypeTemplateMacro,
".xltx": ContentTypeTemplate,
}[filepath.Ext(f.Path)]
if !ok {
return ErrWorkbookFileFormat
}
f.setContentTypePartProjectExtensions(contentType)
file, err := os.OpenFile(filepath.Clean(name), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
if err != nil {
return err
}
defer file.Close()
f.options = nil
for i := range opt {
f.options = &opt[i]
}
return f.Write(file)
}
first if:
if len(name) > MaxFilePathLength {
return ErrMaxFilePathLength
}
Used to judge whether the length of the file name is legal .
The file error message is as follows :
Next, determine the content type according to the file extension :
contentType, ok := map[string]string{
".xlam": ContentTypeAddinMacro,
".xlsm": ContentTypeMacro,
".xlsx": ContentTypeSheetML,
".xltm": ContentTypeTemplateMacro,
".xltx": ContentTypeTemplate,
}[filepath.Ext(f.Path)]
The writing method of this paragraph can be learned ... As for its content, the following figure can be seen ...
setContentTypePartProjectExtensions() Will get contentType Assign to document relation part and main document part .
Then open the file and write f File stream .
os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm
I want to introduce this :
os.O_APPEND: When writing to a file , Append new content to existing content .
os.O_CREATE: When the file on the given path does not exist , Create a new file .
os.O_EXCL: Need and os.O_CREATE Use together , Indicates that there cannot be an existing file on the given path .
os.O_SYNC: Synchronize on open files I/O. It ensures that the contents read and written are always synchronized with the data on the hard disk .
os.O_TRUNC: If the file already exists , And it's a regular file , Then empty anything that already exists .
os.O_WRONLY | os.O_CREATE | O_EXCL 【 If it already exists , The failure 】
os.O_WRONLY | os.O_CREATE 【 If it already exists , Will cover up and write , Will not empty the original file , It's all over the top 】
os.O_WRONLY | os.O_CREATE | os.O_APPEND 【 If it already exists , Then add at the end 】
边栏推荐
- 深度学习调参技巧
- NFT display guide: how to display your NFT collection
- 卷积神经网络——LeNet(pytorch实现)
- MySQL transaction, phantom reading, current reading, snapshot reading related notes
- Chapter 1 requirements analysis and SSM environment preparation
- Azure synapse analytics Performance Optimization Guide (4) -- optimize performance using result set caching
- 买不到的数目
- Double. isNaN(double var)
- 生成yolov5.wts文件出错
- Typesript generic constraint
猜你喜欢

The basic operation of data tables in MySQL is very difficult. This experiment will take you through it from the beginning

Hcip day 2_ HCIA review comprehensive experiment

C language array

动态sql

Pytorch data pipeline standardized code template

About no module named'django.db.backends.mysql'

4-4 object lifecycle
![[C language] array](/img/b7/fe090984af689e45cf3492ff8d4c61.png)
[C language] array

第1章 开发第一个restful应用

What scenarios are Tencent cloud lightweight application servers suitable for?
随机推荐
DHCP, VLAN, NAT, large comprehensive experiment
LeetCode题目——数组篇
C and pointer Chapter 18 runtime environment 18.1 judgment of runtime environment
Iptables prevent nmap scanning and binlog
Baidu website Collection
Chapter 1 requirements analysis and SSM environment preparation
Codeforces d.constructing the array (priority queue)
第1章 拦截器入门及使用技巧
Upload files to OSS file server
Modulo (remainder) operation in the range of real numbers: how to find the remainder of negative numbers
Bid farewell to wide tables and achieve a new generation of Bi with DQL
Transformers is a graph neural network
C语言数组
100. Same tree
Hcip day 2_ HCIA review comprehensive experiment
Anaconda => PyCharm => CUDA => cudnn => PyTorch 环境配置
Last week's hot review (7.11-7.17)
14_ Basic list
Method of setting QQ to blank ID
Recent answers - column