iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > GO >Go Excelize API源码解析GetSheetFormatPr使用示例
  • 297
分享到

Go Excelize API源码解析GetSheetFormatPr使用示例

2024-04-02 19:04:59 297人浏览 安东尼
摘要

目录一、Go-excelize简介二、GetSheetFORMatPr一、Go-Excelize简介 Excelize 是 Go 语言编写的用于操作 Office Excel 文档基

一、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

根据给定的工作表名称获取格式属性。

可选格式参数数据类型
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)

输出结果:

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 指定默认情况下行是否具有粗下边框。

以上就是Go Excelize API源码解析GetSheetFormatPr使用示例的详细内容,更多关于Go源码解析GetSheetFormatPr的资料请关注编程网其它相关文章!

您可能感兴趣的文档:

--结束END--

本文标题: Go Excelize API源码解析GetSheetFormatPr使用示例

本文链接: https://www.lsjlt.com/news/121027.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作