Esempio n. 1
0
        /// <summary>
        /// 非表格形式的航路点
        /// </summary>
        /// <returns></returns>
        public string InitBlockPoint()
        {
            if (null == dicBlockColName)
            {
                dicBlockColName = new Dictionary <int, string>();
            }
            TextChunk     lastChunk = null;
            int           row       = 1;
            int           col       = 1;
            StringBuilder msg       = new StringBuilder();
            AirportPoint  point     = new AirportPoint();

            try
            {
                StringBuilder blockText = new StringBuilder();
                bool          isLast    = false;
                foreach (var chunk in Chunks)
                {
                    if (lastChunk == null)
                    {
                        lastChunk = chunk;
                        col       = 1;
                        continue;
                    }

                    blockText.Append(lastChunk.Text);

                    //最后一块检测不到结尾,特殊处理
                    if (Chunks.IndexOf(chunk) == Chunks.Count - 1)
                    {
                        isLast = true;
                        blockText.Append(chunk.Text);
                    }
                    if (IsChunkAtWordBoundary(chunk, lastChunk) || isLast)
                    {
                        string preColText = blockText.ToString().Trim();
                        blockText = new StringBuilder();
                        col++;

                        if (!string.IsNullOrEmpty(preColText))
                        {
                            bool ret = SetPointColName(col - 1, preColText, ref dicBlockColName);
                            //若为标题行,则不进行后续处理
                            if (!ret)
                            {
                                string fieldName = string.Empty;
                                if (dicBlockColName.Keys.Contains(col - 1))
                                {
                                    fieldName = dicBlockColName[col - 1];

                                    SetModeFieldVal(point, fieldName, preColText);

                                    if (!string.IsNullOrEmpty(point.PointNo) &&
                                        !string.IsNullOrEmpty(point.Lat) &&
                                        !string.IsNullOrEmpty(point.Long))
                                    {
                                        //转化经纬度
                                        string err = point.ConvertLatLong();
                                        if (string.IsNullOrEmpty(err))
                                        {
                                            Points.Add(point);
                                            point = new AirportPoint();
                                        }
                                        else
                                        {
                                            msg.AppendLine(string.Format("航路点(编号:{0} 纬度:{1} 经度:{2})经纬度解析失败。", point.PointNo, point.Long, point.Lat));
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            col = 1;
                        }
                    }
                    //判断是否换行
                    if (!chunk.SameLine(lastChunk))
                    {
                        row++;
                        col       = 1;
                        blockText = new StringBuilder();
                        point     = new AirportPoint();
                    }

                    lastChunk = chunk;
                }
            }
            catch (Exception ex)
            {
                msg.AppendLine(string.Format("解析出现异常:{0}", ex.Message));
            }

            return(msg.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// 初始化航路点数据
        /// </summary>
        /// <returns></returns>
        public string InitTablePoint()
        {
            TextChunk     lastChunk = null;
            int           row       = 1;
            int           col       = 0;
            StringBuilder msg       = new StringBuilder();

            StringBuilder            lastColText = new StringBuilder();
            Dictionary <int, string> colName     = new Dictionary <int, string>();
            AirportPoint             point       = new AirportPoint();
            //跳过的行
            int contiRow = 0;

            try
            {
                foreach (var chunk in Chunks)
                {
                    if (lastChunk == null)
                    {
                        col       = 1;
                        lastChunk = chunk;
                    }

                    //判断是否换行
                    if (!chunk.SameLine(lastChunk))
                    {
                        row++;
                        col = 1;

                        //换行时,如果有上次读取的字符串为匹配,则改行数据匹配错误
                        if (!string.IsNullOrEmpty(lastColText.ToString()))
                        {
                            msg.AppendLine(string.Format("第{0}行,解析出错,内容:{1}", row, lastColText.ToString()));
                        }
                    }

                    if (row == contiRow)
                    {
                        continue;
                    }

                    string val = chunk.Text;

                    //判断是否为需要跳过的行
                    if (IsContiRow(val))
                    {
                        contiRow = row;
                        continue;
                    }

                    //对内容预处理
                    string preColText = string.Empty;
                    string fieldName  = string.Empty;
                    if (colName.Keys.Contains(col))
                    {
                        fieldName = colName[col];
                    }
                    preColText = PreDisposeText(ref lastColText, ref val, fieldName);

                    //如果遇到空格则认为进入下一列
                    if (val.Contains(" "))
                    {
                        col++;

                        if (!string.IsNullOrEmpty(preColText))
                        {
                            //根据列名,设置对应的字段名
                            bool ret = SetPointColName(col - 1, preColText, ref colName);
                            //若为标题行,则不进行后续处理
                            if (!ret)
                            {
                                if (!string.IsNullOrEmpty(fieldName))
                                {
                                    SetModeFieldVal <AirportPoint>(point, fieldName, preColText);

                                    if (!string.IsNullOrEmpty(point.PointNo) &&
                                        !string.IsNullOrEmpty(point.LatLong))
                                    {
                                        Points.Add(point);
                                        point = new AirportPoint();
                                    }
                                }
                            }
                        }
                        else
                        {
                            //航路点中间列不存在空列,如果遇到空列变从1重新开始
                            col = 1;
                        }
                    }
                    else
                    {
                        //未遇到空格前不能确认单元格内容是否已读取完全,先保存内容
                        lastColText.Append(val);
                    }

                    lastChunk = chunk;
                }
            }
            catch (Exception ex)
            {
                msg.AppendLine(string.Format("解析出现异常:{0}", ex.Message));
                Console.Write(ex.Message);
            }

            return(msg.ToString());
        }