当前位置:网站首页>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 】
边栏推荐
- Analysis of encoding and decoding of encode() and decode(), common encoding and why encode and decode are needed
- 3 esp8266 nodemcu network server
- Method of setting QQ to blank ID
- PTA 7-4 small generation (DFS)
- 带你熟悉云网络的“电话簿”:DNS
- Question 152: product maximum subarray
- 第7章 课程总结
- In simple terms, cchart's daily lesson - Lesson 59 of happy high school 4 comes to the same end by different ways, and the C code style of the colorful interface library
- ResNet论文解读及代码实现(pytorch)
- 04 traditional synchronized lock
猜你喜欢

Database: MySQL foundation +crud basic operation

3 esp8266 nodemcu network server

Midge paper reading notes

卷积神经网络——LeNet(pytorch实现)

08 design of intelligent agricultural environmental monitoring system based on ZigBee
![[C language] array](/img/b7/fe090984af689e45cf3492ff8d4c61.png)
[C language] array
![[literature reading] an investigation on hardware aware vision transformer scaling](/img/3d/6f2cf1fc1e9189e7557703820d021f.png)
[literature reading] an investigation on hardware aware vision transformer scaling

The difference between SQL join and related subinquiry

LeetCode题目——二叉树篇

Anaconda => PyCharm => CUDA => cudnn => PyTorch 环境配置
随机推荐
LeetCode题目——二叉树篇
[Gorm] model relationship -hasone
Relationship between Unicode and UTF-8
Last week's hot review (7.11-7.17)
01 knapsack problem 416. Segmentation and equal sum subset -494. Goal and
Question 152: product maximum subarray
The place where the dream begins ---- first knowing C language (2)
07 design of ponding monitoring system based on 51 single chip microcomputer
Push to origin/master was rejected error resolution
12_ Binding style
08_ Event modifier
Pytorch data pipeline standardized code template
[step by step, even thousands of miles] key words in the specified time period of the statistical log
2022.7.18-----leetcode.749
3 esp8266 nodemcu network server
分页插件--PageHelper
NFT display guide: how to display your NFT collection
Relationship between limit, continuity, partial derivative and total differential of multivariate function (learning notes)
LeetCode——链表篇
DHCP, VLAN, NAT, large comprehensive experiment