private void Save(ExcelWorkbook workbook, VSheet sheet, bool saveAs) { string outputPath = null; if (saveAs) { SaveFileDialog dialog = new SaveFileDialog(); dialog.Filter = FileForm.FILE_FILTER; dialog.Title = "另存为"; dialog.FileName = Path.GetFileName(workbook.filePath); dialog.InitialDirectory = Path.GetDirectoryName(workbook.filePath); dialog.FilterIndex = FileForm.GetFilterIndex(workbook.filePath); if (dialog.ShowDialog(this) == DialogResult.OK) { outputPath = dialog.FileName; } else { return; } } try { workbook.SaveSheet(sheet, outputPath); } catch (Exception ex) { MessageBox.Show(this, ex.ToString()); } }
private void GetSummary() { _sheetDifferences.Clear(); for (int i = 0; i < leftWorkbook.sheetNames.Count; i++) { string sheetName = leftWorkbook.sheetNames[i]; if (rightWorkbook.sheetNames.Contains(sheetName)) { ExcelSheet leftExcelSheet = leftWorkbook.LoadSheet(sheetName); ExcelSheet rightExcelSheet = rightWorkbook.LoadSheet(sheetName); int columnCount = Math.Max(leftExcelSheet.columnCount, rightExcelSheet.columnCount); VSheet leftSheet = new VSheet(leftExcelSheet, columnCount); VSheet rightSheet = new VSheet(rightExcelSheet, columnCount); diff_match_patch comparer = new diff_match_patch(); string leftContent = leftSheet.GetContent(); string rightContent = rightSheet.GetContent(); List <Diff> diffs = comparer.diff_main(leftContent, rightContent, true); comparer.diff_cleanupSemanticLossless(diffs); bool isDifferent = false; for (int diffIndex = 0; diffIndex < diffs.Count; diffIndex++) { if (diffs[diffIndex].operation != Operation.EQUAL) { isDifferent = true; break; } } _sheetDifferences.Add(sheetName, isDifferent); } } }
public VRow(VSheet sheet, int rowIndex, IExcelRow referenceRow) { _sheet = sheet; _rowIndex = rowIndex; if (referenceRow != null) { if (referenceRow is VRow) { VRow row = referenceRow as VRow; _realRowIndex = row.realRowIndex; _targetRowIndex = row.targetRowIndex; } else { ExcelRow row = referenceRow as ExcelRow; _realRowIndex = rowIndex; } for (int columnIndex = 0; columnIndex < sheet.columnCount; columnIndex++) { IExcelCell cell = referenceRow.GetCell(columnIndex); IExcelCell newCell = null; if (cell == null) { newCell = new ExcelCell(_rowIndex, columnIndex); } else { newCell = new ExcelCell(_rowIndex, columnIndex, cell.cellType, cell.value); } _columns.Add(newCell); } } else { _realRowIndex = -1; for (int columnIndex = 0; columnIndex < sheet.columnCount; columnIndex++) { _columns.Add(new ExcelCell(_rowIndex, columnIndex)); } } }
public void SaveSheet(VSheet sheet, string outputPath = null) { int sheetIndex = workbook.GetSheetIndex(sheet.name); if (sheetIndex == -1) { return; } ISheet worksheet = workbook.GetSheetAt(sheetIndex); if (worksheet != null) { sheet.Save(worksheet); } if (string.IsNullOrEmpty(outputPath)) { outputPath = _filePath; } using (FileStream fs = new FileStream(outputPath, FileMode.Create, FileAccess.Write)) { workbook.Write(fs); } }
public void Execute(IExcelSheet left, IExcelSheet right, int leftAlignIndex = -1, int rightAlignIndex = -1) { _isDifferent = false; _columnCount = Math.Max(left.columnCount, right.columnCount); _left = new VSheet(left, _columnCount); _right = new VSheet(right, _columnCount); _leftAlignIndex = leftAlignIndex; _rightAlignIndex = rightAlignIndex; if (_leftAlignIndex != -1 && _rightAlignIndex != -1) { int beginRow = 0; int endRow = 0; if (_leftAlignIndex > _rightAlignIndex) { endRow = _leftAlignIndex - 1; ((VSheet)_right).PadRowsAt(_rightAlignIndex, (_leftAlignIndex - _rightAlignIndex)); } else { endRow = _rightAlignIndex - 1; ((VSheet)_left).PadRowsAt(_leftAlignIndex, (_rightAlignIndex - _leftAlignIndex)); } leftContent.Clear(); rightContent.Clear(); { diff_match_patch comparer = new diff_match_patch(); string leftContent = _left.GetContent(beginRow, endRow); string rightContent = _right.GetContent(beginRow, endRow); List <Diff> diffs = comparer.diff_main(leftContent, rightContent, true); comparer.diff_cleanupSemantic(diffs); Compare(diffs, beginRow, beginRow); } leftContent.Clear(); rightContent.Clear(); { endRow++; diff_match_patch comparer = new diff_match_patch(); string leftContent = _left.GetContent(endRow, endRow); string rightContent = _right.GetContent(endRow, endRow); List <Diff> diffs = comparer.diff_main(leftContent, rightContent, true); comparer.diff_cleanupSemantic(diffs); Compare(diffs, endRow, endRow); } leftContent.Clear(); rightContent.Clear(); { endRow++; diff_match_patch comparer = new diff_match_patch(); string leftContent = _left.GetContent(endRow, _left.rowCount - 1); string rightContent = _right.GetContent(endRow, _right.rowCount - 1); List <Diff> diffs = comparer.diff_main(leftContent, rightContent, true); comparer.diff_cleanupSemantic(diffs); Compare(diffs, endRow, endRow); } } else { diff_match_patch comparer = new diff_match_patch(); string leftContent = _left.GetContent(); string rightContent = _right.GetContent(); List <Diff> diffs = comparer.diff_main(leftContent, rightContent, true); comparer.diff_cleanupSemantic(diffs); Compare(diffs, 0, 0); } VSheet leftResult = new VSheet(_left.name, _columnCount); VSheet rightResult = new VSheet(_right.name, _columnCount); List <int> deleteList = new List <int>(); for (int i = 0; i < _rows.Count; i++) { RowComparer row = _rows[i]; IExcelRow leftRow = _left.GetRow(row.leftRowIndex); IExcelRow rightRow = _right.GetRow(row.rightRowIndex); if (GetRealRowIndex(leftRow) == -1 && GetRealRowIndex(rightRow) == -1) { deleteList.Add(i); continue; } leftResult.NewRow(leftRow); rightResult.NewRow(rightRow); } for (int i = deleteList.Count - 1; i >= 0; i--) { _rows.RemoveAt(deleteList[i]); } _left = leftResult; _right = rightResult; }