網站首頁 編程語言 正文
一、Go-Excelize簡介
Excelize 是 Go 語言編寫的用于操作 Office Excel 文檔基礎庫,基于 ECMA-376,ISO/IEC 29500 國際標準。
可以使用它來讀取、寫入由 Microsoft Excel? 2007 及以上版本創建的電子表格文檔。
支持 XLAM / XLSM / XLSX / XLTM / XLTX 等多種文檔格式,高度兼容帶有樣式、圖片(表)、透視表、切片器等復雜組件的文檔,并提供流式讀寫 API,用于處理包含大規模數據的工作簿。
可應用于各類報表平臺、云計算、邊緣計算等系統。
使用本類庫要求使用的 Go 語言為 1.15 或更高版本。
二、GetSheetFormatPr
func (f *File) GetSheetFormatPr(sheet string, opts ...SheetFormatPrOptionsPtr) error
根據給定的工作表名稱獲取格式屬性。
可選格式參數 | 數據類型 |
---|---|
BaseColWidth | uint8 |
DefaultColWidth | float64 |
DefaultRowHeight | float64 |
CustomHeight | bool |
ZeroHeight | bool |
ThickTop | bool |
ThickBottom | bool |
使用示例:
f := excelize.NewFile() const sheet = "Sheet1" var ( baseColWidth excelize.BaseColWidth defaultColWidth excelize.DefaultColWidth defaultRowHeight excelize.DefaultRowHeight customHeight excelize.CustomHeight zeroHeight excelize.ZeroHeight thickTop excelize.ThickTop thickBottom excelize.ThickBottom ) if err := f.GetSheetFormatPr(sheet, &baseColWidth, &defaultColWidth, &defaultRowHeight, &customHeight, &zeroHeight, &thickTop, &thickBottom, ); err != nil { fmt.Println(err) } fmt.Println("Defaults:") fmt.Println("- baseColWidth:", baseColWidth) fmt.Println("- defaultColWidth:", defaultColWidth) fmt.Println("- defaultRowHeight:", defaultRowHeight) fmt.Println("- customHeight:", customHeight) fmt.Println("- zeroHeight:", zeroHeight) fmt.Println("- thickTop:", thickTop) fmt.Println("- thickBottom:", thickBottom)
輸出結果:
Defaults:
- baseColWidth: 0
- defaultColWidth: 0
- defaultRowHeight: 15
- customHeight: false
- zeroHeight: false
- thickTop: false
- thickBottom: false
廢話少說,上代碼:
func (f *File) GetSheetFormatPr(sheet string, opts ...SheetFormatPrOptionsPtr) error { s, err := f.workSheetReader(sheet) if err != nil { return err } fp := s.SheetFormatPr for _, opt := range opts { opt.getSheetFormatPr(fp) } return err }
代碼很簡單,先讀取工作表,然后獲取工作表的格式屬性,然后遍歷不定長參數opts,對fp的每個opt進行讀取。
SheetFormatPrOptionsPtr是一個interface。
type SheetFormatPrOptionsPtr interface { SheetFormatPrOptions getSheetFormatPr(formatPr *xlsxSheetFormatPr) }
該interface 內有兩個函數。
我們可以發現,他們都大同小異,第一步的if語句是判斷格式屬性是否存在,如果不存在就賦一個默認值。 然后取格式屬性指針fp的格式屬性,前面是類型轉換:
type xlsxSheetFormatPr struct { XMLName xml.Name `xml:"sheetFormatPr"` BaseColWidth uint8 `xml:"baseColWidth,attr,omitempty"` DefaultColWidth float64 `xml:"defaultColWidth,attr,omitempty"` DefaultRowHeight float64 `xml:"defaultRowHeight,attr"` CustomHeight bool `xml:"customHeight,attr,omitempty"` ZeroHeight bool `xml:"zeroHeight,attr,omitempty"` ThickTop bool `xml:"thickTop,attr,omitempty"` ThickBottom bool `xml:"thickBottom,attr,omitempty"` OutlineLevelRow uint8 `xml:"outlineLevelRow,attr,omitempty"` OutlineLevelCol uint8 `xml:"outlineLevelCol,attr,omitempty"` }
下面介紹一下各個參數的作用:
- BaseColWidth:指定普通樣式字體的最大數字寬度的字符數。此值不包括邊距填充或網格線的額外填充。它只是字符數。
- DefaultColWidth 指定默認列寬,其度量值為普通樣式字體的最大數字寬度的字符數。
- DefaultRowHeight 指定以磅值度量的默認行高,我們不必在所有行上寫入高度。如果大多數行具有自定義高度,則可以將其寫出,以實現優化。
- CustomHeight 指定自定義高度。
- ZeroHeight 指定是否隱藏行。
- ThickTop 指定默認情況下行是否具有粗上邊框。
- ThickBottom 指定默認情況下行是否具有粗下邊框。
原文鏈接:https://juejin.cn/post/7131983142763626503
相關推薦
- 2023-03-17 Pandas檢查dataFrame中的NaN實現_python
- 2022-08-05 Entity?Framework映射TPH、TPT、TPC與繼承類_C#教程
- 2022-11-17 Android?Compose?屬性動畫使用探索詳解_Android
- 2022-06-06 Python數據可視化Pyecharts制作Heatmap熱力圖_python
- 2023-07-18 SpringBoot Cache 整合 Redis 緩存框架
- 2022-04-22 appium報錯:Original error: socket hang up
- 2022-09-26 C++實現頁面的緩沖區管理器_C 語言
- 2022-12-12 flutter?Bloc?add兩次只響應一次問題解析_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支