You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
1.5 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package xlsx
import (
"joybuywork/internal/plugins/export"
)
type Task struct {
export.BaseTask
*Export
}
// UpgradeTask xlsx导出升级成任务模式
func (e *Export) UpgradeTask(opts ...export.TaskOption) (*Task, error) {
task := &Task{
Export: e,
}
//初始化任务
task.Init(e.Ctx, task, opts...)
//设置表头,多sheet的情况下,不用设置表头
if !task.HasManySheet() {
err := task.setHeader()
if err != nil {
return nil, err
}
}
return task, nil
}
// 写入xlsx表格
func (t *Task) writeRow(row SheetRowData) error {
//写入一行
t.FrAdd()
//不能写入当前行数据
if !t.CanWriteRow() {
return nil
}
for axis, value := range row.row {
err := t.file.SetCellValue(row.sheet, axis, value)
if err != nil {
return err
}
}
//看看任务有没有完成
t.CheckFinish()
return nil
}
// WriteData 写入数据
func (t *Task) WriteData(data []export.RowData) (err error) {
list := t.getList(data)
for k, v := range list {
//自动切sheet
ok := t.checkSwitchSheet()
if ok { //切换成功,要重新设置当前sheet在哪一行写入
return t.WriteData(data[k:])
}
//写入一行
err = t.writeRow(v)
if err != nil {
return
}
}
return
}
// FileClose 关闭文件资源
func (t *Task) FileClose() {
_ = t.file.SaveAs(t.GetFullPath())
}
// GetTotal 获取导出的总条数
func (t *Task) GetTotal() int64 {
return t.Total
}
// Done 执行任务
func (t *Task) Done() {
export.Done(t)
}