Ejemplo n.º 1
0
        public static List <PowerConsumptionRecord> ReadPowerValuesFromXLS()
        {
            const string TRIM_CURVE_FILE_NAME = @"C:\GreenFLOAT\Ship1\TrimCurveSample.xlsx";

            Excel.Application xlApp;
            Excel.Workbook    xlWorkbook;
            Excel.Worksheet   xlWorksheet;
            GetExcelReferences(TRIM_CURVE_FILE_NAME, out xlApp, out xlWorkbook, out xlWorksheet);

            var       range            = xlWorksheet.UsedRange;
            const int HEADER_ROW_INDEX = 2;
            const int SPEED_CELL_START = 3;
            const int DRAFT_INDEX      = 1;
            const int TRIM_INDEX       = 2;
            var       speeds           = new List <int>();
            int       speedIndex       = SPEED_CELL_START;

            while (true)
            {
                var speedCell = range.Cells[HEADER_ROW_INDEX, speedIndex++] as Excel.Range;
                if (speedCell.Value2 == null)
                {
                    break;
                }
                var value = (int)(speedCell).Value2;
                speeds.Add(value);
            }

            var powerRecords = new List <PowerConsumptionRecord>();

            for (int rCnt = HEADER_ROW_INDEX + 1; rCnt <= range.Rows.Count; rCnt++)
            {
                double draft = (double)(range.Cells[rCnt, DRAFT_INDEX] as Excel.Range).Value2;
                double trim  = (double)(range.Cells[rCnt, TRIM_INDEX] as Excel.Range).Value2;

                for (int i = 0; i < speeds.Count; i++)
                {
                    var curUsageCell = SPEED_CELL_START + i;
                    var powerUsage   = (double)(range.Cells[rCnt, curUsageCell] as Excel.Range).Value2;
                    var powerSavings = (double)(range.Cells[rCnt, curUsageCell + speeds.Count + 1] as Excel.Range).Value2 * 100;
                    var rec          = new PowerConsumptionRecord(draft, speeds[i], trim, powerUsage, powerSavings);
                    powerRecords.Add(rec);
                }
            }

            CloseAndReleaseExcelObjects(xlApp, xlWorkbook, xlWorksheet);
            return(powerRecords);
        }
        public static List<PowerConsumptionRecord> ReadPowerValuesFromXLS()
        {
            const string TRIM_CURVE_FILE_NAME = @"C:\GreenFLOAT\Ship1\TrimCurveSample.xlsx";
            Excel.Application xlApp;
            Excel.Workbook xlWorkbook;
            Excel.Worksheet xlWorksheet;
            GetExcelReferences(TRIM_CURVE_FILE_NAME, out xlApp, out xlWorkbook, out xlWorksheet);

            var range = xlWorksheet.UsedRange;
            const int HEADER_ROW_INDEX = 2;
            const int SPEED_CELL_START = 3;
            const int DRAFT_INDEX = 1;
            const int TRIM_INDEX = 2;
            var speeds = new List<int>();
            int speedIndex = SPEED_CELL_START;
            while (true) {
                var speedCell = range.Cells[HEADER_ROW_INDEX, speedIndex++] as Excel.Range;
                if (speedCell.Value2 == null)
                    break;
                var value = (int)(speedCell).Value2;
                speeds.Add(value);
            }

            var powerRecords = new List<PowerConsumptionRecord>();
            for (int rCnt = HEADER_ROW_INDEX + 1; rCnt <= range.Rows.Count; rCnt++) {
                double draft = (double)(range.Cells[rCnt, DRAFT_INDEX] as Excel.Range).Value2;
                double trim = (double)(range.Cells[rCnt, TRIM_INDEX] as Excel.Range).Value2;

                for (int i = 0; i < speeds.Count; i++) {
                    var curUsageCell = SPEED_CELL_START + i;
                    var powerUsage = (double)(range.Cells[rCnt, curUsageCell] as Excel.Range).Value2;
                    var powerSavings = (double)(range.Cells[rCnt, curUsageCell + speeds.Count + 1] as Excel.Range).Value2 * 100;
                    var rec = new PowerConsumptionRecord(draft, speeds[i], trim, powerUsage, powerSavings);
                    powerRecords.Add(rec);
                }
            }

            CloseAndReleaseExcelObjects(xlApp, xlWorkbook, xlWorksheet);
            return powerRecords;
        }