Exemplo n.º 1
0
        /**
         * Gets a TimeSeries from the data section of a mutable Sheet. Data follows
         * a data section header and appears in the first six columns to the end of
         * the file. Empty cells in the data are forbidden.
         *
         * @param sheet mutable Sheet
         * @return TimeSeries of the data
         * @throws DataFormatException if getData throws DataFormatException or if
         *             the data contains empty cells
         */
        private static ITimeSeries GetSeries(ISheet sheet)
        {
            ITimeSeries       series       = new BaseTimeSeries();
            IFormulaEvaluator evaluator    = sheet.Workbook.GetCreationHelper().CreateFormulaEvaluator();
            TimeSpan          weekDuration = new TimeSpan(7, 0, 0, 0);
            List <IRow>       rows         = GetData(sheet);

            // parse the rows from the data section
            foreach (IRow row in rows)
            {
                CellValue[] cellValues = new CellValue[6];
                for (int i = 0; i < 6; i++)
                {
                    // empty cells in the data section are forbidden
                    if (row.GetCell(i) == null)
                    {
                        throw new FormatException("empty cell in xls time series data");
                    }
                    cellValues[i] = evaluator.Evaluate(row.GetCell(i));
                }
                // build a bar from the row and add it to the series
                DateTime weekEndDate = DateUtil.GetJavaDate(cellValues[0].NumberValue);
                IBar     bar         = new BaseBar(weekDuration, weekEndDate,
                                                   // open, high, low, close, volume
                                                   cellValues[1].FormatAsString().ToDecimal(),
                                                   cellValues[2].FormatAsString().ToDecimal(),
                                                   cellValues[3].FormatAsString().ToDecimal(),
                                                   cellValues[4].FormatAsString().ToDecimal(),
                                                   cellValues[5].FormatAsString().ToDecimal());
                series.AddBar(bar);
            }
            return(series);
        }