Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
2.2 kB
1
Indexable
Never
public static List<PowerProfile> ExportPowerProfileFromExcel(string filePath)
        {
            if (filePath is null || filePath.Equals(string.Empty))
                return null;

            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            List<PowerProfile> PowerProfile = new();
            int startRow = 2;

            using (var package = new ExcelPackage(new FileInfo(filePath)))
            {
                try
                {
                    ExcelWorksheet workSheet = package.Workbook.Worksheets[0];
                    int rowCount = workSheet.Dimension.Rows;
                    int colCount = workSheet.Dimension.Columns;

                    for (int i = 0; i < workSheet.Dimension.Columns; i += 4)
                    {
                        var name = workSheet.Cells[6, i + 2].Value?.ToString();
                        if (string.IsNullOrEmpty(name)) continue;

                        var profile = new PowerProfile(name, new List<PowerProfileData>());

                        for (int row = 9; row <= workSheet.Dimension.End.Row - 1; row++)
                        {
                            if (IsRowEmpty(workSheet, row, i + 1))
                                break;

                            profile.PowerData.Add(new PowerProfileData
                            {
                                DateAndTime = DateTime.Parse(workSheet.Cells[row, 1].Value.ToString()),
                                APlus = GetValue(workSheet, row, i + 2) * 1000,
                                AMinus = GetValue(workSheet, row, i + 3) * 1000,
                                RPlus = GetValue(workSheet, row, i + 4) * 1000,
                                RMinus = GetValue(workSheet, row, i + 5) * 1000
                            });
                        }
                        PowerProfile.Add(profile);
                    }
                }
                catch
                {
                    MessageBox.Show("Указана таблица с неверной структурой", "Ошибка");
                    return null;
                }
            }
            return PowerProfile;
        }
Leave a Comment