2016-10-31 19:13:22 +08:00
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2016-11-02 12:58:51 +08:00
|
|
|
"strconv"
|
2016-10-31 19:13:22 +08:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2016-11-02 12:58:51 +08:00
|
|
|
// GetRows return all the rows in a sheet, for example:
|
|
|
|
//
|
|
|
|
// rows := xlsx.GetRows("Sheet2")
|
|
|
|
// for _, row := range rows {
|
|
|
|
// for _, colCell := range row {
|
|
|
|
// fmt.Print(colCell, "\t")
|
|
|
|
// }
|
|
|
|
// fmt.Println()
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
func (f *File) GetRows(sheet string) [][]string {
|
|
|
|
xlsx := xlsxWorksheet{}
|
|
|
|
r := [][]string{}
|
2017-01-18 14:47:23 +08:00
|
|
|
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
|
2016-10-31 19:13:22 +08:00
|
|
|
err := xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
|
2016-11-02 12:58:51 +08:00
|
|
|
if err != nil {
|
|
|
|
return r
|
2016-10-31 19:13:22 +08:00
|
|
|
}
|
|
|
|
rows := xlsx.SheetData.Row
|
2016-11-02 12:58:51 +08:00
|
|
|
for _, row := range rows {
|
|
|
|
c := []string{}
|
|
|
|
for _, colCell := range row.C {
|
|
|
|
val, _ := colCell.getValueFrom(f)
|
|
|
|
c = append(c, val)
|
|
|
|
}
|
|
|
|
r = append(r, c)
|
|
|
|
}
|
|
|
|
return r
|
2016-10-31 19:13:22 +08:00
|
|
|
}
|
|
|
|
|
2016-11-02 12:58:51 +08:00
|
|
|
// readXMLSST read xmlSST simple function.
|
2016-10-31 19:13:22 +08:00
|
|
|
func readXMLSST(f *File) (xlsxSST, error) {
|
|
|
|
shardStrings := xlsxSST{}
|
2017-01-18 14:47:23 +08:00
|
|
|
err := xml.Unmarshal([]byte(f.readXML("xl/sharedStrings.xml")), &shardStrings)
|
2016-10-31 19:13:22 +08:00
|
|
|
return shardStrings, err
|
|
|
|
}
|
|
|
|
|
2017-01-18 16:05:01 +08:00
|
|
|
// getValueFrom return a value from a column/row cell, this function is inteded
|
|
|
|
// to be used with for range on rows an argument with the xlsx opened file.
|
2016-11-02 12:58:51 +08:00
|
|
|
func (xlsx *xlsxC) getValueFrom(f *File) (string, error) {
|
|
|
|
switch xlsx.T {
|
|
|
|
case "s":
|
|
|
|
xlsxSI := 0
|
|
|
|
xlsxSI, _ = strconv.Atoi(xlsx.V)
|
|
|
|
d, err := readXMLSST(f)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return d.SI[xlsxSI].T, nil
|
|
|
|
case "str":
|
|
|
|
return xlsx.V, nil
|
|
|
|
default:
|
|
|
|
return xlsx.V, nil
|
|
|
|
}
|
2016-10-31 19:13:22 +08:00
|
|
|
}
|