tablib_xlsx.go 1014 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package tablib
  2. import (
  3. "github.com/tealeg/xlsx"
  4. )
  5. // XLSX exports the Dataset as a byte array representing the .xlsx format.
  6. func (d *Dataset) XLSX() (*Exportable, error) {
  7. file := xlsx.NewFile()
  8. if err := d.addXlsxSheetToFile(file, "Sheet 1"); err != nil {
  9. return nil, err
  10. }
  11. b := newBuffer()
  12. file.Write(b)
  13. return newExportable(b), nil
  14. }
  15. // XLSX returns a XLSX representation of the Databook as an exportable.
  16. func (d *Databook) XLSX() (*Exportable, error) {
  17. file := xlsx.NewFile()
  18. for _, s := range d.sheets {
  19. s.dataset.addXlsxSheetToFile(file, s.title)
  20. }
  21. b := newBuffer()
  22. file.Write(b)
  23. return newExportable(b), nil
  24. }
  25. func (d *Dataset) addXlsxSheetToFile(file *xlsx.File, sheetName string) error {
  26. sheet, err := file.AddSheet(sheetName)
  27. if err != nil {
  28. return nil
  29. }
  30. back := d.Records()
  31. for i, r := range back {
  32. row := sheet.AddRow()
  33. for _, c := range r {
  34. cell := row.AddCell()
  35. cell.Value = c
  36. if i == 0 {
  37. cell.GetStyle().Font.Bold = true
  38. }
  39. }
  40. }
  41. return nil
  42. }