sheet.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package xlsx
  2. import (
  3. // "bytes"
  4. // "encoding/xml"
  5. "fmt"
  6. "strconv"
  7. )
  8. // Sheet is a high level structure intended to provide user access to
  9. // the contents of a particular sheet within an XLSX file.
  10. type Sheet struct {
  11. Rows []*Row
  12. MaxRow int
  13. MaxCol int
  14. }
  15. // Add a new Row to a Sheet
  16. func (s *Sheet) AddRow() *Row {
  17. row := &Row{}
  18. s.Rows = append(s.Rows, row)
  19. if len(s.Rows) > s.MaxRow {
  20. s.MaxRow = len(s.Rows)
  21. }
  22. return row
  23. }
  24. // Dump sheet to it's XML representation
  25. func (s *Sheet) makeXLSXSheet(refTable *RefTable) *xlsxWorksheet {
  26. worksheet := &xlsxWorksheet{}
  27. xSheet := xlsxSheetData{}
  28. maxRow := 0
  29. maxCell := 0
  30. for r, row := range s.Rows {
  31. if r > maxRow {
  32. maxRow = r
  33. }
  34. xRow := xlsxRow{}
  35. xRow.R = r + 1
  36. for c, cell := range row.Cells {
  37. if c > maxCell {
  38. maxCell = c
  39. }
  40. xC := xlsxC{}
  41. xC.R = fmt.Sprintf("%s%d", numericToLetters(c), r + 1)
  42. xC.V = strconv.Itoa(refTable.AddString(cell.Value))
  43. xC.T = "s" // Hardcode string type, for now.
  44. xRow.C = append(xRow.C, xC)
  45. }
  46. xSheet.Row = append(xSheet.Row, xRow)
  47. }
  48. worksheet.SheetData = xSheet
  49. dimension := xlsxDimension{}
  50. dimension.Ref = fmt.Sprintf("A1:%s%d",
  51. numericToLetters(maxCell), maxRow + 1)
  52. worksheet.Dimension = dimension
  53. return worksheet
  54. // output := bytes.NewBufferString(xml.Header)
  55. // body, err := xml.MarshalIndent(worksheet, " ", " ")
  56. // if err != nil {
  57. // return nil, err
  58. // }
  59. // _, err = output.Write(body)
  60. // if err != nil {
  61. // return nil, err
  62. // }
  63. // return output.Bytes(), nil
  64. }