qax-os / excelize

Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
https://xuri.me/excelize
BSD 3-Clause "New" or "Revised" License
18.28k stars 1.71k forks source link

如何批量写入数据 #364

Closed lengrongfu closed 5 years ago

lengrongfu commented 5 years ago

如果遇到一个需要写入100万行的数据,如何高效的写入?

mlh758 commented 5 years ago

What sort of limitations are you running into with a million rows? Are you running out of memory?

lengrongfu commented 5 years ago

写了N久都没有写进去。

exfly commented 5 years ago

@lengrongfu csv, if dificute to write to excel?~

Nisus-Liu commented 5 years ago

请问怎么遍历二维数组, 写入Excel呀, 我看api都是要指定单元格写数据, 如 "A2", "B3".

exfly commented 5 years ago

@Nisus-Liu like this?

func axis(rowN, colN int) string {
    return excelize.ToAlphaString(colN) + strconv.Itoa(rowN+1)
}
var sheet string
for rowN :=range data{
    for colNum :=range data[rowNum]{
        xlsx.SetCellValue(sheet, axis(rowNum, colNum), data[rowNum][colNum])
    } 
}

it is comfortable for me

han2015 commented 5 years ago

use data slice as the third parameters of SetSheetRow

for k, v := range arrs {
    f.SetSheetRow("sheet1", fmt.Sprint("A", k+1), &[]string{v.field1,v.field2,v.field3,....v.fieldN})
}
haoyutc commented 4 years ago

SetSheetRow()只有这一种方式吗?有木有一次插入1000行、5000行、、、

xuri commented 4 years ago

Please using stream writer for generating a new worksheet with huge amounts of data.

zmjaction commented 2 years ago
ToAlphaString

ToAlphaString 在 v2.0.0 API已经删除

xuri commented 2 years ago

Please using ColumnNumberToName instead of ToAlphaString.

CarsonSlovoka commented 2 years ago

work for v2.6.1

package main

import (
    "fmt"
    "github.com/xuri/excelize/v2"
    "strconv"
)

func axis(rowN, colN int) string {
    colName, err := excelize.ColumnNumberToName(colN + 1)
    if err != nil {
        panic(err)
    }
    return colName + strconv.Itoa(rowN+1)
}

func Run(datas [][]string) {
    f := excelize.NewFile()
    sheetName := "ocr"
    index := f.NewSheet(sheetName)

    for rowN := range datas {
        for colNum := range datas[rowN] {
            if err := f.SetCellValue(sheetName, axis(rowN, colNum), datas[rowN][colNum]); err != nil {
                panic(err)
            }
        }
    }

    f.SetActiveSheet(index)

    if err := f.SaveAs("my.xlsx"); err != nil {
        fmt.Println(err)
    }
}
butterfly4147 commented 1 year ago
a usage from https://github.com/qax-os/excelize/issues/364#issuecomment-478312887
//axis(1, 1) return "A1"
func axis(rowN, colN int) string {
    return excelize.ToAlphaString(colN-1) + strconv.Itoa(rowN)
}