rows.go 1.4 KB
Newer Older
A
ahmad 已提交
1 2 3 4
package excelize

import (
	"encoding/xml"
5
	"strconv"
A
ahmad 已提交
6 7 8
	"strings"
)

9 10 11 12 13 14 15 16 17 18 19 20 21
// 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{}
22
	name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
A
ahmad 已提交
23
	err := xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
24 25
	if err != nil {
		return r
A
ahmad 已提交
26 27
	}
	rows := xlsx.SheetData.Row
28 29 30 31 32 33 34 35 36
	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
A
ahmad 已提交
37 38
}

39
// readXMLSST read xmlSST simple function.
A
ahmad 已提交
40 41
func readXMLSST(f *File) (xlsxSST, error) {
	shardStrings := xlsxSST{}
42
	err := xml.Unmarshal([]byte(f.readXML("xl/sharedStrings.xml")), &shardStrings)
A
ahmad 已提交
43 44 45
	return shardStrings, err
}

46
// getValueFrom return a value from a column/row cell,
A
ahmad 已提交
47
// this function is inteded to be used with for range on rows
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
// an argument with the xlsx opened file.
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
	}
A
ahmad 已提交
64
}