예제 #1
0
파일: Logic.cs 프로젝트: nmkooxx/ExcelMaker
    private bool ReadExcel(string filePath, char type, Action headAction, Action <char, IRow> rowAction)
    {
        using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
            XSSFWorkbook workbook = new XSSFWorkbook(fs);
            //只读取第一张表
            ISheet sheet = workbook.GetSheetAt(0);
            if (sheet == null)
            {
                Debug.LogError("ReadExcel Error Sheet:" + filePath);
                return(false);
            }
            m_sheetName = sheet.SheetName;

            //第一行为表头,必定是最大列数
            IRow nameRow   = sheet.GetRow(0);
            int  cellCount = nameRow.LastCellNum;
            //表头
            IRow headRow = sheet.GetRow(1);
            //导出类型
            IRow typeRow = sheet.GetRow(2);
            //导出服务器客户端
            IRow exportRow = sheet.GetRow(3);

            ICell cell = exportRow.GetCell(0);
            if (cell == null || cell.StringCellValue == null)
            {
                Debug.LogError("导出配置错误:" + filePath);
                return(false);
            }
            if (cell.StringCellValue != "A" && !cell.StringCellValue.Contains(type))
            {
                Debug.LogWarning("跳过导出:" + filePath);
                return(false);
            }

            //Debug.Log("导出:" + filePath);

            string[] exportSettings = new string[cellCount];
            for (int i = 0; i < cellCount; i++)
            {
                cell = exportRow.GetCell(i);
                if (cell == null)
                {
                    exportSettings[i] = string.Empty;
                    continue;
                }
                exportSettings[i] = cell.StringCellValue;
            }

            m_defineIndex = -1;
            m_rawTypes    = new string[cellCount];
            m_cellTypes   = new string[cellCount];
            //第一列为id,只支持int,string
            cell = typeRow.GetCell(0);
            string value = cell.StringCellValue;
            if (value[0] == CsvConfig.skipFlag)
            {
                m_rawTypes[0]  = value;
                m_cellTypes[0] = value.Substring(1);
            }
            else
            {
                m_rawTypes[0]  = CsvConfig.skipFlag + value;
                m_cellTypes[0] = value;
            }
            for (int i = 1; i < cellCount; i++)
            {
                cell = typeRow.GetCell(i);
                if (cell == null)
                {
                    continue;
                }
                value         = cell.StringCellValue;
                m_rawTypes[i] = value;
                int pos = value.LastIndexOf(CsvConfig.classSeparator);
                if (pos > 0)
                {
                    m_cellTypes[i] = value.Substring(pos + 1);
                }
                else
                {
                    m_cellTypes[i] = value;
                }
            }

            m_headers = new CsvHeader[cellCount];
            for (int i = 0; i < cellCount; i++)
            {
                if (string.IsNullOrEmpty(exportSettings[i]))
                {
                    m_headers[i] = CsvHeader.Pop(string.Empty);
                    continue;
                }
                if (exportSettings[i] != "A" && !exportSettings[i].Contains(type))
                {
                    m_headers[i] = CsvHeader.Pop(string.Empty);
                    continue;
                }

                cell = headRow.GetCell(i);
                if (cell == null)
                {
                    m_headers[i] = CsvHeader.Pop(string.Empty);
                    continue;
                }

                var cellType = m_cellTypes[i];
                if (cellType == "define")
                {
                    m_defineIndex = i;
                    m_defineName  = cell.StringCellValue;
                    m_headers[i]  = CsvHeader.Pop(string.Empty);
                    continue;
                }

                m_headers[i] = CsvHeader.Pop(cell.StringCellValue);
                if (m_headers[i].type == eFieldType.Array && cellType.Length > 2 &&
                    cellType.EndsWith("[]", StringComparison.OrdinalIgnoreCase) &&
                    m_headers[i].name == m_headers[i].baseName)
                {
                    //去除结尾的[]
                    m_cellTypes[i] = cellType.Substring(0, cellType.Length - 2);
                }
            }
            int            slot;
            List <string>  nodeSlots = new List <string>(1);
            List <CsvNode> nodes     = new List <CsvNode>(1);
            for (int i = 0; i < m_headers.Length; i++)
            {
                CsvHeader header = m_headers[i];
                if (header.type == eFieldType.Primitive)
                {
                    header.SetSlot(-1);
                    continue;
                }
                slot = nodeSlots.IndexOf(header.baseName);
                if (slot < 0)
                {
                    nodeSlots.Add(header.baseName);
                    header.SetSlot(nodeSlots.Count - 1);
                    var node     = CsvNode.Pop(header.baseName, header.type);
                    var subTypes = m_rawTypes[i].Split(CsvConfig.arrayChars, CsvConfig.classSeparator);
                    node.cellType = subTypes[0];
                    nodes.Add(node);
                }
                else
                {
                    header.SetSlot(slot);
                }
            }
            if (nodeSlots.Count > 0)
            {
                m_nodes = nodes.ToArray();
            }
            else
            {
                m_nodes = new CsvNode[0];
            }

            headAction();

            m_rawIds.Clear();
            int startIndex = 4;// sheet.FirstRowNum;
            int lastIndex  = sheet.LastRowNum;
            for (int index = startIndex; index <= lastIndex; index++)
            {
                m_curIndex = index;
                IRow row = sheet.GetRow(index);
                if (row == null)
                {
                    continue;
                }

                //跳过id为空,或者#号开头的行
                cell = row.GetCell(0);
                if (cell == null)
                {
                    continue;
                }
                var obj = getCellValue(cell);
                if (obj == null)
                {
                    continue;
                }
                string id = obj.ToString();
                if (string.IsNullOrEmpty(id) || id[0] == CsvConfig.skipFlag)
                {
                    continue;
                }

                if (m_rawIds.IndexOf(id) >= 0)
                {
                    Debug.LogError(m_filePath + " 重复id, index:" + index + " id:" + id);
                    continue;
                }
                m_rawIds.Add(id);

                try {
                    rowAction(type, row);
                }
                catch (Exception e) {
                    Debug.LogError(m_filePath + " 转换错误, index:" + index + " info:" + obj + " \n" + e);
                }
            }
        }
        return(true);
    }