write.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package xlsx
  2. import "reflect"
  3. // Writes an array to row r. Accepts a pointer to array type 'e',
  4. // and writes the number of columns to write, 'cols'. If 'cols' is < 0,
  5. // the entire array will be written if possible. Returns -1 if the 'e'
  6. // doesn't point to an array, otherwise the number of columns written.
  7. func (r *Row) WriteSlice(e interface{}, cols int) int {
  8. if cols == 0 {
  9. return cols
  10. }
  11. t := reflect.TypeOf(e).Elem()
  12. if t.Kind() != reflect.Slice { // is 'e' even a slice?
  13. return -1
  14. }
  15. // it's a slice, so open up its values
  16. v := reflect.ValueOf(e).Elem()
  17. n := v.Len()
  18. if cols < n && cols > 0 {
  19. n = cols
  20. }
  21. var i int
  22. switch t.Elem().Kind() { // underlying type of slice
  23. case reflect.String:
  24. for i = 0; i < n; i++ {
  25. cell := r.AddCell()
  26. cell.SetString(v.Index(i).Interface().(string))
  27. }
  28. case reflect.Int, reflect.Int8,
  29. reflect.Int16, reflect.Int32:
  30. for i = 0; i < n; i++ {
  31. cell := r.AddCell()
  32. cell.SetInt(v.Index(i).Interface().(int))
  33. }
  34. case reflect.Int64:
  35. for i = 0; i < n; i++ {
  36. cell := r.AddCell()
  37. cell.SetInt64(v.Index(i).Interface().(int64))
  38. }
  39. case reflect.Bool:
  40. for i = 0; i < n; i++ {
  41. cell := r.AddCell()
  42. cell.SetBool(v.Index(i).Interface().(bool))
  43. }
  44. case reflect.Float64, reflect.Float32:
  45. for i = 0; i < n; i++ {
  46. cell := r.AddCell()
  47. cell.SetFloat(v.Index(i).Interface().(float64))
  48. }
  49. }
  50. return i
  51. }
  52. // Writes a struct to row r. Accepts a pointer to struct type 'e',
  53. // and the number of columns to write, `cols`. If 'cols' is < 0,
  54. // the entire struct will be written if possible. Returns -1 if the 'e'
  55. // doesn't point to a struct, otherwise the number of columns written
  56. func (r *Row) WriteStruct(e interface{}, cols int) int {
  57. if cols == 0 {
  58. return cols
  59. }
  60. v := reflect.ValueOf(e).Elem()
  61. if v.Kind() != reflect.Struct {
  62. return -1 // bail if it's not a struct
  63. }
  64. n := v.NumField() // number of fields in struct
  65. if cols < n && cols > 0 {
  66. n = cols
  67. }
  68. var k int
  69. for i := 0; i < n; i, k = i+1, k+1 {
  70. f := v.Field(i).Kind()
  71. cell := r.AddCell()
  72. switch f {
  73. case reflect.Int, reflect.Int8,
  74. reflect.Int16, reflect.Int32:
  75. cell.SetInt(v.Field(i).Interface().(int))
  76. case reflect.Int64:
  77. cell.SetInt64(v.Field(i).Interface().(int64))
  78. case reflect.String:
  79. cell.SetString(v.Field(i).Interface().(string))
  80. case reflect.Float64, reflect.Float32:
  81. cell.SetFloat(v.Field(i).Interface().(float64))
  82. case reflect.Bool:
  83. cell.SetBool(v.Field(i).Interface().(bool))
  84. default:
  85. k-- // nothing set so reset to previous
  86. }
  87. }
  88. return k
  89. }