README
¶
** Notice** Special thanks to the original author for the open source library: https://github.com/carmel/gooxml. As the original repository is not maintained , can not repair the original repository by submitting the code in the way of problems , so I used the original repository of the code , for secondary development / repair some of the possible bugs .
特别感谢原作者提供的开源库:https://github.com/carmel/gooxml。 由于原始仓库不在维护,无法通过提交代码的方式修复原始仓库中存在的问题,因此我使用了原始仓库的代码,进行二次开发/修复一些可能存在的bug.
**gooxml** is a library for creation of Office Open XML documents (.docx, .xlsx and .pptx). It's goal is to be the most compatible and highest performance Go library for creation and editing of docx/xlsx/pptx files.
Requires go1.8+, builds are tested with 1.8, 1.9 and tip.
Status
- Documents (docx) [Word]
- Read/Write/Edit
- Formatting
- Images
- Tables
- Spreadsheets (xlsx) [Excel]
- Read/Write/Edit
- Cell formatting including conditional formatting
- Cell validation (drop down combobox, rules, etc.)
- Retrieve cell values as formatted by Excel (e.g. retrieve a date or number as displayed in Excel)
- Formula Evaluation (100+ functions supported currently, more will be added as required)
- Embedded Images
- All chart types
- PowerPoint (pptx) [PowerPoint]
- Creation from templates
- Textboxes/shapes
Performance
There has been a great deal of interest in performance numbers for spreadsheet creation/reading lately, so here are gooxml numbers for this benchmark which creates a sheet with 30k rows, each with 100 columns.
creating 30000 rows * 100 cells took 3.92506863s
saving took 89ns
reading took 9.522383048s
Creation is fairly fast, saving is very quick due to no reflection usage, and reading is a bit slower. The downside is that the binary is large (33MB) as it contains generated structs, serialization and deserialization code for all of DOCX/XLSX/PPTX.
Installation
// Before installing this package, please check the latest tags version number, e.g. v1.0.6-alpha
go get github.com/Snowmanazz/gooxml@v1.0.6-alpha
Document Examples
- Simple Text Formatting Text font colors, sizes, highlighting, etc.
- Auto Generated Table of Contents Creating document headings with an auto generated TOC based off of the headingds
- Floating Image Placing an image somewhere on a page, absolutely positioned with different text wrapping.
- Header & Footer Creating headers and footers including page numbering.
- Multiple Headers & Footers Using different headers and footers depending on document section.
- Inline Tables Adding an table with and without borders.
- Using Existing Word Document as a Template Opening a document as a template to re-use the styles created in the document.
- Filling out Form Fields Opening a document with embedded form fields, filling out the fields and saving the result as a new filled form.
- Editing an existing document Open an existing document and replace/remove text without modifying formatting.
Spreadsheet Examples
- Simple A simple sheet with a few cells
- Named Cells Different ways of referencing rows and cells
- Cell Number/Date/Time Formats Creating cells with various number/date/time formats
- Line Chart/Line Chart 3D Line Charts
- Bar Chart Bar Charts
- Mutiple Charts Multiple charts on a single sheet
- Named Cell Ranges Naming cell ranges
- Merged Cells Merge and unmerge cells
- Conditional Formatting Conditionally formatting cells, styling, gradients, icons, data bar
- Complex Multiple charts, auto filtering and conditional formatting
- Borders Individual cell borders and rectangular borders around a range of cells.
- Validation Data validation including combo box dropdowns.
- Frozen Rows/Cols A sheet with a frozen header column and row
Presentation Examples
- Simple Text Boxes Simple text boxes and shapes
- Images Simple image insertion
- Template Creating a presentation from a template
Raw Types
The OOXML specification is large and creating a friendly API to cover the entire specification is a very time consuming endeavor. This library attempts to provide an easy to use API for common use cases in creating OOXML documents while allowing users to fall back to raw document manipulation should the library's API not cover a specific use case.
The raw XML based types reside in the schema/
directory. These types are
accessible from the wrapper types via a X()
method that returns the raw
type.
For example, the library currently doesn't have an API for setting a document
background color. However it's easy to do manually via editing the
CT_Background
element of the document.
dox := document.New()
doc.X().Background = wordprocessingml.NewCT_Background()
doc.X().Background.ColorAttr = &wordprocessingml.ST_HexColor{}
doc.X().Background.ColorAttr.ST_HexColorRGB = color.RGB(50, 50, 50).AsRGBString()
Contribution guidelines
All contributors are must sign a contributor license agreement before their code will be reviewed and merged.
Licensing
This library is offered under a dual license. It is freely available for use under the terms of AGPLv3. If you would like to use this library for a closed source project, please contact sales@unidoc.io.
There are no differences in functionality between the open source and commercial versions. You are encouraged to use the open source version to evaluate the library before purchasing a commercial license.
Documentation
¶
Overview ¶
Package gooxml 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/Snowmanazz/gooxml/document, baliance/gooxml/spreadsheet and github.com/Snowmanazz/gooxml/presentation.
Index ¶
- Constants
- Variables
- func AbsoluteFilename(dt DocType, typ string, index int) string
- func AddPreserveSpaceAttr(se *xml.StartElement, s string)
- func Bool(v bool) *bool
- func DisableLogging()
- func Float32(v float32) *float32
- func Float64(v float64) *float64
- func InstallLicense(s string) error
- func Int32(v int32) *int32
- func Int64(v int64) *int64
- func Int8(v int8) *int8
- func NeedsSpacePreserve(s string) bool
- func RegisterConstructor(ns, name string, fn interface{})
- func RelativeFilename(dt DocType, relToTyp, typ string, index int) string
- func String(v string) *string
- func Stringf(f string, args ...interface{}) *string
- func Uint16(v uint16) *uint16
- func Uint32(v uint32) *uint32
- func Uint64(v uint64) *uint64
- func Uint8(v uint8) *uint8
- type Any
- type DocType
- type License
- type LicenseType
- type XSDAny
Examples ¶
Constants ¶
const ( ContentTypesFilename = "[Content_Types].xml" BaseRelsFilename = "_rels/.rels" )
Common filenames used in zip packages.
const ( // Common OfficeDocumentType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" StylesType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" ThemeType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" ThemeContentType = "application/vnd.openxmlformats-officedocument.theme+xml" SettingsType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" ImageType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" CommentsType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" CommentsContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml" ThumbnailType = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail" DrawingType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing" DrawingContentType = "application/vnd.openxmlformats-officedocument.drawing+xml" ChartType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart" ChartContentType = "application/vnd.openxmlformats-officedocument.drawingml.chart+xml" HyperLinkType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" ExtendedPropertiesType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" CorePropertiesType = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" CustomPropertiesType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties" // purl.oclc.org 站点提供的ooxml 属性 OfficeDocumentTypeOclc = "http://purl.oclc.org/ooxml/officeDocument/relationships/officeDocument" ExtendedPropertiesTypeOclc = "http://purl.oclc.org/ooxml/officeDocument/relationships/extendedProperties" StylesTypeOclc = "http://purl.oclc.org/ooxml/officeDocument/relationships/styles" SettingsTypeOclc = "http://purl.oclc.org/ooxml/officeDocument/relationships/settings" ThemeTypeOclc = "http://purl.oclc.org/ooxml/officeDocument/relationships/theme" // SML WorksheetType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" WorksheetContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" SMLStyleSheetContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml" TableType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/table" TableContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml" ViewPropertiesType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/viewProps" TableStylesType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/tableStyles" // WML HeaderType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header" NumberingType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering" FontTableType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" FontTableTypeOclc = "http://purl.oclc.org/ooxml/officeDocument/relationships/fontTable" WebSettingsType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" FootNotesType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes" EndNotesType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes" // purl.oclc.org 站点提供的ooxml 属性 WebSettingsTypeOclc = "http://purl.oclc.org/ooxml/officeDocument/relationships/webSettings" FootNotesTypeOclc = "http://purl.oclc.org/ooxml/officeDocument/relationships/footnotes" EndNotesTypeOclc = "http://purl.oclc.org/ooxml/officeDocument/relationships/endnotes" // PML SlideType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" SlideContentType = "application/vnd.openxmlformats-officedocument.presentationml.slide+xml" SlideMasterType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster" SlideMasterContentType = "application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml" SlideLayoutType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" SlideLayoutContentType = "application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml" PresentationPropertiesType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/presProps" // VML VMLDrawingType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing" VMLDrawingContentType = "application/vnd.openxmlformats-officedocument.vmlDrawing" // CustomProperties CustomProperties = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties" CustomXml = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml" )
Consts for content types used throughout the package
const MinGoVersion = requires_go_18
MinGoVersion is used to cause a compile time error if gooxml is compiled with an older version of go. Specifically it requires a feature in go 1.8 regarding collecting all attributes from arbitrary xml used in decode gooxml.XSDAny.
const OpenSourceLicense = `` /* 271-byte string literal not displayed */
OpenSourceLicense is the AGPL open source license. It is installed by default if no license has previously been installed.
Variables ¶
var Log = log.Printf
Log is used to log content from within the library. The intent is to use logging sparingly, preferring to return an error. At the very least this allows redirecting logs to somewhere more appropriate than stdout.
var ReleaseDate = time.Date(2018, 9, 14, 0, 0, 0, 0, time.UTC)
ReleaseDate is the release date of the source code for licensing purposes.
var ReleaseVersion = "v0.8000"
Release is the last release version of the software.
Functions ¶
func AbsoluteFilename ¶
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 AddPreserveSpaceAttr ¶
func AddPreserveSpaceAttr(se *xml.StartElement, s string)
AddPreserveSpaceAttr adds an xml:space="preserve" attribute to a start element if it is required for the string s.
func DisableLogging ¶
func DisableLogging()
DisableLogging sets the Log function to a no-op so that any log messages are silently discarded.
func InstallLicense ¶
InstallLicense installs a license, returning an error if the license is invalid or expired. Expiration checks the ReleaseDate variable in version.go.
func NeedsSpacePreserve ¶
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 ¶
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)
Types ¶
type Any ¶
type Any interface { MarshalXML(e *xml.Encoder, start xml.StartElement) error UnmarshalXML(d *xml.Decoder, start xml.StartElement) error }
Any is the interface used for marshaling/unmarshaling xsd:any
func CreateElement ¶
func CreateElement(start xml.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)
type License ¶
type License struct { Name string Signature string `json:",omitempty"` Expiration time.Time LicenseType LicenseType }
License holds the gooxml license information.
func GetLicense ¶
func GetLicense() License
GetLicense returns the current license. This can be used by commercial customers to assist in ensuring that their license hasn't expired.
func (License) Encoded ¶
Encoded returns a base64 encoded version of the license for use with InstallLicense.
type LicenseType ¶
type LicenseType byte
LicenseType is the type of license
const ( LicenseTypeInvalid LicenseType = iota LicenseTypeAGPL LicenseTypeCommercial )
LicenseType constants
func (LicenseType) String ¶
func (i LicenseType) String() string
type XSDAny ¶
XSDAny is used to marshal/unmarshal xsd:any types in the OOXML schema.
func (*XSDAny) MarshalXML ¶
MarshalXML implements the xml.Marshaler interface.
func (*XSDAny) UnmarshalXML ¶
UnmarshalXML implements the xml.Unmarshaler interface.
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
_examples
|
|
cmd
|
|
Package color provides color handling structures and functions for use across all of the document types.
|
Package color provides color handling structures and functions for use across all of the document types. |
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. |
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. |
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. |