導出二維數據excel,其實很簡單,使用cvs就能夠了。可是若是導出格式複雜帶樣式還帶圖片的怎麼辦?客戶的要求有時就是這麼變態。呵呵。若是使用.net,微軟提供的有庫,使用php好像也有現成的有庫。我大體對這些庫進行了解,均可以實現咱們的需求。惋惜我如今使用的golang沒有那麼庫支持,因此只好裸搞了。php
Excel格式分爲兩種,第一種是封閉式,不知道使用啥格式,好比office 2003使用的格式,擴展名爲*.xsl。另外一種是開放式的,使用的是Open XML技術,好比office 2007 之後的版本,好在如今已經2014了,7年過去,大部分人都已經用到office 2012,即便國產的wps也早已經完美支持Open XML 了。 因此不用考慮兼容問題了。golang
此次個人解決方案就是從Open XML 入手。經過對Open XML學習,解決思路大體可分爲3步驟:sql
第一步:使用支持Open XML 的軟件,好比 office 2010製做一個咱們想要的Excel,保存擴展名名爲xlsx。app
第二步:把xlsx修改擴展名爲 zip,解壓後使用佔位符,修改裏面相應的XML文件,而後壓縮,再把擴展名修改成xlsx。這個壓縮文件咱們可稱爲導出模板。學習
第三步:使用程序動態解壓,替換佔位符,再壓縮。其中對圖片的處理,是把圖片保存到相應文件夾以及相應XML,千言萬語,不如代碼更加直接,實現代碼以下:spa
func (c Order) Excel(pageIndex int, pageSize int, sortField string, sortOrder string, customId int64, state string, orderTime string) revel.Result { sql := "select a.name A,b.name B,e.name C,d.name D,c.name E,a.order_time F,a.money G,a.state H,a.image I,a.width J,a.height K,a.area L,a.unit M,a.amount N,a.price O,f.alias P,a.remarks Q from ad_order a,ad_custom b,ad_product c,ad_stuff d,ad_stuff_cat e,ad_user f where a.product_id=c.id and c.stuff_id=d.id and d.cat_id=e.id and a.custom_id=b.id and a.user_id=f.id and a.del_state='未刪' %s %s %s order by a.id desc" sql = fmt.Sprintf(sql, fmt.Sprintf("and a.custom_id=%d", customId), "%s", "%s") if state != "" { sql = fmt.Sprintf(sql, fmt.Sprintf("and a.state='%s'", state), "%s") } else { sql = fmt.Sprintf(sql, "", "%s") } if orderTime != "" { sql = fmt.Sprintf(sql, fmt.Sprintf("and a.order_time='%s'", orderTime)) } else { sql = fmt.Sprintf(sql, "") } orders, err := Orm.Query(sql) if err != nil { return c.RenderJson(models.Message{State: "failure", Msg: err.Error()}) } rows := make([]Rows, 0) col := 17 for i := 1; i <= len(orders); i++ { row := Rows{} row.RowId = i + 1 for k := 0; k < col; k++ { row.Columns = append(row.Columns, Columns{R: fmt.Sprintf("%s%d", string(ABC[k]), i+1), V: i*col + k}) } rows = append(rows, row) } vmlDrawings := make([]VmlDrawing, 0) sharedStrings := make([]string, 0) sharedStrings = append(sharedStrings, "訂單名稱") sharedStrings = append(sharedStrings, "客戶名稱") sharedStrings = append(sharedStrings, "產品目錄") sharedStrings = append(sharedStrings, "產品材料") sharedStrings = append(sharedStrings, "產品名稱") sharedStrings = append(sharedStrings, "訂單日期") sharedStrings = append(sharedStrings, "金額") sharedStrings = append(sharedStrings, "收費狀態") sharedStrings = append(sharedStrings, "產品圖片") sharedStrings = append(sharedStrings, "寬度(米)") sharedStrings = append(sharedStrings, "高度(米)") sharedStrings = append(sharedStrings, "面積") sharedStrings = append(sharedStrings, "單位") sharedStrings = append(sharedStrings, "數量") sharedStrings = append(sharedStrings, "單價") sharedStrings = append(sharedStrings, "經手人") sharedStrings = append(sharedStrings, "備註") for i, row := range orders { if string(row["I"]) != "" { img := string(row["I"]) vmlDrawing := VmlDrawing{} vmlDrawing.Index = i vmlDrawing.Id = img[:strings.Index(img, ".")] vmlDrawing.Name = fmt.Sprintf("S%s", img) vmlDrawing.RowBegin = i + 1 vmlDrawing.RowEnd = i + 2 vmlDrawings = append(vmlDrawings, vmlDrawing) } sharedStrings = append(sharedStrings, string(row["A"])) //訂單名稱 sharedStrings = append(sharedStrings, string(row["B"])) //客戶名稱 sharedStrings = append(sharedStrings, string(row["C"])) //產品目錄 sharedStrings = append(sharedStrings, string(row["D"])) //產品材料 sharedStrings = append(sharedStrings, string(row["E"])) //產品名稱 sharedStrings = append(sharedStrings, string(row["F"])) //訂單日期 f1, _ := strconv.ParseFloat(string(row["G"]), 32) sharedStrings = append(sharedStrings, fmt.Sprintf("%.2f", f1)) //金額 sharedStrings = append(sharedStrings, string(row["H"])) //收費狀態 sharedStrings = append(sharedStrings, "") //產品圖片 f2, _ := strconv.ParseFloat(string(row["J"]), 32) sharedStrings = append(sharedStrings, fmt.Sprintf("%.2f", f2)) //寬度(米) f3, _ := strconv.ParseFloat(string(row["K"]), 32) sharedStrings = append(sharedStrings, fmt.Sprintf("%.2f", f3)) //高度(米) f4, _ := strconv.ParseFloat(string(row["L"]), 32) sharedStrings = append(sharedStrings, fmt.Sprintf("%.4f", f4)) //面積 sharedStrings = append(sharedStrings, string(row["M"])) //單位 sharedStrings = append(sharedStrings, string(row["N"])) //數量 f5, _ := strconv.ParseFloat(string(row["O"]), 32) sharedStrings = append(sharedStrings, fmt.Sprintf("%.2f", f5)) //單價 sharedStrings = append(sharedStrings, string(row["P"])) //經手人 sharedStrings = append(sharedStrings, string(row["Q"])) //備註 } basePath := revel.BasePath basePathPrefix := fpath.Join(basePath, fpath.FromSlash("app/templates")) file, _ := os.Create(fpath.Join(basePathPrefix, fpath.FromSlash(fmt.Sprintf("%s.xlsx", "orders")))) w := zip.NewWriter(file) defer w.Close() r, _ := zip.OpenReader(fpath.Join(basePathPrefix, fpath.FromSlash(fmt.Sprintf("%s.xlsx", "order")))) defer r.Close() for _, f := range r.File { switch f.Name { case "xl/worksheets/sheet1.xml": buf := new(bytes.Buffer) rc, _ := f.Open() data, _ := ioutil.ReadAll(rc) rc.Close() tmpl, _ := template.New("sheet").Parse(string(data)) tmpl.Execute(buf, rows) ff, _ := w.Create(f.Name) ff.Write(buf.Bytes()) break case "xl/sharedStrings.xml": buf := new(bytes.Buffer) rc, _ := f.Open() data, _ := ioutil.ReadAll(rc) rc.Close() tmpl, _ := template.New("sharedStrings").Parse(string(data)) tmpl.Execute(buf, sharedStrings) ff, _ := w.Create(f.Name) ff.Write(buf.Bytes()) break case "xl/drawings/_rels/vmlDrawing1.vml.rels": buf := new(bytes.Buffer) rc, _ := f.Open() data, _ := ioutil.ReadAll(rc) rc.Close() tmpl, _ := template.New("vmlDrawing1.vml").Parse(string(data)) tmpl.Execute(buf, vmlDrawings) ff, _ := w.Create(f.Name) ff.Write(buf.Bytes()) break case "xl/drawings/vmlDrawing1.vml": buf := new(bytes.Buffer) rc, _ := f.Open() data, _ := ioutil.ReadAll(rc) rc.Close() tmpl, _ := template.New("vmlDrawing1").Parse(string(data)) tmpl.Execute(buf, vmlDrawings) ff, _ := w.Create(f.Name) ff.Write(buf.Bytes()) basePath := revel.BasePath basePathPrefix := fpath.Join(basePath, fpath.FromSlash("public/upload")) for _, v := range vmlDrawings { fsmall := fpath.Join(basePathPrefix, fpath.FromSlash(v.Name)) file, _ := os.Open(fsmall) data, _ := ioutil.ReadAll(file) ff, _ := w.Create(fmt.Sprintf("xl/media/%s", v.Name)) ff.Write(data) } break default: ff, _ := w.Create(f.Name) rc, _ := f.Open() data, _ := ioutil.ReadAll(rc) ff.Write(data) rc.Close() } } return c.RenderFile(file, revel.Attachment) }
最後吐槽一下,博客園插入code,既然沒有golang。.net