|
| 1 | +using OfficeOpenXml; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.ComponentModel; |
| 5 | + |
| 6 | +namespace massive_sheet_splitter |
| 7 | +{ |
| 8 | + public class ExcelSheetData |
| 9 | + { |
| 10 | + public string SheetName { get; set; } |
| 11 | + public List<List<string>> Rows { get; set; } = new List<List<string>>(); |
| 12 | + } |
| 13 | + |
| 14 | + public class ExcelHelper |
| 15 | + { |
| 16 | + public ExcelHelper() |
| 17 | + { |
| 18 | + ExcelPackage.License.SetNonCommercialPersonal("rcht"); |
| 19 | + } |
| 20 | + |
| 21 | + public List<ExcelSheetData> read_excel(string file_path) |
| 22 | + { |
| 23 | + var workbookData = new List<ExcelSheetData>(); |
| 24 | + |
| 25 | + using (var package = new ExcelPackage(new FileInfo(file_path))) |
| 26 | + { |
| 27 | + foreach (var sheet in package.Workbook.Worksheets) |
| 28 | + { |
| 29 | + var sheetData = new ExcelSheetData { SheetName = sheet.Name }; |
| 30 | + |
| 31 | + int rowCount = sheet.Dimension.Rows; |
| 32 | + int colCount = sheet.Dimension.Columns; |
| 33 | + |
| 34 | + for (int row = 1; row <= rowCount; row++) |
| 35 | + { |
| 36 | + var rowData = new List<string>(); |
| 37 | + for (int col = 1; col <= colCount; col++) |
| 38 | + { |
| 39 | + rowData.Add(sheet.Cells[row, col].Text); |
| 40 | + } |
| 41 | + sheetData.Rows.Add(rowData); |
| 42 | + } |
| 43 | + |
| 44 | + workbookData.Add(sheetData); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return workbookData; |
| 49 | + } |
| 50 | + |
| 51 | + public void write_excel(string dest_path, List<ExcelSheetData> sheets, int start_row, int end_row, int last_header_row) |
| 52 | + { |
| 53 | + using (var package = new ExcelPackage()) |
| 54 | + { |
| 55 | + foreach (var sheetData in sheets) |
| 56 | + { |
| 57 | + var ws = package.Workbook.Worksheets.Add(sheetData.SheetName); |
| 58 | + |
| 59 | + int currentRow = 1; |
| 60 | + for (int i = 0; i < last_header_row; i++) |
| 61 | + { |
| 62 | + for (int col = 0; col < sheetData.Rows[i].Count; col++) |
| 63 | + { |
| 64 | + ws.Cells[currentRow, col + 1].Value = sheetData.Rows[i][col]; |
| 65 | + } |
| 66 | + currentRow++; |
| 67 | + } |
| 68 | + |
| 69 | + for (int i = start_row - 1; i < end_row && i < sheetData.Rows.Count; i++) |
| 70 | + { |
| 71 | + for (int col = 0; col < sheetData.Rows[i].Count; col++) |
| 72 | + { |
| 73 | + ws.Cells[currentRow, col + 1].Value = sheetData.Rows[i][col]; |
| 74 | + } |
| 75 | + currentRow++; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + package.SaveAs(new FileInfo(dest_path)); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public (string SheetName, int RowCount) GetSheetWithMostRows(List<ExcelSheetData> data) |
| 84 | + { |
| 85 | + string maxSheetName = ""; |
| 86 | + int maxRowCount = 0; |
| 87 | + |
| 88 | + foreach (var sheet in data) |
| 89 | + { |
| 90 | + int rowCount = sheet.Rows.Count; |
| 91 | + if (rowCount > maxRowCount) |
| 92 | + { |
| 93 | + maxRowCount = rowCount; |
| 94 | + maxSheetName = sheet.SheetName; |
| 95 | + } |
| 96 | + } |
| 97 | + return (maxSheetName, maxRowCount); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments