溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Go?Excelize?API中GetSheetFormatPr如何使用

發(fā)布時(shí)間:2022-08-17 10:18:04 來源:億速云 閱讀:147 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了Go Excelize API中GetSheetFormatPr如何使用的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Go Excelize API中GetSheetFormatPr如何使用文章都會(huì)有所收獲,下面我們一起來看看吧。

一、Go-Excelize簡(jiǎn)介

Excelize 是 Go 語言編寫的用于操作 Office Excel 文檔基礎(chǔ)庫,基于 ECMA-376,ISO/IEC 29500 國(guó)際標(biāo)準(zhǔn)。

可以使用它來讀取、寫入由 Microsoft Excel™ 2007 及以上版本創(chuàng)建的電子表格文檔。

支持 XLAM / XLSM / XLSX / XLTM / XLTX 等多種文檔格式,高度兼容帶有樣式、圖片(表)、透視表、切片器等復(fù)雜組件的文檔,并提供流式讀寫 API,用于處理包含大規(guī)模數(shù)據(jù)的工作簿。

可應(yīng)用于各類報(bào)表平臺(tái)、云計(jì)算、邊緣計(jì)算等系統(tǒng)。

使用本類庫要求使用的 Go 語言為 1.15 或更高版本。

二、GetSheetFormatPr

func (f *File) GetSheetFormatPr(sheet string, opts ...SheetFormatPrOptionsPtr) error

根據(jù)給定的工作表名稱獲取格式屬性。

可選格式參數(shù)數(shù)據(jù)類型
BaseColWidthuint8
DefaultColWidthfloat64
DefaultRowHeightfloat64
CustomHeightbool
ZeroHeightbool
ThickTopbool
ThickBottombool

使用示例:

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)

輸出結(jié)果:

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
}

代碼很簡(jiǎn)單,先讀取工作表,然后獲取工作表的格式屬性,然后遍歷不定長(zhǎng)參數(shù)opts,對(duì)fp的每個(gè)opt進(jìn)行讀取。

SheetFormatPrOptionsPtr是一個(gè)interface。

type SheetFormatPrOptionsPtr interface {
	SheetFormatPrOptions
	getSheetFormatPr(formatPr *xlsxSheetFormatPr)
}

該interface 內(nèi)有兩個(gè)函數(shù)。

Go?Excelize?API中GetSheetFormatPr如何使用

Go?Excelize?API中GetSheetFormatPr如何使用

Go?Excelize?API中GetSheetFormatPr如何使用

Go?Excelize?API中GetSheetFormatPr如何使用

Go?Excelize?API中GetSheetFormatPr如何使用

我們可以發(fā)現(xiàn),他們都大同小異,第一步的if語句是判斷格式屬性是否存在,如果不存在就賦一個(gè)默認(rèn)值。 然后取格式屬性指針fp的格式屬性,前面是類型轉(zhuǎn)換:

Go?Excelize?API中GetSheetFormatPr如何使用

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"`
}

下面介紹一下各個(gè)參數(shù)的作用:

  • BaseColWidth:指定普通樣式字體的最大數(shù)字寬度的字符數(shù)。此值不包括邊距填充或網(wǎng)格線的額外填充。它只是字符數(shù)。

  • DefaultColWidth 指定默認(rèn)列寬,其度量值為普通樣式字體的最大數(shù)字寬度的字符數(shù)。

  • DefaultRowHeight 指定以磅值度量的默認(rèn)行高,我們不必在所有行上寫入高度。如果大多數(shù)行具有自定義高度,則可以將其寫出,以實(shí)現(xiàn)優(yōu)化。

  • CustomHeight 指定自定義高度。

  • ZeroHeight 指定是否隱藏行。

  • ThickTop 指定默認(rèn)情況下行是否具有粗上邊框。

  • ThickBottom 指定默認(rèn)情況下行是否具有粗下邊框。

關(guān)于“Go Excelize API中GetSheetFormatPr如何使用”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Go Excelize API中GetSheetFormatPr如何使用”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI