golang excelize 如何复制单元格及其格式

发布于 2025-01-10 21:29:55 字数 2182 浏览 2 评论 0原文

我使用 github.com/xuri/excelize/v2 来处理 Excel 文件。

我将许多 Excel 文件中的许多工作表附加到一个 Excel 中的一张工作表中。

下面是示例代码。

    var mergedRows [][]string
    for _, f := range files {
        excelPath := folder + "/" + f.Name()
        rows := loadXlsx(excelPath, sheetName)
        for _, row := range rows[rowOffset:] {
            mergedRows = append(mergedRows, row)
        }
    }

    saveXlsx(aggregatedFilePath, sheetName, mergedRows, rowOffset)


...

func loadXlsx(xlsxPath string, sheetName string) [][]string {

    f, err := excelize.OpenFile(xlsxPath)
    if err != nil {
        log.Fatal(err)
    }

    defer func() {
        if err := f.Close(); err != nil{
            fmt.Println(err)
        }
    }()

    rows, err := f.GetRows(sheetName)
    if err != nil {
        log.Fatal(err)
    }

    return rows
}

func saveXlsx(path string, sheetName string, rows [][]string, rowOffset int) {

    f, err := excelize.OpenFile(path)
    if err != nil {
        log.Fatal(err)
    }

    defer func() {
        if err := f.Close(); err != nil{
            fmt.Println(err)
        }
    }()


    index := f.GetSheetIndex(sheetName)
    offset := 1
    sequence := 1
    for _, row := range rows{
        row[0] = strconv.Itoa(sequence)
        sequence = sequence + 1
        offset = offset + 1
        axis := "A" + strconv.Itoa(offset)
        f.SetSheetRow(sheetName, axis, &row)
    }

    for index, _ := range rows[0] {
        axis, _ := excelize.CoordinatesToCellName(index, 2)
        column, _ := excelize.ColumnNumberToName(index)
        styleId, _ := f.GetCellStyle(sheetName, axis)
        cellType, _ := f.GetCellType(sheetName, axis)
        fmt.Println(styleId)
        fmt.Println(cellType)
        f.SetColStyle(sheetName, column, styleId)
    }


    f.SetActiveSheet(index)
    if err := f.Save(); err != nil {
        fmt.Println(err)
    }

}

除了一些数据格式问题之外,这是可行的。号码样式被复制,但不起作用;日期已复制,但值错误。

  1. 在源文件中,有一些数字为 2 位十进制格式,显示为 70.12,而在输出文件中,格式相同,但显示为 70.119

  2. 在源文件中,有一些年/月/日格式的日期,显示为2022/1/12,而在输出文件中,格式相同,但显示为>01-12-22

I use github.com/xuri/excelize/v2 to process the excel file.

I append many sheets in many excel files into one sheet in one excel.

Below is the sample code.

    var mergedRows [][]string
    for _, f := range files {
        excelPath := folder + "/" + f.Name()
        rows := loadXlsx(excelPath, sheetName)
        for _, row := range rows[rowOffset:] {
            mergedRows = append(mergedRows, row)
        }
    }

    saveXlsx(aggregatedFilePath, sheetName, mergedRows, rowOffset)


...

func loadXlsx(xlsxPath string, sheetName string) [][]string {

    f, err := excelize.OpenFile(xlsxPath)
    if err != nil {
        log.Fatal(err)
    }

    defer func() {
        if err := f.Close(); err != nil{
            fmt.Println(err)
        }
    }()

    rows, err := f.GetRows(sheetName)
    if err != nil {
        log.Fatal(err)
    }

    return rows
}

func saveXlsx(path string, sheetName string, rows [][]string, rowOffset int) {

    f, err := excelize.OpenFile(path)
    if err != nil {
        log.Fatal(err)
    }

    defer func() {
        if err := f.Close(); err != nil{
            fmt.Println(err)
        }
    }()


    index := f.GetSheetIndex(sheetName)
    offset := 1
    sequence := 1
    for _, row := range rows{
        row[0] = strconv.Itoa(sequence)
        sequence = sequence + 1
        offset = offset + 1
        axis := "A" + strconv.Itoa(offset)
        f.SetSheetRow(sheetName, axis, &row)
    }

    for index, _ := range rows[0] {
        axis, _ := excelize.CoordinatesToCellName(index, 2)
        column, _ := excelize.ColumnNumberToName(index)
        styleId, _ := f.GetCellStyle(sheetName, axis)
        cellType, _ := f.GetCellType(sheetName, axis)
        fmt.Println(styleId)
        fmt.Println(cellType)
        f.SetColStyle(sheetName, column, styleId)
    }


    f.SetActiveSheet(index)
    if err := f.Save(); err != nil {
        fmt.Println(err)
    }

}

This works, except some data format issues. the number's style is copyed, but not works; the date is copyed, but with wrong value.

  1. In the source file, there has some number with 2 decimal format and shows like 70.12, while in the output file the format is the same but shows like 70.119.

  2. In the source file, there has some date with Y/m/d format and shows like 2022/1/12, while in the output file the format is the same but shows like 01-12-22.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里°也失望 2025-01-17 21:29:55

来自手册

func (f *File) GetRows(sheet string, opts ...Options) ([][]string, error)

如果单元格格式可以应用于单元格的值,则
将使用应用的值,否则将使用原始值。

因此,在我的问题中, rows, err := f.GetRows(sheetName) 将以格式复制日期和数字值,而不是原始数字。格式化的值可以转换为不等值。

解决方案只是使用 RawCellValue 选项 true 读取原始值,

rows, err := f.GetRows(sheetName, excelize.Options{RawCellValue:true})

如果格式更改,只需将原始文件中的样式应用到新文件即可。

From the manual

func (f *File) GetRows(sheet string, opts ...Options) ([][]string, error)

If the cell format can be applied to the value of the cell, the
applied value will be used, otherwise the original value will be used.

So in my question, rows, err := f.GetRows(sheetName) will copy the date and number value with format, not the original number. The formated value may be convert to non equal value.

The solution is just read the raw value with RawCellValue option true,

rows, err := f.GetRows(sheetName, excelize.Options{RawCellValue:true})

If the format is changed, just apply the style from the original file to the new file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文