c# 读取word表格内容并计数

在这里插入图片描述

 object oMissing = System.Reflection.Missing.Value;

            object format = MSWord.WdSaveFormat.wdFormatDocument;// office 2007就是wdFormatDocumentDefault
            xxx.wordDoc = xxx.app.Documents.Open(ref xxx.c, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref 

oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);            //打开path指定的word文档
            xxx.app.Visible = true;                            //设置文档为可见
            //————————————————
            //版权声明:本文为CSDN博主「亨利先生」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
            //原文链接:https://blog.csdn.net/weixin_40212554/article/details/83796199
            int ip = xxx.wordDoc.Tables.Count;//获取表格数
            Microsoft.Office.Interop.Word.Table nowTable = xxx.wordDoc.Tables[ip];//获取表格中的行数
            //int ip = xxx.wordDoc.Tables.Count;
            int ip6 = nowTable.Rows.Count;//获取行数
            // DataSet ds = new DataSet();

            //ds = xxx.wordDoc.Tables.;
            // MessageBox.Show(Convert.ToString(ip6));
           // DateTime.Now.ToString();            // 2008-9-4 20:02:10
            //string da = DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd");        // 2008-9-4 20:12:12
            int i6 = 1;//从第一行开始读取
            //xxx.i = ip6 + 1;
            //  MSWord.Document wordDoc;

            // xxx.app.Quit(ref oMissing, ref oMissing, ref oMissing);
            int jici3 = 0;//获取有几个三小时
            int jici6 = 0;//获取有几个六小时
            for (int jitian=1; jitian < ip6;jitian++ ) {
    
    //从第一行开始读取  每次读完i6加1
                i6 = i6 + 1;
            string text = nowTable.Cell(i6, 6).Range.Text.ToString();//读表格的第i6行 第6列
            
            string c = text.Substring(0, 1);//读取的内容从头开始数的第一个字
            int duoshao = Convert.ToInt32(c);//将小时数转换为int
            label2.Text = c;
            string cc = label2.Text;
            if (duoshao == 3) {
    
     //判断有几个3小时
                jici3 = jici3 + 1; //给3小时加1 方便最后结算
            }
            else{
    
    
                jici6 = jici6 + 1;
            }
            MessageBox.Show(c);
        } MessageBox.Show("三小时:"+Convert.ToString(jici3)+"次"+","+"十二小时"+Convert.ToString(jici6)+"次");

猜你喜欢

转载自blog.csdn.net/weixin_40938312/article/details/102626741