Skip to content

Instantly share code, notes, and snippets.

@mlh758
Created April 12, 2019 20:29
Show Gist options
  • Save mlh758/e981bb29e5a87e42d97f5547e922f275 to your computer and use it in GitHub Desktop.
Save mlh758/e981bb29e5a87e42d97f5547e922f275 to your computer and use it in GitHub Desktop.
Excelize Interface
package excel
import (
"fmt"
"io"
"github.com/360EntSecGroup-Skylar/excelize"
)
type File interface {
AddPicture(string, string, string, string) error
DeleteSheet(string)
GetCellStyle(string, string) int
GetCellValue(string, string) string
GetRowOutlineLevel(string, int) uint8
GetCellHyperLink(string, string) (bool, string)
GetRows(string) [][]string
GetSheetIndex(string) int
MergeCell(string, string, string)
NewSheet(string) int
NewStyle(string) (int, error)
RemoveRow(string, int)
Rows(string) (*excelize.Rows, error)
SetActiveSheet(int)
SetCellBool(string, string, bool)
SetCellDefault(string, string, string)
SetCellHyperLink(string, string, string, string)
SetCellInt(string, string, int)
SetCellStr(string, string, string)
SetCellStyle(string, string, string, int)
SetCellValue(string, string, interface{})
SetColWidth(string, string, string, float64)
SetPanes(string, string)
SetRowHeight(string, int, float64)
SetRowOutlineLevel(string, int, uint8)
SetSheetName(string, string)
SetSheetPrOptions(string, ...excelize.SheetPrOption) error
Write(io.Writer) error
}
type excelWriter struct {
excelize.File
}
// ToAlphaString converts a column number (0 indexed) to an excel
// column string such as AB
func ToAlphaString(col int) string {
val, _ := excelize.ColumnNumberToName(col + 1)
return val
}
// TitleToNumber converts a column string (AB) to an integer
// such as 28
func TitleToNumber(title string) int {
val, _ := excelize.ColumnNameToNumber(title)
return val
}
func NewFile() File {
writer := new(excelWriter)
writer.File = *excelize.NewFile()
return writer
}
// GetCellValue provides a function to get formatted value from cell by given worksheet name and axis in XLSX file.
// If it is possible to apply a format to the cell value, it will do so, if not then the raw value of the cell will
// be returned
func (e *excelWriter) GetCellValue(sheet string, axis string) string {
val, _ := e.File.GetCellValue(sheet, axis)
return val
}
// GetCellStyle returns the cell style index for the given cell
func (e *excelWriter) GetCellStyle(sheet, axis string) int {
val, _ := e.File.GetCellStyle(sheet, axis)
return val
}
// GetRows returns the list of rows their lists of columns for the given sheet
// [rows][columns]
func (e *excelWriter) GetRows(sheet string) [][]string {
reply, _ := e.File.GetRows(sheet)
return reply
}
// GetCellHyperLink returns whether the cell has a hyperlink and the target of that links
func (e *excelWriter) GetCellHyperLink(sheet, axis string) (bool, string) {
linked, target, _ := e.File.GetCellHyperLink(sheet, axis)
return linked, target
}
// GetRowOutlineLevel returns the outline level of a row (0 based index)
func (e *excelWriter) GetRowOutlineLevel(sheet string, row int) uint8 {
val, _ := e.File.GetRowOutlineLevel(sheet, row+1)
return val
}
// MergeCell provides a function to merge cells by given coordinate area and sheet name. For example create a merged cell of D3:E9 on Sheet1:
//
// xlsx.MergeCell("Sheet1", "D3", "E9")
//
// If you create a merged cell that overlaps with another existing merged cell, those merged cells that already exist will be removed.
func (e *excelWriter) MergeCell(sheet, hcell, vcell string) {
mergedCells := e.GetMergeCells(sheet)
for _, mc := range mergedCells {
if mc[0] == fmt.Sprint(hcell, ":", vcell) {
return
}
}
e.File.MergeCell(sheet, hcell, vcell)
}
// Removes the given row (1 based index) from the sheet
func (e *excelWriter) RemoveRow(sheet string, row int) {
e.File.RemoveRow(sheet, row)
}
// SetCellBool sets a bool value into the sheet
func (e *excelWriter) SetCellBool(sheet, axis string, val bool) {
e.File.SetCellBool(sheet, axis, val)
}
// SetCellInt sets an integer value into the sheet
func (e *excelWriter) SetCellInt(sheet, axis string, val int) {
e.File.SetCellInt(sheet, axis, val)
}
// SetCellDefault sets an untyped value into the sheet
// Excel will determine the type on its own
func (e *excelWriter) SetCellDefault(sheet, axis, val string) {
e.File.SetCellDefault(sheet, axis, val)
}
// SetCellStr sets a string into the sheet
func (e *excelWriter) SetCellStr(sheet, axis, val string) {
e.File.SetCellStr(sheet, axis, val)
}
// SetCellValue sets an arbitrary value into the sheet. The library will
// reflect on the value and determine the appropriate type to add
func (e *excelWriter) SetCellValue(sheet, axis string, val interface{}) {
e.File.SetCellValue(sheet, axis, val)
}
// SetCellStyle sets a style between hcell at the top left and vcell at the bottom
// right, forming a rectangle between those coordiantes. styleID is a value from
// NewStyle
func (e *excelWriter) SetCellStyle(sheet, hcell, vcell string, styleID int) {
e.File.SetCellStyle(sheet, hcell, vcell, styleID)
}
// SetColWidth sets all the columns from start to end to the given width
func (e *excelWriter) SetColWidth(sheet, startCol, endCol string, width float64) {
e.File.SetColWidth(sheet, startCol, endCol, width)
}
func (e *excelWriter) SetRowHeight(sheet string, row int, height float64) {
e.File.SetRowHeight(sheet, row, height)
}
// SetCellHyperlink sets an unstyled link into the cell.
// Valid link types are "External" and "Location"
func (e *excelWriter) SetCellHyperLink(sheet, axis, link, linkType string) {
e.File.SetCellHyperLink(sheet, axis, link, linkType)
}
// SetRowOutlineLevel sets the outline level of the given row (0 indexed)
func (e *excelWriter) SetRowOutlineLevel(sheet string, row int, level uint8) {
e.File.SetRowOutlineLevel(sheet, row+1, level)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment