Beispiel #1
0
        public bool ReadFromWorkSheet(WorkSheet wsheet)
        {
            if (wsheet == null)
            {
                return(false);
            }
            name = wsheet.Name;

            string nameReg = @"^[A-Z]\w+$";

            if (string.IsNullOrEmpty(name) || !Regex.IsMatch(name, nameReg))
            {
                logMsg.Add(new ErrorLogData1(LogConst.E_DataSheet_NameFormat, name));
                return(false);
            }
            int rowCount    = wsheet.RowCount();
            int columnCount = wsheet.ColumnCount();

            if (rowCount < MIN_ROW_COUNT)
            {
                logMsg.Add(new ErrorLogData2(LogConst.E_DataSheet_RowNum, name, "" + MIN_ROW_COUNT));
                return(false);
            }
            if (columnCount < MIN_COLUMN_COUNT)
            {
                logMsg.Add(new ErrorLogData2(LogConst.E_DataSheet_ColNum, name, "" + MIN_COLUMN_COUNT));
                return(false);
            }

            if (ParseToField(wsheet))
            {
                if (fields.Count > 0)
                {
                    ParseToContent(wsheet);
                }
            }
            return(true);
        }
Beispiel #2
0
        private bool ParseToField(WorkSheet wsheet)
        {
            int rowCount = wsheet.RowCount();
            int colCount = wsheet.ColumnCount();

            DataFieldType preFieldType = DataFieldType.None;

            for (int i = 1; i < colCount; i++)
            {
                string fName       = wsheet.GetValue(0, i);
                string fTypeStr    = wsheet.GetValue(1, i);
                string fExportStr  = wsheet.GetValue(2, i);
                string fDesc       = wsheet.GetValue(3, i);
                string fDefault    = wsheet.GetValue(4, i);
                string fValidation = wsheet.GetValue(5, i);
                string fValidValue = wsheet.GetValue(6, i);

                if (string.IsNullOrEmpty(fName))
                {
                    if (preFieldType == DataFieldType.Dic || preFieldType == DataFieldType.Array)
                    {
                        continue;
                    }
                    else
                    {
                        logMsg.Add(new ErrorLogData1(LogConst.E_DataField_Empty, "" + i));
                        continue;
                    }
                }

                DataFieldType fType = DataHelper.GetFieldType(fTypeStr);
                if (fType == DataFieldType.None)
                {
                    logMsg.Add(new ErrorLogData2(LogConst.E_DataField_TypeNone, "" + i, string.IsNullOrEmpty(fTypeStr) ? "" : fTypeStr));
                    continue;
                }
                DataFieldExport fExport = DataHelper.GetFieldExport(fExportStr);
                if (fExport == DataFieldExport.None)
                {
                    logMsg.Add(new ErrorLogData1(LogConst.E_DataField_ExportNone, "" + i));
                    continue;
                }

                DataFieldInfo fInfo = new DataFieldInfo
                {
                    columnIndex     = i,
                    name            = fName,
                    type            = fType,
                    export          = fExport,
                    desc            = fDesc,
                    validation      = DataHelper.GetFieldValidationType(fValidation),
                    validationValue = fValidValue,
                    defaultContent  = fDefault
                };

                if (fType == DataFieldType.Dic)
                {
                    DataHelper.GetFieldDicKeyInfo(fTypeStr, out fInfo.keyField, out fInfo.valueField);
                    if (fInfo.keyField == null || fInfo.valueField == null)
                    {
                        logMsg.Add(new ErrorLogData1(LogConst.E_DataField_DicType, "" + i));
                        continue;
                    }
                    if (fInfo.keyField.type == DataFieldType.Ref)
                    {
                        if (string.IsNullOrEmpty(fInfo.keyField.refName))
                        {
                            logMsg.Add(new ErrorLogData1(LogConst.E_DataField_DicKeyRef, "" + i));
                            continue;
                        }
                    }
                    if (fInfo.valueField.type == DataFieldType.Ref)
                    {
                        if (string.IsNullOrEmpty(fInfo.valueField.refName))
                        {
                            logMsg.Add(new ErrorLogData1(LogConst.E_DataField_DicValueRef, "" + i));
                            continue;
                        }
                    }
                }
                else if (fType == DataFieldType.Array)
                {
                    DataHelper.GetArrayFieldInfo(fTypeStr, out fInfo.valueField);
                    if (fInfo.valueField == null)
                    {
                        logMsg.Add(new ErrorLogData1(LogConst.E_DataField_ArrayType, "" + i));
                        continue;
                    }
                    else if (fInfo.valueField.type == DataFieldType.Ref)
                    {
                        if (string.IsNullOrEmpty(fInfo.valueField.refName))
                        {
                            logMsg.Add(new ErrorLogData1(LogConst.E_DataField_ArrayValueRef, "" + i));
                            continue;
                        }
                    }
                }
                else if (fType == DataFieldType.Ref)
                {
                    fInfo.refName = DataHelper.GetFieldRefName(fTypeStr);
                    if (string.IsNullOrEmpty(fInfo.refName))
                    {
                        logMsg.Add(new ErrorLogData1(LogConst.E_DataField_RefRef, "" + i));
                        continue;
                    }
                }
                else if (fType == DataFieldType.Res)
                {
                    fInfo.refName = DataHelper.GetFieldRefName(fTypeStr);
                    if (string.IsNullOrEmpty(fInfo.refName))
                    {
                        logMsg.Add(new ErrorLogData1(LogConst.E_DataField_ResType, "" + i));
                        continue;
                    }
                }
                preFieldType = fType;
                if (!AddField(fInfo))
                {
                    fields.Clear();
                    return(false);
                }
            }

            return(true);
        }
Beispiel #3
0
        private bool ParseToContent(WorkSheet wSheet)
        {
            int rowCount = wSheet.RowCount();
            int colCount = wSheet.ColumnCount();
            int startRow = MIN_ROW_COUNT;

            for (int j = startRow; j < rowCount; j++)
            {
                DataLineInfo content = new DataLineInfo(fields.Count);
                for (int i = 0; i < fields.Count; i++)
                {
                    DataFieldInfo fInfo = fields[i];
                    string        value = wSheet.GetValue(j, fInfo.columnIndex);
                    if (fInfo.type == DataFieldType.Dic)
                    {
                        if (string.IsNullOrEmpty(value))
                        {
                            logMsg.Add(new ErrorLogData2(LogConst.E_DataContent_DicLen, "" + j, "" + i));
                            continue;
                        }
                        else
                        {
                            if (int.TryParse(value, out int count))
                            {
                                int maxLen = -1;
                                if (i == fields.Count - 1)
                                {
                                    maxLen = colCount - fInfo.columnIndex - 1;
                                }
                                else
                                {
                                    maxLen = fields[i + 1].columnIndex - fInfo.columnIndex - 1;
                                }
                                if (count * 2 > maxLen)
                                {
                                    logMsg.Add(new ErrorLogData2(LogConst.E_DataContent_DicLarge, "" + j, "" + i));
                                    continue;
                                }
                                else
                                {
                                    Dictionary <string, string> result = new Dictionary <string, string>();
                                    for (int m = 1; m <= count; m++)
                                    {
                                        string k = wSheet.GetValue(j, fInfo.columnIndex + 2 * (m - 1) + 1);
                                        string v = wSheet.GetValue(j, fInfo.columnIndex + 2 * m);
                                        if (result.ContainsKey(k))
                                        {
                                            logMsg.Add(new ErrorLogData2(LogConst.E_DataContent_DicLarge, "" + j, "" + i));
                                            result.Clear();
                                            break;
                                        }
                                        else
                                        {
                                            result.Add(k, v);
                                        }
                                    }
                                    content.AddCellData(i, j, fInfo.columnIndex, result);
                                }
                            }
                            else
                            {
                                logMsg.Add(new ErrorLogData2(LogConst.E_DataContent_DicLen, "" + j, "" + i));
                                continue;
                            }
                        }
                    }
                    else if (fInfo.type == DataFieldType.Array)
                    {
                        if (string.IsNullOrEmpty(value))
                        {
                            logMsg.Add(new ErrorLogData2(LogConst.E_DataContent_DicLen, "" + j, "" + i));
                            continue;
                        }
                        else
                        {
                            if (int.TryParse(value, out int count))
                            {
                                int maxLen = -1;
                                if (i == fields.Count - 1)
                                {
                                    maxLen = colCount - fInfo.columnIndex - 1;
                                }
                                else
                                {
                                    maxLen = fields[i + 1].columnIndex - fInfo.columnIndex - 1;
                                }
                                if (count > maxLen)
                                {
                                    logMsg.Add(new ErrorLogData2(LogConst.E_DataContent_ArrayLarge, "" + j, "" + i));
                                    continue;
                                }
                                else
                                {
                                    List <string> result = new List <string>();
                                    for (int m = 1; m <= count; m++)
                                    {
                                        result.Add(wSheet.GetValue(j, fInfo.columnIndex + m));
                                    }
                                    content.AddCellData(i, j, fInfo.columnIndex, result);
                                }
                            }
                            else
                            {
                                logMsg.Add(new ErrorLogData2(LogConst.E_DataContent_DicLen, "" + j, "" + i));
                                continue;
                            }
                        }
                    }
                    else
                    {
                        string v = wSheet.GetValue(j, fInfo.columnIndex);
                        content.AddCellData(i, j, fInfo.columnIndex, v);
                    }
                }
                contents.Add(content);
            }

            contents.Sort(SortDataLineByID);

            return(true);
        }
Beispiel #4
0
        public bool ReadWorkbook()
        {
            bool result = true;

            if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
            {
                logMsgMgr.Add(new ErrorLogData1(LogConst.E_WorkBook_NotFound, filePath == null?"":filePath));
                result = false;
            }
            else
            {
                string ext = Path.GetExtension(filePath);
                if (ext != ".xlsx" && ext != ".xls")
                {
                    logMsgMgr.Add(new ErrorLogData1(LogConst.E_WorkBook_NotExcel, filePath));
                    result = false;
                }
                else
                {
                    logMsgMgr.Add(new InfoLogData1(LogConst.I_WorkBook_Start, filePath));
                    logMsgMgr.Indent++;

                    FileStream fs = null;
                    try
                    {
                        fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                        IWorkbook workbook = null;
                        if (ext == ".xlsx")
                        {
                            workbook = new XSSFWorkbook(fs);
                        }
                        else
                        {
                            workbook = new HSSFWorkbook(fs);
                        }
                        if (workbook != null)
                        {
                            int sheetCount = workbook.NumberOfSheets;
                            if (sheetCount > 0)
                            {
                                for (int i = 0; i < sheetCount; i++)
                                {
                                    ISheet sheet = workbook.GetSheetAt(i);
                                    if (sheet == null)
                                    {
                                        logMsgMgr.Add(new ErrorLogData2(LogConst.E_WorkBook_SheetNull, filePath, "" + i));
                                    }
                                    else if (string.IsNullOrEmpty(sheet.SheetName))
                                    {
                                        logMsgMgr.Add(new ErrorLogData2(LogConst.E_WorkSheet_NameNull, filePath, "" + i));
                                    }
                                    else
                                    {
                                        WorkSheet wSheet = new WorkSheet();
                                        if (wSheet.ReadSheet(sheet))
                                        {
                                            sheets.Add(wSheet.Name, wSheet);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                logMsgMgr.Add(new WarningLogData1(LogConst.E_WorkBook_Empty, filePath));
                            }
                        }
                        else
                        {
                            logMsgMgr.Add(new ErrorLogData1(LogConst.E_WorkBook_Null, filePath));
                            result = false;
                        }
                    }
                    catch (Exception e)
                    {
                        logMsgMgr.Add(new ErrorLogData2(LogConst.E_WorkBook_Format, filePath, e.Message));
                        if (fs != null)
                        {
                            fs.Close();
                        }

                        result = false;
                    }

                    logMsgMgr.Indent--;
                    logMsgMgr.Add(new InfoLogData1(LogConst.I_WorkBook_End, filePath));
                    logMsgMgr.Add(new EmptyLogData());
                }
            }
            return(result);
        }