refactor: Simplify data handling in processWriteAll function
- Replace individual row handling with a unified approach to convert input values into a two-dimensional slice of interfaces. - Streamline the writing process by directly passing the constructed slice to the WriteAll method, enhancing code clarity and maintainability.
This commit is contained in:
parent
efb588bbd1
commit
bc576f1bab
1 changed files with 10 additions and 38 deletions
|
|
@ -250,51 +250,23 @@ func processWriteAll(process *process.Process) interface{} {
|
||||||
exception.New("excel.write.all %s error: %s", 500, handle, err.Error()).Throw()
|
exception.New("excel.write.all %s error: %s", 500, handle, err.Error()).Throw()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理二维切片值
|
// Convert data to [][]interface{}
|
||||||
|
var sheetData [][]interface{}
|
||||||
if arr, ok := values.([]interface{}); ok {
|
if arr, ok := values.([]interface{}); ok {
|
||||||
for _, row := range arr {
|
for _, row := range arr {
|
||||||
if rowArr, ok := row.([]interface{}); ok {
|
if rowArr, ok := row.([]interface{}); ok {
|
||||||
// 对于每行,使用 SetSheetRow
|
sheetData = append(sheetData, rowArr)
|
||||||
err = xls.SetSheetRow(sheet, cell, &rowArr)
|
|
||||||
if err != nil {
|
|
||||||
exception.New("excel.write.all %s:%s:%s error: %s", 500, handle, sheet, cell, err.Error()).Throw()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 移动到下一行
|
|
||||||
colIndex, rowIndex, err := excelize.CellNameToCoordinates(cell)
|
|
||||||
if err != nil {
|
|
||||||
exception.New("excel.write.all %s:%s:%s error: %s", 500, handle, sheet, cell, err.Error()).Throw()
|
|
||||||
}
|
|
||||||
cell, err = excelize.CoordinatesToCellName(colIndex, rowIndex+1)
|
|
||||||
if err != nil {
|
|
||||||
exception.New("excel.write.all %s:%s:%s error: %s", 500, handle, sheet, cell, err.Error()).Throw()
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// 单元素行
|
sheetData = append(sheetData, []interface{}{row})
|
||||||
singleRow := []interface{}{row}
|
|
||||||
err = xls.SetSheetRow(sheet, cell, &singleRow)
|
|
||||||
if err != nil {
|
|
||||||
exception.New("excel.write.all %s:%s:%s error: %s", 500, handle, sheet, cell, err.Error()).Throw()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 移动到下一行
|
|
||||||
colIndex, rowIndex, err := excelize.CellNameToCoordinates(cell)
|
|
||||||
if err != nil {
|
|
||||||
exception.New("excel.write.all %s:%s:%s error: %s", 500, handle, sheet, cell, err.Error()).Throw()
|
|
||||||
}
|
|
||||||
cell, err = excelize.CoordinatesToCellName(colIndex, rowIndex+1)
|
|
||||||
if err != nil {
|
|
||||||
exception.New("excel.write.all %s:%s:%s error: %s", 500, handle, sheet, cell, err.Error()).Throw()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 单元素
|
sheetData = [][]interface{}{{values}}
|
||||||
singleElement := []interface{}{values}
|
}
|
||||||
err = xls.SetSheetRow(sheet, cell, &singleElement)
|
|
||||||
if err != nil {
|
err = xls.WriteAll(sheet, cell, sheetData)
|
||||||
exception.New("excel.write.all %s:%s:%s error: %s", 500, handle, sheet, cell, err.Error()).Throw()
|
if err != nil {
|
||||||
}
|
exception.New("excel.write.all %s:%s error: %s", 500, handle, sheet, err.Error()).Throw()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue