github.com/team-ide/go-dialect@v1.9.20/worker/data_source_excel.go (about)

     1  package worker
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"github.com/tealeg/xlsx"
     7  	"github.com/team-ide/go-dialect/dialect"
     8  )
     9  
    10  func NewDataSourceExcel(param *DataSourceParam) (res DataSource) {
    11  	res = &dataSourceExcel{
    12  		DataSourceParam: param,
    13  	}
    14  	return
    15  }
    16  
    17  type dataSourceExcel struct {
    18  	*DataSourceParam
    19  	xlsxFForRead  *xlsx.File
    20  	xlsxFForWrite *xlsx.File
    21  	sheetForWrite *xlsx.Sheet
    22  	isStop        bool
    23  	headerWritten bool
    24  }
    25  
    26  func (this_ *dataSourceExcel) Stop() {
    27  	this_.isStop = true
    28  }
    29  
    30  func (this_ *dataSourceExcel) ReadStart() (err error) {
    31  	if this_.Path == "" {
    32  		err = errors.New("文件地址不能为空")
    33  		return
    34  	}
    35  	this_.xlsxFForRead, err = xlsx.OpenFile(this_.Path)
    36  	if err != nil {
    37  		err = errors.New("excel [" + this_.Path + "] open error, " + err.Error())
    38  		return
    39  	}
    40  	return
    41  }
    42  func (this_ *dataSourceExcel) ReadEnd() (err error) {
    43  	if this_.xlsxFForRead != nil {
    44  	}
    45  	return
    46  }
    47  func (this_ *dataSourceExcel) Read(columnList []*dialect.ColumnModel, onRead func(data *DataSourceData) (err error)) (err error) {
    48  	defer func() {
    49  		if e := recover(); e != nil {
    50  			err = errors.New(fmt.Sprint(e))
    51  		}
    52  	}()
    53  
    54  	if this_.xlsxFForRead == nil {
    55  		err = this_.ReadStart()
    56  		if err != nil {
    57  			return
    58  		}
    59  	}
    60  
    61  	sheets := this_.xlsxFForRead.Sheets
    62  	if this_.SheetIndex >= 0 {
    63  		if len(sheets) < this_.SheetIndex+1 {
    64  			err = errors.New("excel [" + this_.Path + "] sheets len is [" + fmt.Sprint(len(sheets)) + "]")
    65  			return
    66  		}
    67  	}
    68  
    69  	startRow := this_.StartRow - 1
    70  	if startRow < 0 {
    71  		startRow = 0
    72  	}
    73  	for index, sheet := range sheets {
    74  		if this_.SheetIndex >= 0 {
    75  			if index != this_.SheetIndex {
    76  				continue
    77  			}
    78  		}
    79  		if this_.isStop {
    80  			return
    81  		}
    82  		maxRow := sheet.MaxRow
    83  
    84  		for rowIndex := startRow; rowIndex < maxRow; rowIndex++ {
    85  
    86  			if this_.isStop {
    87  				return
    88  			}
    89  
    90  			row := sheet.Rows[rowIndex]
    91  
    92  			var data = map[string]interface{}{}
    93  
    94  			for cellIndex, column := range columnList {
    95  				if cellIndex >= len(row.Cells) {
    96  					break
    97  				}
    98  				cell := row.Cells[cellIndex]
    99  				var v = cell.String()
   100  				if !column.ColumnNotNull && v == "" {
   101  					continue
   102  				}
   103  				var info *dialect.ColumnTypeInfo
   104  				info, err = this_.Dia.GetColumnTypeInfo(column)
   105  				if err != nil {
   106  					return
   107  				}
   108  				if info.IsDateTime || info.IsNumber {
   109  					if v == "" {
   110  						continue
   111  					}
   112  				}
   113  				data[column.ColumnName] = v
   114  			}
   115  			err = onRead(&DataSourceData{
   116  				HasData: true,
   117  				Data:    data,
   118  			})
   119  			if err != nil {
   120  				return
   121  			}
   122  		}
   123  	}
   124  	return
   125  }
   126  
   127  func (this_ *dataSourceExcel) save() (err error) {
   128  	if this_.Path == "" {
   129  		err = errors.New("文件地址不能为空")
   130  		return
   131  	}
   132  	err = this_.xlsxFForWrite.Save(this_.Path)
   133  	if err != nil {
   134  		err = errors.New("excel [" + this_.Path + "] save error, " + err.Error())
   135  		return
   136  	}
   137  	return
   138  }
   139  
   140  func (this_ *dataSourceExcel) WriteStart() (err error) {
   141  	this_.xlsxFForWrite = xlsx.NewFile()
   142  
   143  	sheetName := this_.SheetName
   144  	if len(sheetName) > 31 {
   145  		sheetName = sheetName[0:30]
   146  	}
   147  	this_.sheetForWrite, err = this_.xlsxFForWrite.AddSheet(sheetName)
   148  	if err != nil {
   149  		err = errors.New("excel [" + this_.Path + "] add shell [" + this_.SheetName + "] error, " + err.Error())
   150  		return
   151  	}
   152  
   153  	if len(this_.TitleList) > 0 {
   154  		var valueList []interface{}
   155  		for _, title := range this_.TitleList {
   156  			valueList = append(valueList, title)
   157  		}
   158  		sheetWrite(this_.sheetForWrite, valueList)
   159  	}
   160  	err = this_.save()
   161  	if err != nil {
   162  		return
   163  	}
   164  	return
   165  }
   166  func (this_ *dataSourceExcel) WriteEnd() (err error) {
   167  	err = this_.save()
   168  	if err != nil {
   169  		return
   170  	}
   171  	return
   172  }
   173  
   174  func (this_ *dataSourceExcel) WriteHeader(columnList []*dialect.ColumnModel) (err error) {
   175  	if this_.headerWritten {
   176  		return
   177  	}
   178  	this_.headerWritten = true
   179  
   180  	if this_.xlsxFForWrite == nil {
   181  		err = this_.WriteStart()
   182  		if err != nil {
   183  			return
   184  		}
   185  	}
   186  
   187  	if this_.isStop {
   188  		return
   189  	}
   190  
   191  	var valueList []interface{}
   192  	for _, column := range columnList {
   193  		valueList = append(valueList, column.ColumnName)
   194  	}
   195  
   196  	sheetWrite(this_.sheetForWrite, valueList)
   197  	return
   198  }
   199  
   200  func (this_ *dataSourceExcel) Write(data *DataSourceData) (err error) {
   201  	defer func() {
   202  		if e := recover(); e != nil {
   203  			err = errors.New(fmt.Sprint(e))
   204  		}
   205  	}()
   206  
   207  	if this_.xlsxFForWrite == nil {
   208  		err = this_.WriteStart()
   209  		if err != nil {
   210  			return
   211  		}
   212  	}
   213  	if this_.isStop {
   214  		return
   215  	}
   216  	columnList := data.ColumnList
   217  	if data.Data == nil || columnList == nil {
   218  		return
   219  	}
   220  	var valueList []interface{}
   221  	for _, column := range data.ColumnList {
   222  		valueList = append(valueList, data.Data[column.ColumnName])
   223  	}
   224  	sheetWrite(this_.sheetForWrite, valueList)
   225  	return
   226  }
   227  
   228  func sheetWrite(sheet *xlsx.Sheet, valueList []interface{}) {
   229  	row := sheet.AddRow()
   230  	for _, value := range valueList {
   231  		str := dialect.GetStringValue(value)
   232  		call := row.AddCell()
   233  		call.SetValue(str)
   234  	}
   235  }