提交 3c4ad28d 编写于 作者: xurime's avatar xurime

- Get cell value support

- Optimisation code use fmt package
- Update README
- Remove useless function
上级 0d60020f
......@@ -16,8 +16,6 @@ Excelize is a library written in pure Golang and providing a set of function tha
### Installation
Golang version requirements 1.6.0 or higher.
```
go get github.com/Luxurioust/excelize
```
......
package excelize
import (
"encoding/xml"
"strconv"
"strings"
)
// Get value from cell by given sheet index and axis in XLSX file
func GetCellValue(file []FileList, sheet string, axis string) string {
axis = strings.ToUpper(axis)
var xlsx xlsxWorksheet
row := getRowIndex(axis)
xAxis := row - 1
name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml`
xml.Unmarshal([]byte(readXml(file, name)), &xlsx)
rows := len(xlsx.SheetData.Row)
if rows <= xAxis {
return ``
}
for _, v := range xlsx.SheetData.Row[xAxis].C {
if xlsx.SheetData.Row[xAxis].R == row {
if axis == v.R {
switch v.T {
case "s":
shardStrings := xlsxSST{}
xlsxSI := 0
xlsxSI, _ = strconv.Atoi(v.V)
xml.Unmarshal([]byte(readXml(file, `xl/sharedStrings.xml`)), &shardStrings)
return shardStrings.SI[xlsxSI].T
case "str":
return v.V
default:
return v.V
}
}
}
}
return ``
}
......@@ -34,7 +34,7 @@ func SetCellInt(file []FileList, sheet string, axis string, value int) []FileLis
xAxis := row - 1
yAxis := titleToNumber(col)
name := fmt.Sprintf("xl/worksheets/%s.xml", strings.ToLower(sheet))
name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml`
xml.Unmarshal([]byte(readXml(file, name)), &xlsx)
rows := xAxis + 1
......@@ -65,7 +65,7 @@ func SetCellStr(file []FileList, sheet string, axis string, value string) []File
xAxis := row - 1
yAxis := titleToNumber(col)
name := fmt.Sprintf("xl/worksheets/%s.xml", strings.ToLower(sheet))
name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml`
xml.Unmarshal([]byte(readXml(file, name)), &xlsx)
rows := xAxis + 1
......
package excelize
import (
"fmt"
"strconv"
"testing"
)
......@@ -10,7 +9,7 @@ func TestExcelize(t *testing.T) {
// Test update a XLSX file
file, err := OpenFile("./test/Workbook1.xlsx")
if err != nil {
fmt.Println(err)
t.Error(err)
}
file = SetCellInt(file, "SHEET2", "B2", 100)
file = SetCellStr(file, "SHEET2", "C11", "Knowns")
......@@ -19,21 +18,31 @@ func TestExcelize(t *testing.T) {
file = SetCellStr(file, "SHEET3", "b230", "10")
file = SetActiveSheet(file, 2)
if err != nil {
fmt.Println(err)
t.Error(err)
}
for i := 1; i <= 300; i++ {
file = SetCellStr(file, "SHEET3", fmt.Sprintf("c%d", i), strconv.Itoa(i))
file = SetCellStr(file, "SHEET3", "c"+strconv.Itoa(i), strconv.Itoa(i))
}
err = Save(file, "./test/Workbook_2.xlsx")
// Test create a XLSX file
file2 := CreateFile()
file2 = NewSheet(file2, 2, "SHEETxxx")
file2 = NewSheet(file2, 3, "asd")
file2 = NewSheet(file2, 2, "TestSheet2")
file2 = NewSheet(file2, 3, "TestSheet3")
file2 = SetCellInt(file2, "Sheet2", "A23", 10)
file2 = SetCellStr(file2, "SHEET1", "B20", "10")
err = Save(file2, "./test/Workbook_3.xlsx")
if err != nil {
fmt.Println(err)
t.Error(err)
}
// Test read cell value
file, err = OpenFile("./test/Workbook1.xlsx")
if err != nil {
t.Error(err)
}
GetCellValue(file, "Sheet2", "a5")
GetCellValue(file, "Sheet2", "D11")
GetCellValue(file, "Sheet2", "D12")
GetCellValue(file, "Sheet2", "E12")
}
......@@ -29,7 +29,7 @@ func setContentTypes(file []FileList, index int) []FileList {
var content xlsxTypes
xml.Unmarshal([]byte(readXml(file, `[Content_Types].xml`)), &content)
content.Overrides = append(content.Overrides, xlsxOverride{
PartName: fmt.Sprintf("/xl/worksheets/sheet%d.xml", index),
PartName: `/xl/worksheets/sheet` + strconv.Itoa(index) + `.xml`,
ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
})
output, err := xml.MarshalIndent(content, "", "")
......@@ -50,7 +50,7 @@ func setSheet(file []FileList, index int) []FileList {
if err != nil {
fmt.Println(err)
}
path := fmt.Sprintf("xl/worksheets/sheet%d.xml", index)
path := `xl/worksheets/sheet` + strconv.Itoa(index) + `.xml`
return saveFileList(file, path, replaceRelationshipsID(replaceWorkSheetsRelationshipsNameSpace(string(output))))
}
......@@ -86,7 +86,7 @@ func addXlsxWorkbookRels(file []FileList, sheet int) []FileList {
rId := len(content.Relationships) + 1
content.Relationships = append(content.Relationships, xlsxWorkbookRelation{
Id: "rId" + strconv.Itoa(rId),
Target: fmt.Sprintf("worksheets/sheet%d.xml", sheet),
Target: `worksheets/sheet` + strconv.Itoa(sheet) + `.xml`,
Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet",
})
output, err := xml.MarshalIndent(content, "", "")
......@@ -150,7 +150,7 @@ func SetActiveSheet(file []FileList, index int) []FileList {
for i := 0; i < sheets; i++ {
xlsx := xlsxWorksheet{}
sheetIndex := i + 1
path := fmt.Sprintf("xl/worksheets/sheet%d.xml", sheetIndex)
path := `xl/worksheets/sheet` + strconv.Itoa(sheetIndex) + `.xml`
xml.Unmarshal([]byte(readXml(file, path)), &xlsx)
if index == sheetIndex {
if len(xlsx.SheetViews.SheetView) > 0 {
......
......@@ -19,31 +19,3 @@ type xlsxDefault struct {
Extension string `xml:",attr"`
ContentType string `xml:",attr"`
}
func MakeDefaultContentTypes() (types xlsxTypes) {
types.Overrides = make([]xlsxOverride, 8)
types.Defaults = make([]xlsxDefault, 2)
types.Overrides[0].PartName = "/_rels/.rels"
types.Overrides[0].ContentType = "application/vnd.openxmlformats-package.relationships+xml"
types.Overrides[1].PartName = "/docProps/app.xml"
types.Overrides[1].ContentType = "application/vnd.openxmlformats-officedocument.extended-properties+xml"
types.Overrides[2].PartName = "/docProps/core.xml"
types.Overrides[2].ContentType = "application/vnd.openxmlformats-package.core-properties+xml"
types.Overrides[3].PartName = "/xl/_rels/workbook.xml.rels"
types.Overrides[3].ContentType = "application/vnd.openxmlformats-package.relationships+xml"
types.Overrides[4].PartName = "/xl/sharedStrings.xml"
types.Overrides[4].ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"
types.Overrides[5].PartName = "/xl/styles.xml"
types.Overrides[5].ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"
types.Overrides[6].PartName = "/xl/workbook.xml"
types.Overrides[6].ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"
types.Overrides[7].PartName = "/xl/theme/theme1.xml"
types.Overrides[7].ContentType = "application/vnd.openxmlformats-officedocument.theme+xml"
types.Defaults[0].Extension = "rels"
types.Defaults[0].ContentType = "application/vnd.openxmlformats-package.relationships+xml"
types.Defaults[1].Extension = "xml"
types.Defaults[1].ContentType = "application/xml"
return
}
package excelize
import (
"encoding/xml"
)
// xlsxSST directly maps the sst element from the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main currently
// I have not checked this for completeness - it does as much as I need.
type xlsxSST struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main sst"`
Count int `xml:"count,attr"`
UniqueCount int `xml:"uniqueCount,attr"`
SI []xlsxSI `xml:"si"`
}
// xlsxSI directly maps the si element from the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
// currently I have not checked this for completeness - it does as
// much as I need.
type xlsxSI struct {
T string `xml:"t"`
R []xlsxR `xml:"r"`
}
// xlsxR directly maps the r element from the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
// currently I have not checked this for completeness - it does as
// much as I need.
type xlsxR struct {
T string `xml:"t"`
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册