example_read_test.go 736 B

1234567891011121314151617181920212223242526272829303132333435
  1. package xlsx
  2. import "fmt"
  3. func ExampleRow_ReadStruct() {
  4. //example type
  5. type structTest struct {
  6. IntVal int `xlsx:"0"`
  7. StringVal string `xlsx:"1"`
  8. FloatVal float64 `xlsx:"2"`
  9. IgnoredVal int `xlsx:"-"`
  10. BoolVal bool `xlsx:"4"`
  11. }
  12. structVal := structTest{
  13. IntVal: 16,
  14. StringVal: "heyheyhey :)!",
  15. FloatVal: 3.14159216,
  16. IgnoredVal: 7,
  17. BoolVal: true,
  18. }
  19. //create a new xlsx file and write a struct
  20. //in a new row
  21. f := NewFile()
  22. sheet, _ := f.AddSheet("TestRead")
  23. row := sheet.AddRow()
  24. row.WriteStruct(&structVal, -1)
  25. //read the struct from the same row
  26. readStruct := &structTest{}
  27. err := row.ReadStruct(readStruct)
  28. if err != nil {
  29. panic(err)
  30. }
  31. fmt.Println(readStruct)
  32. }