20
0
This commit is contained in:
Liu Ming 2017-02-21 16:27:01 +08:00
parent 1127e94c69
commit f02a04cbdd
6 changed files with 68 additions and 32 deletions

6
col.go
View File

@ -4,7 +4,6 @@ import (
"fmt"
"math"
"strconv"
"strings"
"time"
)
@ -50,13 +49,14 @@ func (xf *XfRk) String(wb *WorkBook) string {
if len(wb.Xfs) > idx {
fNo := wb.Xfs[idx].formatNo()
if fNo >= 164 { // user defined format
if fmt := wb.Formats[fNo]; fmt != nil && strings.Contains(fmt.str, "YY") {
if fmt := wb.Formats[fNo]; fmt != nil {
i, f, isFloat := xf.Rk.number()
if !isFloat {
f = float64(i)
}
t := timeFromExcelTime(f, wb.dateMode == 1)
return t.Format(time.RFC3339) //TODO it should be international
return t.Format(time.RFC3339) //TODO it should be international and format as the describled style
}
// see http://www.openoffice.org/sc/excelfileformat.pdf
} else if 14 <= fNo && fNo <= 17 || fNo == 22 || 27 <= fNo && fNo <= 36 || 50 <= fNo && fNo <= 58 { // jp. date format

View File

@ -24,13 +24,13 @@ func ExampleWorkBook_GetSheet() {
if xlFile, err := Open("Table.xls", "utf-8"); err == nil {
if sheet1 := xlFile.GetSheet(0); sheet1 != nil {
fmt.Print("Total Lines ", sheet1.MaxRow, sheet1.Name)
col1 := sheet1.Rows[0].Cols[0]
col2 := sheet1.Rows[0].Cols[0]
col1 := sheet1.Row(0).Col(0)
col2 := sheet1.Row(0).Col(0)
for i := 0; i <= (int(sheet1.MaxRow)); i++ {
row1 := sheet1.Rows[uint16(i)]
col1 = row1.Cols[0]
col2 = row1.Cols[1]
fmt.Print("\n", col1.String(xlFile), ",", col2.String(xlFile))
row1 := sheet1.Row(i)
col1 = row1.Col(0)
col2 = row1.Col(1)
fmt.Print("\n", col1, ",", col2)
}
}
}

32
row.go
View File

@ -10,7 +10,37 @@ type rowInfo struct {
Flags uint32
}
//Row the data of one row
type Row struct {
wb *WorkBook
info *rowInfo
Cols map[uint16]contentHandler
cols map[uint16]contentHandler
}
//Col Get the Nth Col from the Row, if has not, return nil.
//Suggest use Has function to test it.
func (r *Row) Col(i int) string {
serial := uint16(i)
if ch, ok := r.cols[serial]; ok {
strs := ch.String(r.wb)
return strs[0]
} else {
for _, v := range r.cols {
if v.FirstCol() <= serial && v.LastCol() >= serial {
strs := v.String(r.wb)
return strs[serial-v.FirstCol()]
}
}
}
return ""
}
//LastCol Get the number of Last Col of the Row.
func (r *Row) LastCol() int {
return int(r.info.Lcell)
}
//FirstCol Get the number of First Col of the Row.
func (r *Row) FirstCol() int {
return int(r.info.Fcell)
}

View File

@ -277,10 +277,10 @@ func (w *WorkBook) ReadAllCells(max int) (res [][]string) {
leng = max
}
temp := make([][]string, leng)
for k, row := range sheet.Rows {
for k, row := range sheet.rows {
data := make([]string, 0)
if len(row.Cols) > 0 {
for _, col := range row.Cols {
if len(row.cols) > 0 {
for _, col := range row.cols {
if uint16(len(data)) <= col.LastCol() {
data = append(data, make([]string, col.LastCol()-uint16(len(data))+1)...)
}

View File

@ -19,14 +19,20 @@ type WorkSheet struct {
bs *boundsheet
wb *WorkBook
Name string
Rows map[uint16]*Row
rows map[uint16]*Row
//NOTICE: this is the max row number of the sheet, so it should be count -1
MaxRow uint16
parsed bool
}
func (w *WorkSheet) Row(i int) *Row {
row := w.rows[uint16(i)]
row.wb = w.wb
return row
}
func (w *WorkSheet) parse(buf io.ReadSeeker) {
w.Rows = make(map[uint16]*Row)
w.rows = make(map[uint16]*Row)
b := new(bof)
var bof_pre *bof
for {
@ -181,12 +187,12 @@ func (w *WorkSheet) addRange(rang Ranger, ch contentHandler) {
func (w *WorkSheet) addContent(row_num uint16, ch contentHandler) {
var row *Row
var ok bool
if row, ok = w.Rows[row_num]; !ok {
if row, ok = w.rows[row_num]; !ok {
info := new(rowInfo)
info.Index = row_num
row = w.addRow(info)
}
row.Cols[ch.FirstCol()] = ch
row.cols[ch.FirstCol()] = ch
}
func (w *WorkSheet) addRow(info *rowInfo) (row *Row) {
@ -194,11 +200,11 @@ func (w *WorkSheet) addRow(info *rowInfo) (row *Row) {
w.MaxRow = info.Index
}
var ok bool
if row, ok = w.Rows[info.Index]; ok {
if row, ok = w.rows[info.Index]; ok {
row.info = info
} else {
row = &Row{info: info, Cols: make(map[uint16]contentHandler)}
w.Rows[info.Index] = row
row = &Row{info: info, cols: make(map[uint16]contentHandler)}
w.rows[info.Index] = row
}
return
}

View File

@ -7,20 +7,20 @@ import (
)
func TestOpen(t *testing.T) {
if xlFile, err := Open("Book1.xls", "utf-8"); err == nil {
if xlFile, err := Open("expenses.xls", "utf-8"); err == nil {
if sheet1 := xlFile.GetSheet(0); sheet1 != nil {
fmt.Println("Total Lines ", sheet1.MaxRow, sheet1.Name)
for i := 0; i < int(sheet1.MaxRow); i++ {
fmt.Printf("row %v point %v \n", i, sheet1.Rows[uint16(i)])
if sheet1.Rows[uint16(i)] == nil {
for i := 0; i <= int(sheet1.MaxRow); i++ {
fmt.Printf("row %v point %v \n", i, sheet1.Row(i))
if sheet1.Row(i) == nil {
continue
}
row := sheet1.Rows[uint16(i)]
for n, col := range row.Cols {
fmt.Println(n, "==>", col.String(xlFile), " ")
row := sheet1.Row(i)
for index := row.FirstCol(); index < row.LastCol(); index++ {
fmt.Println(index, "==>", row.Col(index), " ")
}
// col1 := .Cols[0]
// col2 := sheet1.Rows[uint16(i)].Cols[1]
// col2 := sheet1.Row(uint16(i)].Cols[1]
// fmt.Printf("\ncol1 %v \nCol2 %v \n", col1.String(xlFile), col2.String(xlFile))
}
}
@ -42,16 +42,16 @@ func TestEuropeString(t *testing.T) {
// for i := 0; i < xlFile.NumSheets(); i++ {
// fmt.Println(xlFile.GetSheet(i).Name)
// sheet := xlFile.GetSheet(i)
// row := sheet.Rows[1]
// row := sheet.Row(1]
// for i, col := range row.Cols {
// fmt.Println(i, col.String(xlFile))
// }
// }
// // sheet1 := xlFile.GetSheet(0)
// // fmt.Println(sheet1.Name)
// // fmt.Print(sheet1.Rows)
// // for k, row1 := range sheet1.Rows {
// // // row1 := sheet1.Rows[1]
// // fmt.Print(sheet1.Row()
// // for k, row1 := range sheet1.Row({
// // // row1 := sheet1.Row(1]
// // fmt.Printf("\n[%d]", k)
// // for _, col1 := range row1.Cols {
// // // col1 := row1.Cols[0]