美文网首页
万里晴空

万里晴空

作者: 宇宙不大人心不小 | 来源:发表于2017-04-29 09:55 被阅读0次

    很早就开始打算写博客了,无奈于学习的东西不多,深度不够,再加上自己内向的性格。始终不敢动笔,最近读掘金,开发者头条的时间较多,突发奇想,或许可以用简书来写作,一周一次,一个小时左右的时间,也差不多。于是,就开始了第一次

    这周的话,最值得我高兴的就是自己写的一个统计信息员交表的NPOI程序,索引器的定义和反射的实现使得代码十分优雅(自夸一下),对信息条数的处理也真是cool。代码统一放在最后。
    我对于自己的摇摆已经到了无语的地步:

    • ArcGIS Engine
    • Android
    • HTML5
    • Nodejs
    • 编译原理

    当然只是随便看看啦,无聊的时候谢谢代码放松放松就行了。太散了,导致无法集中精力,导致无法短期内看到效果,所以呢,零件,你就看着办吧。

    // 一种泛型委托  Converter
    var names = records.ConvertAll(record => record.Name);
    
    // 这个索引器真棒
    /// <summary>
    /// 定义索引器
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public object this[RecordspropertyEnum index]
    {
        get { return  this.GetType().GetProperty(index.ToString()).GetValue(this,null); }
    
        set { this.GetType().GetProperty(index.ToString()).SetValue(this, value, null); }
    }
    
    /* 对信息进行计数 */
    for (int i = columnStartIndex; i <= columnEndIndex; i++)
    {
        curInfo = curRow.GetCell(i) != null ? curRow.GetCell(i).StringCellValue : "";
        curWordsCounts = curInfo.Length;
        curItemsCounts = GetInfoNum(curInfo);
        countType = (RecordspropertyEnum)(i - 5);
        curCounts = (int)curRecord[countType] + curItemsCounts;
    
        curRecord[countType] = curCounts;
        curRecord.WordsCounts += curWordsCounts;
    }
    
    // 比较好的一句
    curInfo = curRow.GetCell(i) != null ? curRow.GetCell(i).StringCellValue : "";
    
    // 妙在对信息条数的处理,也是源于上次Node去除空白行的结果,(split -> 去除空数组)
    // 没有去除空格(Trim)
    // 当前string的信息条数
    int GetInfoNum(string info)
    {
        int num = 0;
        if (info.Length <= 6)
            num = 0;
        else
        {
            // 替换尾随\r\n Split()之后为 \r 后也存在的错误
            info = info.Replace("\r\n", "@");
    
            var infoList = new List<string>(info.Split('@'));
            for (int i = infoList.Count; i > 0; i--)
            {
                if (infoList[i - 1].Length < 8)
                    infoList.RemoveAt(i - 1);
            }
            num = infoList.Count;
        }
    
        return num;
    }
    

    相关文章

      网友评论

          本文标题:万里晴空

          本文链接:https://www.haomeiwen.com/subject/rrlzzttx.html