gooffice

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2026 License: MIT Imports: 9 Imported by: 0

README

gooffice 项目说明

项目简介

gooffice 是一个功能强大的 Go 语言库,用于创建、读取和编辑 Office Open XML 文档(.docx, .xlsx 和 .pptx)。其目标是成为最兼容且性能最高的 Go 库,用于处理这些办公文档格式。

主要功能

  • 文档处理(.docx)

    • 读写编辑 Word 文档
    • 文本格式化
    • 表格操作
    • 图片插入
    • Word 转 PDF
  • 电子表格处理(.xlsx)

    • 读写编辑 Excel 表格
    • 单元格格式化(包括条件格式化)
    • 数据验证(下拉列表、规则等)
    • 公式计算(支持 100+ 函数)
    • 图表支持
    • 嵌入式图片
  • 演示文稿处理(.pptx)

    • 从模板创建
    • 文本框和形状
    • 图片插入

性能特点

gooffice 采用高性能设计,能够高效处理大型文档:

  • 创建 30,000 行 × 100 列的表格仅需约 4 秒
  • 保存操作非常快速
  • 读取操作相对较慢,但仍在可接受范围内

安装方法

go get github.com/golgys0621/gooffice

示例代码

创建 Word 文档
package main

import (
    "log"
    "github.com/golgys0621/gooffice/document"
)

func main() {
    // 创建新文档
    doc := document.New()
    defer doc.Close()
    
    // 添加标题
    para := doc.AddParagraph()
    run := para.AddRun()
    run.AddText("文档标题")
    run.Properties().SetBold(true)
    run.Properties().SetFontSize(24)
    
    // 添加段落
    para = doc.AddParagraph()
    run = para.AddRun()
    run.AddText("这是一个示例段落。")
    
    // 保存文档
    err := doc.SaveToFile("example.docx")
    if err != nil {
        log.Fatalf("无法保存文档: %v", err)
    }
}
创建 Excel 电子表格
package main

import (
    "log"
    "github.com/golgys0621/gooffice/spreadsheet"
)

func main() {
    // 创建新工作簿
    wb := spreadsheet.New()
    defer wb.Close()
    
    // 添加工作表
    sheet := wb.AddSheet("示例工作表")
    
    // 设置表头
    sheet.Cell("A1").SetString("产品")
    sheet.Cell("B1").SetString("销量")
    sheet.Cell("C1").SetString("单价")
    sheet.Cell("D1").SetString("销售额")
    
    // 添加数据
    sheet.Cell("A2").SetString("产品 A")
    sheet.Cell("B2").SetInt(100)
    sheet.Cell("C2").SetFloat(10.5)
    sheet.Cell("D2").SetFormula("B2*C2")
    
    // 保存工作簿
    err := wb.SaveToFile("example.xlsx")
    if err != nil {
        log.Fatalf("无法保存工作簿: %v", err)
    }
}

技术特点

  1. 纯 Go 实现:完全使用 Go 语言实现,无需外部依赖
  2. 高性能设计:优化的 XML 处理和内存管理
  3. 类型安全:强类型的 API 设计
  4. 模块化架构:清晰的模块划分和职责分离
  5. 标准合规:严格遵循 Office Open XML 规范

Documentation

Overview

Package unioffice provides creation, reading, and writing of ECMA 376 Office Open XML documents, spreadsheets and presentations. It is still early in development, but is progressing quickly. This library takes a slightly different approach from others, in that it starts by trying to support all of the ECMA-376 standard when marshaling/unmarshaling XML documents. From there it adds wrappers around the ECMA-376 derived types that provide a more convenient interface.

The raw XML based types reside in the `schema/` directory. These types are always accessible from the wrapper types via a `X() method that returns the raw type. Except for the base documents (document.Document, spreadsheet.Workbook and presentation.Presentation), the other wrapper types are value types with non-pointer methods. They exist solely to modify and return data from one or more XML types.

The packages of interest are github.com/golgys0621/gooffice/document, github.com/golgys0621/gooffice/spreadsheet and github.com/golgys0621/gooffice/presentation.

Index

Constants

View Source
const (
	OfficeDocumentTypeStrict     = "" /* 400-byte string literal not displayed */
	StylesTypeStrict             = "" /* 347-byte string literal not displayed */
	ThemeTypeStrict              = "" /* 341-byte string literal not displayed */
	ControlTypeStrict            = "" /* 328-byte string literal not displayed */
	SettingsTypeStrict           = "" /* 349-byte string literal not displayed */
	ImageTypeStrict              = "" /* 341-byte string literal not displayed */
	CommentsTypeStrict           = "" /* 349-byte string literal not displayed */
	ThumbnailTypeStrict          = "" /* 409-byte string literal not displayed */
	DrawingTypeStrict            = "" /* 328-byte string literal not displayed */
	ChartTypeStrict              = "" /* 341-byte string literal not displayed */
	ExtendedPropertiesTypeStrict = "" /* 409-byte string literal not displayed */
	CustomXMLTypeStrict          = "" /* 365-byte string literal not displayed */
	WorksheetTypeStrict          = "" /* 365-byte string literal not displayed */
	SharedStringsTypeStrict      = "" /* 384-byte string literal not displayed */
	TableTypeStrict              = "" /* 341-byte string literal not displayed */
	HeaderTypeStrict             = "" /* 347-byte string literal not displayed */
	FooterTypeStrict             = "" /* 347-byte string literal not displayed */
	NumberingTypeStrict          = "" /* 365-byte string literal not displayed */
	FontTableTypeStrict          = "" /* 365-byte string literal not displayed */
	WebSettingsTypeStrict        = "" /* 367-byte string literal not displayed */
	FootNotesTypeStrict          = "" /* 365-byte string literal not displayed */
	EndNotesTypeStrict           = "" /* 349-byte string literal not displayed */
	SlideTypeStrict              = "" /* 341-byte string literal not displayed */
	VMLDrawingTypeStrict         = "" /* 361-byte string literal not displayed */
	OfficeDocumentType           = "" /* 472-byte string literal not displayed */
	StylesType                   = "" /* 409-byte string literal not displayed */
	ThemeType                    = "" /* 403-byte string literal not displayed */
	ThemeContentType             = "" /* 295-byte string literal not displayed */
	SettingsType                 = "" /* 431-byte string literal not displayed */
	ImageType                    = "" /* 403-byte string literal not displayed */
	ControlType                  = "" /* 420-byte string literal not displayed */
	CommentsType                 = "" /* 431-byte string literal not displayed */
	CommentsContentType          = "" /* 352-byte string literal not displayed */
	ThumbnailType                = "" /* 439-byte string literal not displayed */
	DrawingType                  = "" /* 420-byte string literal not displayed */
	DrawingContentType           = "" /* 322-byte string literal not displayed */
	ChartType                    = "" /* 403-byte string literal not displayed */
	ChartContentType             = "" /* 365-byte string literal not displayed */
	HyperLinkType                = "" /* 417-byte string literal not displayed */
	ExtendedPropertiesType       = "" /* 477-byte string literal not displayed */
	CorePropertiesType           = "" /* 480-byte string literal not displayed */
	CorePropertiesAltType        = "" /* 507-byte string literal not displayed */
	CustomPropertiesType         = "" /* 480-byte string literal not displayed */
	CustomXMLType                = "" /* 417-byte string literal not displayed */
	TableStylesType              = "" /* 439-byte string literal not displayed */
	ViewPropertiesType           = "" /* 417-byte string literal not displayed */
	FontEmbeddingType            = "" /* 352-byte string literal not displayed */
	WorksheetType                = "" /* 417-byte string literal not displayed */
	WorksheetContentType         = "" /* 403-byte string literal not displayed */
	SharedStringsType            = "" /* 446-byte string literal not displayed */
	SharedStringsContentType     = "" /* 417-byte string literal not displayed */
	SMLStyleSheetContentType     = "" /* 400-byte string literal not displayed */
	TableType                    = "" /* 403-byte string literal not displayed */
	TableContentType             = "" /* 384-byte string literal not displayed */
	HeaderType                   = "" /* 409-byte string literal not displayed */
	FooterType                   = "" /* 409-byte string literal not displayed */
	NumberingType                = "" /* 417-byte string literal not displayed */
	FontTableType                = "" /* 417-byte string literal not displayed */
	WebSettingsType              = "" /* 439-byte string literal not displayed */
	FootNotesType                = "" /* 417-byte string literal not displayed */
	EndNotesType                 = "" /* 431-byte string literal not displayed */
	SlideType                    = "" /* 403-byte string literal not displayed */
	SlideContentType             = "" /* 400-byte string literal not displayed */
	SlideMasterType              = "" /* 439-byte string literal not displayed */
	SlideMasterContentType       = "" /* 431-byte string literal not displayed */
	SlideLayoutType              = "" /* 439-byte string literal not displayed */
	SlideLayoutContentType       = "" /* 431-byte string literal not displayed */
	PresentationPropertiesType   = "" /* 417-byte string literal not displayed */
	HandoutMasterType            = "" /* 446-byte string literal not displayed */
	NotesMasterType              = "" /* 439-byte string literal not displayed */
	VMLDrawingType               = "" /* 408-byte string literal not displayed */
	VMLDrawingContentType        = "" /* 306-byte string literal not displayed */
	CommentsExtendedType         = "" /* 391-byte string literal not displayed */
	CommentsExtensibleType       = "" /* 431-byte string literal not displayed */
	CommentsIdsType              = "" /* 384-byte string literal not displayed */
)
View Source
const (
	ContentTypesFilename = "\u005b\u0043\u006f\u006ete\u006e\u0074\u005f\u0054\u0079\u0070\u0065\u0073\u005d\u002e\u0078\u006d\u006c"
	BaseRelsFilename     = "_\u0072\u0065\u006c\u0073\u002f\u002e\u0072\u0065\u006c\u0073"
)
View Source
const MinGoVersion = _bc

Variables

View Source
var Log = _df.Printf

Functions

func AbsoluteFilename

func AbsoluteFilename(dt DocType, typ string, index int) string

AbsoluteFilename returns the full path to a file from the root of the zip container. Index is used in some cases for files which there may be more than one of (e.g. worksheets/drawings/charts)

func AbsoluteImageFilename

func AbsoluteImageFilename(dt DocType, index int, fileExtension string) string

AbsoluteImageFilename returns the full path to an image from the root of the zip container.

func AddPreserveSpaceAttr

func AddPreserveSpaceAttr(se *_g.StartElement, s string)

AddPreserveSpaceAttr adds an xml:space="preserve" attribute to a start element if it is required for the string s.

func Bool

func Bool(v bool) *bool

Bool returns a copy of v as a pointer.

func DisableLogging

func DisableLogging()

DisableLogging sets the Log function to a no-op so that any log messages are silently discarded.

func Float32

func Float32(v float32) *float32

Float32 returns a copy of v as a pointer.

func Float64

func Float64(v float64) *float64

Float64 returns a copy of v as a pointer.

func Int8

func Int8(v int8) *int8

Int8 returns a copy of v as a pointer.

func Int32

func Int32(v int32) *int32

Int32 returns a copy of v as a pointer.

func Int64

func Int64(v int64) *int64

Int64 returns a copy of v as a pointer.

func NeedsSpacePreserve

func NeedsSpacePreserve(s string) bool

NeedsSpacePreserve returns true if the string has leading or trailing space.

func RegisterConstructor

func RegisterConstructor(ns, name string, fn interface{})

RegisterConstructor registers a constructor function used for unmarshaling xsd:any elements.

func RelativeFilename

func RelativeFilename(dt DocType, relToTyp, typ string, index int) string

RelativeFilename returns a filename relative to the source file referenced from a relationships file. Index is used in some cases for files which there may be more than one of (e.g. worksheets/drawings/charts)

func RelativeImageFilename

func RelativeImageFilename(dt DocType, relToTyp, typ string, index int, fileExtension string) string

RelativeImageFilename returns an image filename relative to the source file referenced from a relationships file. It is identical to RelativeFilename but is used particularly for images in order to handle different image formats.

func String

func String(v string) *string

String returns a copy of v as a pointer.

func Stringf

func Stringf(f string, args ...interface{}) *string

Stringf formats according to a format specifier and returns a pointer to the resulting string.

func Uint8

func Uint8(v uint8) *uint8

Uint8 returns a copy of v as a pointer.

func Uint16

func Uint16(v uint16) *uint16

Uint16 returns a copy of v as a pointer.

func Uint32

func Uint32(v uint32) *uint32

Uint32 returns a copy of v as a pointer.

func Uint64

func Uint64(v uint64) *uint64

Uint64 returns a copy of v as a pointer.

Types

type Any

type Any interface {
	MarshalXML(_ef *_g.Encoder, _efd _g.StartElement) error
	UnmarshalXML(_fg *_g.Decoder, _a _g.StartElement) error
}

Any is the interface used for marshaling/unmarshaling xsd:any

func CreateElement

func CreateElement(start _g.StartElement) (Any, error)

CreateElement creates an element with the given namespace and name. It is used to unmarshal some xsd:any elements to the appropriate concrete type.

type DocType

type DocType byte

DocType represents one of the three document types supported (docx/xlsx/pptx)

const (
	Unknown DocType = iota
	DocTypeSpreadsheet
	DocTypeDocument
	DocTypePresentation
)

type XSDAny

type XSDAny struct {
	XMLName _g.Name
	Attrs   []_g.Attr
	Data    []byte
	Nodes   []*XSDAny
}

XSDAny is used to marshal/unmarshal xsd:any types in the OOXML schema.

func (*XSDAny) MarshalXML

func (_aeb *XSDAny) MarshalXML(e *_g.Encoder, start _g.StartElement) error

MarshalXML implements the xml.Marshaler interface.

func (*XSDAny) UnmarshalXML

func (_ffc *XSDAny) UnmarshalXML(d *_g.Decoder, start _g.StartElement) error

UnmarshalXML implements the xml.Unmarshaler interface.

Directories

Path Synopsis
Package common contains wrapper types and utilities common to all of the OOXML document formats.
Package common contains wrapper types and utilities common to all of the OOXML document formats.
tempstorage/memstore
Package memstore implements tempStorage interface by using memory as a storage
Package memstore implements tempStorage interface by using memory as a storage
Package document provides creation, reading, and writing of ECMA 376 Open Office XML documents.
Package document provides creation, reading, and writing of ECMA 376 Open Office XML documents.
internal
schema
format
Package format provides support for parsing and evaluating spreadsheetml/Excel number formats.
Package format provides support for parsing and evaluating spreadsheetml/Excel number formats.
formula
Package formula provides formula parsing and evaluation.
Package formula provides formula parsing and evaluation.
update
Package update contains definitions needed for updating references after removing rows/columns.
Package update contains definitions needed for updating references after removing rows/columns.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL