Skip to content

SetCellValue with time.Time results in Excel "We found a problem with some content" #1464

@koodimetsa

Description

@koodimetsa

When converting a cell value to date(time.Time) the resulting file doesn't work in Excel.

Steps to reproduce the issue:

  1. Open a file with Excelice
  2. Read a cell value and convert it to time.Time. Then save the resulting time to cell.
  3. Save file and try to open it with Excel.

Describe the results you received:
Excel warns: "We found a problem with some content."

Describe the results you expected:
Valid xlsx-file.

Output of go version:

go version go1.20 darwin/amd64

Excelize version or commit ID:

v.2.7.0

Environment details (OS, Microsoft Excel™ version, physical, etc.):

OS: MacOS Ventura 13.2

Code to reproduce

package main

import (
	"fmt"
	"log"
	"strconv"
	"time"

	"github.com/xuri/excelize/v2"
)

func main() {
	f, err := excelize.OpenFile("_tests/test.xlsx")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer func() {
		// Close the spreadsheet.
		if err := f.Close(); err != nil {
			fmt.Println(err)
		}
	}()

	const column = "A"
	const sheet = "Sheet1"
	row := 2

	for {
		cell := column + strconv.Itoa(row)

		value, err := f.GetCellValue(sheet, cell)
		if err != nil {
			log.Println(err)
			return
		}
		if value == "" {
			break
		}

		date, err := time.Parse("January 02 2006", value)
		if err != nil {
			log.Println(err)
                        row++
			continue
		}

		if err := f.SetCellValue(sheet, cell, date); err != nil {
			log.Println(err)
			return
		}

		row++
	}

	err = f.SaveAs("_tests/testDateFixed.xlsx")
	if err != nil {
		log.Println(err)
		return
	}
}

ps. Thank you for an amazing library!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions