Пример #1
0
        public static IList <LocalizationItem> ImportFromXlsx(string fileName, out string errors)
        {
            int minRowCount;

            if (Properties.Settings.Default.ImportContainsHeader == true)
            {
                minRowCount = 2;
            }
            else
            {
                minRowCount = 1;
            }
            errors = null;
            FemtoXLSX.XlsxReader reader = new XlsxReader(fileName);
            try
            {
                reader.Read();
            }
            catch (Exception e)
            {
                errors = e.Message;
                return(null);
            }

            WorksheetReader worksheet = reader.GetWorksheet(0);

            if (worksheet == null)
            {
                errors = "The worksheet could not be loaded";
                return(null);
            }
            if (worksheet.HasColumn("G") == false)
            {
                errors = "Wrong number of columns. The worksheet must have at least 7 columns.";
                return(null);
            }

            int rowCount = worksheet.GetRowCount();

            if (rowCount < minRowCount)
            {
                if (minRowCount == 1)
                {
                    errors = "No data found. The worksheet must have at least 1 data row (no header expected).";
                }
                else
                {
                    errors = "No data found. The worksheet must have at least 2 rows (header + 1 data row).";
                }
                return(null);
            }
            Dictionary <string, Cell> data  = worksheet.Data;
            List <LocalizationItem>   items = new List <LocalizationItem>();
            LocalizationItem          item;
            List <Cell> row;

            int[] mandatory = new int[] { 0, 1, 2, 3, 4, 6 };
            for (int i = minRowCount - 1; i < rowCount; i++) // Start with row 2( index 1) or 1 (index 0)
            {
                row = worksheet.GetRow(i);
                if (worksheet.RowHasColumns(ref row, mandatory) == false)
                {
                    errors = "Row " + (i + 1).ToString() + " has too few values. At least 6 columns are expected.";
                    return(null);
                }
                item = new LocalizationItem();
                foreach (Cell cell in row)
                {
                    if (cell.ColumnNumber == 0)
                    {
                        item.StreamName = cell.GetStringValue();
                    }
                    else if (cell.ColumnNumber == 1)
                    {
                        item.ResourceKey = cell.GetStringValue();
                    }
                    else if (cell.ColumnNumber == 2)
                    {
                        item.ResourceCategory = cell.GetStringValue();
                    }
                    else if (cell.ColumnNumber == 3 && cell.Type == Cell.DataType.Boolean)
                    {
                        item.IsReadable = cell.GetBoolValue();
                    }
                    else if (cell.ColumnNumber == 4 && cell.Type == Cell.DataType.Boolean)
                    {
                        item.IsModifieable = cell.GetBoolValue();
                    }
                    else if (cell.ColumnNumber == 5)
                    {
                        item.Comment = cell.GetStringValue();
                    }
                    else if (cell.ColumnNumber == 6)
                    {
                        item.Content = cell.GetStringValue();
                    }
                    else if (cell.ColumnNumber < 7) // Error fall-back; higher numbers are ignored
                    {
                        errors = "Unexpected data occurred. Check the columns A to G in Row " + (i + 1).ToString();
                        return(null);
                    }
                }
                items.Add(item);
            }
            return(items);
        }