excl

package module
v0.0.0-...-c6a9e4c Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2017 License: MIT Imports: 20 Imported by: 3

README

excl

これはexcelコントロール用のライブラリ

godoc CircleCI go report

Description

基本的にもとのexcelファイルを破壊せずにデータの入力を行うためのライブラリです。 また大量のデータを扱う上でも優位になるように開発を行います。

Usage

既存のExcelファイルを操作

// Excelファイルを読み込み
w, _ := excl.Open("path/to/read.xlsx")
// シートを開く
s, _ := w.OpenSheet("Sheet1")
// 一行目を取得
r := s.GetRow(1)
// 1列目のセルを取得
c := r.GetCell(1)
// セルに10を出力
c.SetNumber("10")
// セルに1を出力
s.GetRow(2).GetCell(1).SetNumber(1)
// セルに1.1を出力
s.GetRow(3).GetCell(1).SetNumber(1.1)
// 2列目のセルにABCDEという文字列を出力
c = r.SetString("ABCDE", 2)
// セルに日付を出力
s.GetRow(4).GetCell(1).SetDate(time.Now())
// セルに数式を出力
s.GetRow(5).GetCell(1).SetFormula("SUM(A2:A3)")
// シートを閉じる
s.Close()
// 保存
w.Save("path/to/new.xlsx")

新規Excelファイルを作成

// 新規Excelファイルを作成
w, _ := excl.Create()
s, _ := w.OpenSheet("Sheet1")
s.Close()
w.Save("path/to/new.xlsx")

セルの書式の設定方法

w, _ := excl.Open("path/to/read.xlsx")
s, _ := w.OpenSheet("Sheet1")
r := s.GetRow(1)
c := r.GetCell(1)
c.SetNumber("10000.00")
// 数値のフォーマットを設定する
c.SetNumFmt("#,##0.0")
// フォントの設定
c.SetFont(excl.Font{Size: 12, Color: "FF00FFFF", Bold: true, Italic: false,Underline: false})
// 背景色の設定
c.SetBackgroundColor("FFFF00FF")
// 罫線の設定
c.SetBorder(excl.Border{
	Left:   &excl.BorderSetting{Style: "thin", Color: "FFFFFF00"},
	Right:  &excl.BorderSetting{Style: "hair"},
	Top:    &excl.BorderSetting{Style: "dashDotDot"},
	Bottom: nil,
})
s.Close()
w.Save("path/to/new.xlsx")

グリッド線の表示非表示

w, _ := excl.Open("path/to/read.xlsx")
s, _ := w.OpenSheet("Sheet1")
// シートのグリッド線を表示
s.ShowGridlines(true)
// シートのグリッド線を非表示
s.ShowGridlines(false)
s.Close()
w.Save("path/to/new.xlsx")

カラム幅の変更

w, _ := excl.Open("path/to/read.xlsx")
s, _ := w.OpenSheet("Sheet1")
// 5番目のカラム幅を1.1に変更
s.SetColWidth(1.1, 5)
s.Close()
w.Save("path/to/new.xlsx")

計算式結果の更新が必要な場合はSetForceFormulaRecalculationを使用する この関数を利用することでExcelを開いた際に結果が自動的に更新される

w, _ := excl.Open("path/to/read.xlsx")
// 何か処理...
w.SetForceFormulaRecalculation(true)
w.Save("path/to/new.xlsx")

シート名変更

w, _ := excl.Open("path/to/read.xlsx")
w.RenameSheet("oldname", "newname")
w.Save("path/to/new.xlsx")

シートの表示非表示切り替え

w, _ := excl.Open("path/to/read.xlsx")
// シートを隠す
w.HideSheet("Sheet1")
// シートを表示する
w.ShowSheet("Sheet1")
w.Save("path/to/new.xlsx")

Install

$ go get github.com/loadoff/excl

Licence

MIT

Author

YuIwasaki

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColNumPosition

func ColNumPosition(col string) int

ColNumPosition obtain column no from AtoZ column string

func ColStringPosition

func ColStringPosition(num int) string

ColStringPosition obtain AtoZ column string from column no

func IsExistString

func IsExistString(strs []string, str string) bool

IsExistString 配列内に文字列が存在するかを確認する

func NewExcl

func NewExcl()

NewExcl は

Types

type Border

type Border struct {
	Left   *BorderSetting
	Right  *BorderSetting
	Top    *BorderSetting
	Bottom *BorderSetting
}

Border 罫線の設定

type BorderSetting

type BorderSetting struct {
	Style string
	Color string
}

BorderSetting 罫線の設定

type Cell

type Cell struct {
	R string
	// contains filtered or unexported fields
}

Cell はセル一つ一つに対する構造体

func NewCell

func NewCell(tag *Tag, sharedStrings *SharedStrings, styles *Styles) *Cell

NewCell は新しくcellを作成する

func (*Cell) GetStyle

func (cell *Cell) GetStyle() *Style

GetStyle Style構造体を取得する

func (*Cell) MarshalXML

func (cell *Cell) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML create xml for cell

func (*Cell) SetBackgroundColor

func (cell *Cell) SetBackgroundColor(color string) *Cell

SetBackgroundColor 背景色をセットする

func (*Cell) SetBorder

func (cell *Cell) SetBorder(border Border) *Cell

SetBorder 罫線情報をセットする

func (*Cell) SetDate

func (cell *Cell) SetDate(val time.Time) *Cell

SetDate set a date in a cell

func (*Cell) SetFont

func (cell *Cell) SetFont(font Font) *Cell

SetFont フォント情報をセットする

func (*Cell) SetFormula

func (cell *Cell) SetFormula(val string) *Cell

SetFormula set a formula in a cell

func (*Cell) SetNumFmt

func (cell *Cell) SetNumFmt(fmt string) *Cell

SetNumFmt 数値フォーマット

func (*Cell) SetNumber

func (cell *Cell) SetNumber(val interface{}) *Cell

SetNumber set a number in a cell

func (*Cell) SetString

func (cell *Cell) SetString(val string) *Cell

SetString 文字列を追加する

func (*Cell) SetStyle

func (cell *Cell) SetStyle(style *Style) *Cell

SetStyle 数値フォーマットIDをセット

type ContentTypes

type ContentTypes struct {
	// contains filtered or unexported fields
}

ContentTypes ContentTypesの情報を保持

func OpenContentTypes

func OpenContentTypes(dir string) (*ContentTypes, error)

OpenContentTypes [Content_Types].xmlファイルを開き構造体に読み込む

func (*ContentTypes) Close

func (types *ContentTypes) Close() error

Close [Content_Types].xmlファイルを閉じる

type ContentTypesXML

type ContentTypesXML struct {
	XMLName   xml.Name          `xml:"Types"`
	Xmlns     string            `xml:"xmlns,attr"`
	Defaults  []contentDefault  `xml:"Default"`
	Overrides []contentOverride `xml:"Override"`
}

ContentTypesXML [Content_Types].xmlファイルを読み込む

type Font

type Font struct {
	Size      int
	Color     string
	Name      string
	Bold      bool
	Italic    bool
	Underline bool
}

Font フォントの設定

type Relationships

type Relationships struct {
	XMLName xml.Name       `xml:"Relationships"`
	Xmlns   string         `xml:"xmlns,attr"`
	Rels    []relationship `xml:"Relationship"`
}

Relationships Relationshipsタグの情報

type Row

type Row struct {
	// contains filtered or unexported fields
}

Row 行の構造体

func NewRow

func NewRow(tag *Tag, sharedStrings *SharedStrings, styles *Styles) *Row

NewRow は新しく行を追加する際に使用する

func (*Row) CreateCells

func (row *Row) CreateCells(from int, to int) []*Cell

CreateCells セル一覧を用意する

func (*Row) GetCell

func (row *Row) GetCell(colNo int) *Cell

GetCell セル番号のセルを取得する

func (*Row) MarshalXML

func (row *Row) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML Create xml tags

func (*Row) SetDate

func (row *Row) SetDate(val time.Time, colNo int) *Cell

SetDate set a date at a row

func (*Row) SetFormula

func (row *Row) SetFormula(val string, colNo int) *Cell

SetFormula set a formula at a row

func (*Row) SetHeight

func (row *Row) SetHeight(height float64)

SetHeight set row height

func (*Row) SetNumber

func (row *Row) SetNumber(val interface{}, colNo int) *Cell

SetNumber set number at a row

func (*Row) SetString

func (row *Row) SetString(val string, colNo int) *Cell

SetString set string at a row

type SharedStrings

type SharedStrings struct {
	// contains filtered or unexported fields
}

SharedStrings 構造体

func OpenSharedStrings

func OpenSharedStrings(dir string) (*SharedStrings, error)

OpenSharedStrings 新しいSharedString構造体を作成する

func (*SharedStrings) AddString

func (ss *SharedStrings) AddString(text string) int

AddString 文字列データを追加する 戻り値はインデックス情報(0スタート)

func (*SharedStrings) Close

func (ss *SharedStrings) Close() error

Close sharedStrings情報をクローズする

type Sheet

type Sheet struct {
	Rows   []*Row
	Styles *Styles
	// contains filtered or unexported fields
}

Sheet struct for control sheet data

func (*Sheet) Close

func (sheet *Sheet) Close() error

Close close sheet

func (*Sheet) Create

func (sheet *Sheet) Create(dir string) error

Create create new sheet

func (*Sheet) CreateRows

func (sheet *Sheet) CreateRows(from int, to int) []*Row

CreateRows create multiple rows

func (*Sheet) GetRow

func (sheet *Sheet) GetRow(rowNo int) *Row

GetRow get row(from 1)

func (*Sheet) Open

func (sheet *Sheet) Open(dir string) error

Open open sheet.xml in directory

func (*Sheet) OutputAll

func (sheet *Sheet) OutputAll()

OutputAll output all rows

func (*Sheet) OutputThroughRowNo

func (sheet *Sheet) OutputThroughRowNo(rowNo int)

OutputThroughRowNo output through to rowno

func (*Sheet) SetColWidth

func (sheet *Sheet) SetColWidth(width float64, colNo int)

SetColWidth set column width

func (*Sheet) ShowGridlines

func (sheet *Sheet) ShowGridlines(show bool)

ShowGridlines switch show/hide grid lines

type SheetXML

type SheetXML struct {
	XMLName xml.Name `xml:"sheet"`
	Name    string   `xml:"name,attr"`
	SheetID string   `xml:"sheetId,attr"`
	RID     string   `xml:"id,attr"`
}

SheetXML sheet.xml information

type Style

type Style struct {
	NumFmtID int
	FontID   int
	FillID   int
	BorderID int
	XfID     int

	Horizontal string
	Vertical   string
	Wrap       int
	// contains filtered or unexported fields
}

Style セルの書式情報

type Styles

type Styles struct {
	// contains filtered or unexported fields
}

Styles スタイルの情報を持った構造体

func OpenStyles

func OpenStyles(dir string) (*Styles, error)

OpenStyles styles.xmlファイルを開く

func (*Styles) Close

func (styles *Styles) Close() error

Close styles.xmlファイルを閉じる

func (*Styles) GetStyle

func (styles *Styles) GetStyle(index int) *Style

GetStyle Style構造体を取得する

func (*Styles) MarshalXML

func (styles *Styles) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML stylesからXMLを作り直す

func (*Styles) SetBackgroundColor

func (styles *Styles) SetBackgroundColor(color string) int

SetBackgroundColor 背景色を追加する

func (*Styles) SetBorder

func (styles *Styles) SetBorder(border Border) int

SetBorder 罫線を設定する

func (*Styles) SetCellXfs

func (styles *Styles) SetCellXfs(style *Style) int

SetCellXfs cellXfsにタグを追加する

func (*Styles) SetFont

func (styles *Styles) SetFont(font Font) int

SetFont フォント情報を追加する

func (*Styles) SetNumFmt

func (styles *Styles) SetNumFmt(format string) int

SetNumFmt 数値フォーマットをセットする

func (*Styles) SetStyle

func (styles *Styles) SetStyle(style *Style) int

SetStyle セルの書式を設定

type Tag

type Tag struct {
	Name      xml.Name
	Attr      []xml.Attr
	Children  []interface{}
	XmlnsList []xml.Attr
}

Tag タグの情報をすべて保管する

func (*Tag) MarshalXML

func (t *Tag) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML タグからXMLを作成しなおす

func (*Tag) UnmarshalXML

func (t *Tag) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML タグにXMLを読み込む

type Workbook

type Workbook struct {
	TempPath string

	SharedStrings *SharedStrings

	Styles *Styles
	// contains filtered or unexported fields
}

Workbook はワークブック内の情報を格納する

func Create

func Create() (*Workbook, error)

Create 新しくワークブックを作成する

func Open

func Open(path string) (*Workbook, error)

Open Excelファイルを開く

func (*Workbook) Close

func (workbook *Workbook) Close() error

Close 操作中のブックを閉じる(保存はしない)

func (*Workbook) HideSheet

func (workbook *Workbook) HideSheet(name string)

HideSheet hide sheet

func (*Workbook) OpenSheet

func (workbook *Workbook) OpenSheet(name string) (*Sheet, error)

OpenSheet Open specified sheet if there is no specified sheet then create new sheet

func (*Workbook) RenameSheet

func (workbook *Workbook) RenameSheet(old string, new string)

RenameSheet rename sheet name from old name to new name.

func (*Workbook) Save

func (workbook *Workbook) Save(path string) error

Save save and close a workbook

func (*Workbook) SetForceFormulaRecalculation

func (workbook *Workbook) SetForceFormulaRecalculation(flg bool)

SetForceFormulaRecalculation set fullCalcOnLoad attribute to calcPr tag. When this excel file is opened, all calculation fomula will be recalculated.

func (*Workbook) ShowSheet

func (workbook *Workbook) ShowSheet(name string)

ShowSheet show sheet

type WorkbookRels

type WorkbookRels struct {
	// contains filtered or unexported fields
}

WorkbookRels workbook.xml.relの情報をもつ構造体

func OpenWorkbookRels

func OpenWorkbookRels(dir string) (*WorkbookRels, error)

OpenWorkbookRels open workbook.xml.rels

func (*WorkbookRels) Close

func (wbr *WorkbookRels) Close() error

Close close workbook.xml.rels

type WorkbookXML

type WorkbookXML struct {
	XMLName xml.Name  `xml:"workbook"`
	Sheets  sheetsXML `xml:"sheets"`
}

WorkbookXML workbook.xmlに記載されている<workbook>タグの中身

Jump to

Keyboard shortcuts

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