提交 0a0a36ba 编写于 作者: xurime's avatar xurime

Update testing file more case added and reading cell value usage example in readme added.

上级 2d0eda1a
...@@ -69,10 +69,30 @@ func main() { ...@@ -69,10 +69,30 @@ func main() {
xlsx = excelize.SetCellInt(xlsx, "Sheet3", "A23", 10) xlsx = excelize.SetCellInt(xlsx, "Sheet3", "A23", 10)
xlsx = excelize.SetCellStr(xlsx, "Sheet3", "b230", "World") xlsx = excelize.SetCellStr(xlsx, "Sheet3", "b230", "World")
xlsx = excelize.SetActiveSheet(xlsx, 2) xlsx = excelize.SetActiveSheet(xlsx, 2)
err = excelize.Save(xlsx, "~/Workbook.xlsx")
if err != nil {
fmt.Println(err)
}
}
```
### Reading XLSX files
```
package main
import (
"fmt"
"github.com/Luxurioust/excelize"
)
func main() {
xlsx, err := excelize.OpenFile("~/Workbook.xlsx")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
err = excelize.Save(xlsx, "~/Workbook.xlsx") cell := excelize.GetCellValue(file, "Sheet2", "D11")
fmt.Println(cell)
} }
``` ```
......
...@@ -9,7 +9,7 @@ func TestExcelize(t *testing.T) { ...@@ -9,7 +9,7 @@ func TestExcelize(t *testing.T) {
// Test update a XLSX file // Test update a XLSX file
file, err := OpenFile("./test/Workbook1.xlsx") file, err := OpenFile("./test/Workbook1.xlsx")
if err != nil { if err != nil {
t.Error(err) t.Log(err)
} }
file = SetCellInt(file, "SHEET2", "B2", 100) file = SetCellInt(file, "SHEET2", "B2", 100)
file = SetCellStr(file, "SHEET2", "C11", "Knowns") file = SetCellStr(file, "SHEET2", "C11", "Knowns")
...@@ -18,12 +18,21 @@ func TestExcelize(t *testing.T) { ...@@ -18,12 +18,21 @@ func TestExcelize(t *testing.T) {
file = SetCellStr(file, "SHEET3", "b230", "10") file = SetCellStr(file, "SHEET3", "b230", "10")
file = SetActiveSheet(file, 2) file = SetActiveSheet(file, 2)
if err != nil { if err != nil {
t.Error(err) t.Log(err)
} }
for i := 1; i <= 300; i++ { for i := 1; i <= 300; i++ {
file = SetCellStr(file, "SHEET3", "c"+strconv.Itoa(i), strconv.Itoa(i)) file = SetCellStr(file, "SHEET3", "c"+strconv.Itoa(i), strconv.Itoa(i))
} }
err = Save(file, "./test/Workbook_2.xlsx") err = Save(file, "./test/Workbook_2.xlsx")
if err != nil {
t.Log(err)
}
// Test save to not exist directory
err = Save(file, "")
if err != nil {
t.Log(err)
}
// Test create a XLSX file // Test create a XLSX file
file2 := CreateFile() file2 := CreateFile()
...@@ -31,18 +40,20 @@ func TestExcelize(t *testing.T) { ...@@ -31,18 +40,20 @@ func TestExcelize(t *testing.T) {
file2 = NewSheet(file2, 3, "TestSheet3") file2 = NewSheet(file2, 3, "TestSheet3")
file2 = SetCellInt(file2, "Sheet2", "A23", 10) file2 = SetCellInt(file2, "Sheet2", "A23", 10)
file2 = SetCellStr(file2, "SHEET1", "B20", "10") file2 = SetCellStr(file2, "SHEET1", "B20", "10")
file2 = SetActiveSheet(file2, 0)
err = Save(file2, "./test/Workbook_3.xlsx") err = Save(file2, "./test/Workbook_3.xlsx")
if err != nil { if err != nil {
t.Error(err) t.Log(err)
} }
// Test read cell value // Test read cell value
file, err = OpenFile("./test/Workbook1.xlsx") file, err = OpenFile("./test/Workbook1.xlsx")
if err != nil { if err != nil {
t.Error(err) t.Log(err)
} }
GetCellValue(file, "Sheet2", "a-1") GetCellValue(file, "Sheet2", "a-1")
GetCellValue(file, "Sheet2", "a5") GetCellValue(file, "Sheet2", "a5")
GetCellValue(file, "Sheet2", "C11")
GetCellValue(file, "Sheet2", "D11") GetCellValue(file, "Sheet2", "D11")
GetCellValue(file, "Sheet2", "D12") GetCellValue(file, "Sheet2", "D12")
GetCellValue(file, "Sheet2", "E12") GetCellValue(file, "Sheet2", "E12")
......
...@@ -3,7 +3,6 @@ package excelize ...@@ -3,7 +3,6 @@ package excelize
import ( import (
"archive/zip" "archive/zip"
"bytes" "bytes"
"fmt"
"os" "os"
) )
...@@ -34,7 +33,7 @@ func Save(files []FileList, name string) error { ...@@ -34,7 +33,7 @@ func Save(files []FileList, name string) error {
for _, file := range files { for _, file := range files {
f, err := w.Create(file.Key) f, err := w.Create(file.Key)
if err != nil { if err != nil {
fmt.Println(err) return err
} }
_, err = f.Write([]byte(file.Value)) _, err = f.Write([]byte(file.Value))
if err != nil { if err != nil {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册