forked from p30928647/excelize
API changed, use `NewFile()` instead of `CreateFile()` and use `SaveAs()` instead of `WriteTo()`.
This commit is contained in:
parent
555e2ba9a8
commit
66e5d1fa80
|
@ -36,7 +36,7 @@ import (
|
|||
)
|
||||
|
||||
func main() {
|
||||
xlsx := excelize.CreateFile()
|
||||
xlsx := excelize.NewFile()
|
||||
// Create a new sheet.
|
||||
xlsx.NewSheet(2, "Sheet2")
|
||||
// Set value of a cell.
|
||||
|
@ -45,7 +45,7 @@ func main() {
|
|||
// Set active sheet of the workbook.
|
||||
xlsx.SetActiveSheet(2)
|
||||
// Save xlsx file by the given path.
|
||||
err := xlsx.WriteTo("./Workbook.xlsx")
|
||||
err := xlsx.SaveAs("./Workbook.xlsx")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
|
@ -109,7 +109,7 @@ import (
|
|||
func main() {
|
||||
categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"}
|
||||
values := map[string]int{"B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
|
||||
xlsx := excelize.CreateFile()
|
||||
xlsx := excelize.NewFile()
|
||||
for k, v := range categories {
|
||||
xlsx.SetCellValue("Sheet1", k, v)
|
||||
}
|
||||
|
@ -118,7 +118,7 @@ func main() {
|
|||
}
|
||||
xlsx.AddChart("Sheet1", "E1", `{"type":"bar3D","series":[{"name":"=Sheet1!$A$2","categories":"=Sheet1!$B$1:$D$1","values":"=Sheet1!$B$2:$D$2"},{"name":"=Sheet1!$A$2","categories":"=Sheet1!$B$1:$D$1","values":"=Sheet1!$B$3:$D$3"},{"name":"=Sheet1!$A$3","categories":"=Sheet1!$B$1:$D$1","values":"=Sheet1!$B$4:$D$4"}],"title":{"name":"Fruit 3D Line Chart"}}`)
|
||||
// Save xlsx file by the given path.
|
||||
err := xlsx.WriteTo("./Workbook.xlsx")
|
||||
err := xlsx.SaveAs("./Workbook.xlsx")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
|
|
4
chart.go
4
chart.go
|
@ -71,7 +71,7 @@ func parseFormatChartSet(formatSet string) *formatChart {
|
|||
// func main() {
|
||||
// categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"}
|
||||
// values := map[string]int{"B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
|
||||
// xlsx := excelize.CreateFile()
|
||||
// xlsx := excelize.NewFile()
|
||||
// for k, v := range categories {
|
||||
// xlsx.SetCellValue("Sheet1", k, v)
|
||||
// }
|
||||
|
@ -80,7 +80,7 @@ func parseFormatChartSet(formatSet string) *formatChart {
|
|||
// }
|
||||
// xlsx.AddChart("SHEET1", "F2", `{"type":"bar3D","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"},{"name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"},{"name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit Line Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`)
|
||||
// // Save xlsx file by the given path.
|
||||
// err := xlsx.WriteTo("./Workbook.xlsx")
|
||||
// err := xlsx.SaveAs("./Workbook.xlsx")
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// os.Exit(1)
|
||||
|
|
2
col.go
2
col.go
|
@ -69,7 +69,7 @@ func (f *File) SetColVisible(sheet, column string, visible bool) {
|
|||
// SetColWidth provides function to set the width of a single column or multiple
|
||||
// columns. For example:
|
||||
//
|
||||
// xlsx := excelize.CreateFile()
|
||||
// xlsx := excelize.NewFile()
|
||||
// xlsx.SetColWidth("Sheet1", "A", "H", 20)
|
||||
// err := xlsx.Save()
|
||||
// if err != nil {
|
||||
|
|
|
@ -89,7 +89,7 @@ func TestOpenFile(t *testing.T) {
|
|||
t.Log(err)
|
||||
}
|
||||
// Test write file to not exist directory.
|
||||
err = xlsx.WriteTo("")
|
||||
err = xlsx.SaveAs("")
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
|
@ -121,7 +121,7 @@ func TestAddPicture(t *testing.T) {
|
|||
t.Log(err)
|
||||
}
|
||||
// Test write file to given path.
|
||||
err = xlsx.WriteTo("./test/Workbook_2.xlsx")
|
||||
err = xlsx.SaveAs("./test/Workbook_2.xlsx")
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
|
@ -135,7 +135,7 @@ func TestBrokenFile(t *testing.T) {
|
|||
t.Log(err)
|
||||
}
|
||||
// Test write file with broken file struct with given path.
|
||||
err = xlsx.WriteTo("./test/Workbook_3.xlsx")
|
||||
err = xlsx.SaveAs("./test/Workbook_3.xlsx")
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
|
@ -154,9 +154,9 @@ func TestBrokenFile(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestCreateFile(t *testing.T) {
|
||||
func TestNewFile(t *testing.T) {
|
||||
// Test create a XLSX file.
|
||||
xlsx := CreateFile()
|
||||
xlsx := NewFile()
|
||||
xlsx.NewSheet(2, "XLSXSheet2")
|
||||
xlsx.NewSheet(3, "XLSXSheet3")
|
||||
xlsx.SetCellInt("Sheet2", "A23", 56)
|
||||
|
@ -171,17 +171,17 @@ func TestCreateFile(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
err = xlsx.WriteTo("./test/Workbook_3.xlsx")
|
||||
err = xlsx.SaveAs("./test/Workbook_3.xlsx")
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetColWidth(t *testing.T) {
|
||||
xlsx := CreateFile()
|
||||
xlsx := NewFile()
|
||||
xlsx.SetColWidth("sheet1", "B", "A", 12)
|
||||
xlsx.SetColWidth("sheet1", "A", "B", 12)
|
||||
err := xlsx.WriteTo("./test/Workbook_4.xlsx")
|
||||
err := xlsx.SaveAs("./test/Workbook_4.xlsx")
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
|
@ -269,10 +269,10 @@ func TestMergeCell(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestSetRowHeight(t *testing.T) {
|
||||
xlsx := CreateFile()
|
||||
xlsx := NewFile()
|
||||
xlsx.SetRowHeight("Sheet1", 0, 50)
|
||||
xlsx.SetRowHeight("Sheet1", 3, 90)
|
||||
err := xlsx.WriteTo("./test/Workbook_5.xlsx")
|
||||
err := xlsx.SaveAs("./test/Workbook_5.xlsx")
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
|
@ -639,7 +639,7 @@ func TestAddChart(t *testing.T) {
|
|||
xlsx.AddChart("SHEET2", "P1", `{"type":"radar","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"},{"name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"},{"name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"top_right","show_legend_key":false},"title":{"name":"Fruit Radar Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"span"}`)
|
||||
xlsx.AddChart("SHEET2", "X1", `{"type":"scatter","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"},{"name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"},{"name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit Scatter Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`)
|
||||
// Save xlsx file by the given path.
|
||||
err = xlsx.WriteTo("./test/Workbook_6.xlsx")
|
||||
err = xlsx.SaveAs("./test/Workbook_6.xlsx")
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
|
|
12
file.go
12
file.go
|
@ -8,12 +8,12 @@ import (
|
|||
"os"
|
||||
)
|
||||
|
||||
// CreateFile provides function to create new file by default template. For
|
||||
// NewFile provides function to create new file by default template. For
|
||||
// example:
|
||||
//
|
||||
// xlsx := CreateFile()
|
||||
// xlsx := NewFile()
|
||||
//
|
||||
func CreateFile() *File {
|
||||
func NewFile() *File {
|
||||
file := make(map[string]string)
|
||||
file["_rels/.rels"] = XMLHeader + templateRels
|
||||
file["docProps/app.xml"] = XMLHeader + templateDocpropsApp
|
||||
|
@ -35,12 +35,12 @@ func (f *File) Save() error {
|
|||
if f.Path == "" {
|
||||
return fmt.Errorf("No path defined for file, consider File.WriteTo or File.Write")
|
||||
}
|
||||
return f.WriteTo(f.Path)
|
||||
return f.SaveAs(f.Path)
|
||||
}
|
||||
|
||||
// WriteTo provides function to create or update to an xlsx file at the provided
|
||||
// SaveAs provides function to create or update to an xlsx file at the provided
|
||||
// path.
|
||||
func (f *File) WriteTo(name string) error {
|
||||
func (f *File) SaveAs(name string) error {
|
||||
file, err := os.OpenFile(name, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -47,7 +47,7 @@ func parseFormatPictureSet(formatSet string) *formatPicture {
|
|||
// )
|
||||
//
|
||||
// func main() {
|
||||
// xlsx := excelize.CreateFile()
|
||||
// xlsx := excelize.NewFile()
|
||||
// // Insert a picture.
|
||||
// err := xlsx.AddPicture("Sheet1", "A2", "./image1.jpg", "")
|
||||
// if err != nil {
|
||||
|
@ -63,7 +63,7 @@ func parseFormatPictureSet(formatSet string) *formatPicture {
|
|||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
// err = xlsx.WriteTo("./Workbook.xlsx")
|
||||
// err = xlsx.SaveAs("./Workbook.xlsx")
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// os.Exit(1)
|
||||
|
|
2
rows.go
2
rows.go
|
@ -103,7 +103,7 @@ func (f *File) getTotalRowsCols(sheet string) (int, int) {
|
|||
// SetRowHeight provides a function to set the height of a single row.
|
||||
// For example:
|
||||
//
|
||||
// xlsx := excelize.CreateFile()
|
||||
// xlsx := excelize.NewFile()
|
||||
// xlsx.SetRowHeight("Sheet1", 0, 50)
|
||||
// err := xlsx.Save()
|
||||
// if err != nil {
|
||||
|
|
Loading…
Reference in New Issue