/// <summary> /// 循环插入数据 /// </summary> /// <param name="startRowIndex"></param> /// <param name="setValueAction"></param> public void CirculateLetterSetValue(int startRowIndex, Func <int, ExcelRowItem, bool> setValueAction) { if (_currentSheet.LastRowNum <= startRowIndex) { _currentSheet.CreateRow(startRowIndex); } int currentRowIndex = startRowIndex; bool isWhile = false; int dataIndex = 0; do { ExcelRowItem item = new ExcelRowItem(); isWhile = setValueAction.Invoke(dataIndex, item); if (isWhile) { _currentSheet.CopyRow(currentRowIndex, currentRowIndex + 1); } SetRowValue(currentRowIndex, item); currentRowIndex++; dataIndex++; } while (isWhile); }
public void SetRowValue(int rowIndex, ExcelRowItem rowItem) { if (_currentSheet.LastRowNum <= rowIndex) { _currentSheet.CreateRow(rowIndex); } foreach (var itemKey in rowItem.Keys) { var value = rowItem.Get(itemKey); SetCellValue(rowIndex, value, itemKey); } }
/// <summary> /// 循环获取数据 /// </summary> /// <param name="startRowIndex"></param> /// <param name="setValueAction"></param> /// <returns></returns> public async Task AsyncCirculateLetterGetValue(int startRowIndex, Func <int, ExcelRowItem, Task> setValueAction) { if (_currentSheet.LastRowNum < startRowIndex) { return; } for (int currentRowIndex = startRowIndex, dataIndex = 0; currentRowIndex <= _currentSheet.LastRowNum; currentRowIndex++, dataIndex++) { var currentRow = _currentSheet.GetRow(currentRowIndex); ExcelRowItem rowDictionary = new ExcelRowItem(); foreach (var currentRowCell in currentRow.Cells) { string cellValue = ExcelHelper.GetCellValue(currentRowCell).ToStr(); int cellIndex = currentRowCell.ColumnIndex; rowDictionary.Add(cellValue, GetColumnLetter(cellIndex)); } await setValueAction.Invoke(dataIndex, rowDictionary); } }