psmithuk / xlsx

Create Excel files in Go
MIT License
33 stars 8 forks source link

suspend unexpected! #9

Open JinAirsOs opened 8 years ago

JinAirsOs commented 8 years ago

When i use it to write large data excel,it did't convert all data to the excel! May be my souce data has many chinese ,but the go lang should be support utf-8,so i did't know what happens!

package main

import ( "bufio" "github.com/psmithuk/xlsx" "regexp" "os" "fmt" )

const PATH = "/home/chuixue/Desktop/"

func conv_excel(file string, row_cnt int) { fi, err := os.Open(PATH + file) if err != nil { panic(err) } defer fi.Close() input := bufio.NewScanner(fi) outputfile, err := os.Create(PATH + file + ".xlsx") w := bufio.NewWriter(outputfile) ww := xlsx.NewWorkbookWriter(w) c := []xlsx.Column{} for i := 0; i < row_cnt; i++ { c = append(c, xlsx.Column{Name: "Col" + string(i), Width: 10}) } sh := xlsx.NewSheetWithColumns(c) sh.Title = "MySheet" sw, err := ww.NewSheetWriter(&sh) //r := sh.NewRow() reg := regexp.MustCompile("\b") i,j := 0,0 var start,line string var s int for { r := sh.NewRow() for i = 0; i < row_cnt && input.Scan() ;i++ { start = fmt.Sprintf("%d %d ",j,i) s = len(start) line = input.Text() reg.ReplaceAllString(line,"") line = line[s:] //fmt.Println(line) //reg.ReplaceAllString(line,"") r.Cells[i] = xlsx.Cell{ Type: xlsx.CellTypeInlineString, Value: line, } }

    if i==0 {
        break;
    }
    j++
    err = sw.WriteRows([]xlsx.Row{r})
    if err != nil {
        fmt.Fprintf(os.Stdout,"row count:%q",err)
    }
}
fmt.Fprintf(os.Stdout,"row count:%d",j)
err = ww.Close()
if err != nil {
    fmt.Fprintf(os.Stdout,"row count:%q",err)
}
defer w.Flush()

}

func main() {

file := "tet"
row_cnt := 20
conv_excel(file, row_cnt)

}

/home/chuixue/Desktop/tet is like this 0 0 订单号 0 1 数量 0 2 单价 0 3 订单金额(元) 0 4 订单状态 0 5 付款状态 0 6 快递状态 0 7 成团时间 0 8 发货时间 0 9 快递费 0 10 折扣金额(元) 0 11 收货人 0 12 手机 0 13 省 0 14 市 0 15 区 0 16 收货地址 0 17 快递号 0 18 商品名称 0 19 规格 1 0 160624-35869229669 1 1 1 1 2 0 1 3 0 1 4 已成团 1 5 已付款 1 6 未发货 1 7 2016-06-25 05:40:07 1 8 1 9 0 1 10 0 1 11 徐娟娟 1 12 13735062670 1 13 浙江 1 14 衢州 1 15 江山市 1 16 江山市双塔街道办事处江滨路54号 1 17 1 18 【0元试用】迪奥真我女士香水30ml 持久留香 成熟优雅(限量20名) 1 19

small data works but this file is about 300M,it suspends at about 10000 rows,which should be 200000rows,i printf out the lines which the variable j,it shows that j=200000 but in the excel is 10000,so the sheetwriter did't write all data into the excel,I needs a reason!