コード例 #1
0
ファイル: Excel.cs プロジェクト: askoric/ExcelParser
        public Excel <T, TCtype> ReadExcell(string filePath, IValueParser valueParser)
        {
            var excel = new Excel <T, TCtype>();

            FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);

            IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

            excelReader.IsFirstRowAsColumnNames = true;

            while (excelReader.Read())
            {
                bool isHeader = !excel.Header.Any();
                if (isHeader)
                {
                    bool haveColumns = true;
                    int  columnIndex = 0;
                    while (haveColumns)
                    {
                        try {
                            string columnName = excelReader.GetString(columnIndex);

                            var column = new T().GetColumn(columnName, columnIndex);

                            if (column.IsRecognizableColumn())
                            {
                                excel.Header.Add(column);
                            }

                            columnIndex++;
                        }
                        catch (IndexOutOfRangeException exc) {
                            haveColumns = false;
                        }
                    }
                }
                else
                {
                    var rowValues = new List <IExcelColumn <TCtype> >();
                    foreach (var headerColumn in excel.Header)
                    {
                        string rowValue = valueParser.ParseValue(excelReader.GetString(headerColumn.ColumnIndex));
                        rowValues.Add(new T
                        {
                            Value       = rowValue,
                            Type        = headerColumn.Type,
                            ColumnIndex = headerColumn.ColumnIndex
                        });
                    }

                    excel.Rows.Add(rowValues);
                }
            }

            //6. Free resources (IExcelDataReader is IDisposable)
            excelReader.Close();

            return(excel);
        }
コード例 #2
0
 private object ParseValue(ERRuleToken rruleToken, string payload)
 {
     return(_valueParser.ParseValue(rruleToken, payload));
 }