C#读写文本文件并导入Excel(二)


一、编程思想

   跟上一篇类似的思想,只是这次多利用了C#中几个好用的字符串函数string.indexof()string.substring()

二、程序运行后导入Excel

  即把用逗号分列好的目标文本.txt导入Excel文件中,我并没有直接对Excel进行读写,这点读者必须注意。达到了我们编程的目的就可以了。截图如下:

 

三、主要代码

  这次的代码比较多,因为涉及几个题型,有单选题和多选题,这里为了简化,只贴出读取单选题的函数代码。重复的代码也比较多的,因为处理的步骤都类似。

static void processTextSingle(string text)

        {

            int questionNum = scanQuestionNum(text);

 

            string targetText = "题目,选项一,选项二,选项三,选项四,答案\n";

 

            string toFindTextSingle = "单项选择题";

            string toFindTextMulti = "多项选择题";

 

            string tempText = text.Substring(0, text.Length);

 

            string[] tempQText = new string[questionNum];

            string[] tempAText = new string[questionNum];

            string[] tempBText = new string[questionNum];

            string[] tempCText = new string[questionNum];

            string[] tempDText = new string[questionNum];

            string[] tempAnswer = new string[questionNum];

 

            int indexQ = 0;

            int indexA = 0;

            int indexB = 0;

            int indexC = 0;

            int indexD = 0;

            int indexNextQ = 0;

 

            int indexAnswerRegion = 0;

            int indexAnswer = 0;

 

            int currentLength = tempText.Length;

 

            for (int i = 1, j = 0; i <= questionNum; i++, j++)

            {

 

                indexQ = tempText.IndexOf((i.ToString() + ".")) + (i.ToString()).Length + 1;

                indexA = tempText.IndexOf("A");

                indexB = tempText.IndexOf("B");

                indexC = tempText.IndexOf("C");

                indexD = tempText.IndexOf("D");

 

                if (i < questionNum)

                {

                    indexNextQ = tempText.IndexOf(((i + 1).ToString() + "."));

                }

                else

                {

                    indexNextQ = tempText.IndexOf("二、多项选择题");

                }

 

 

                tempQText[j] = (tempText.Substring(indexQ, (indexA - indexQ))).Trim();

                tempAText[j] = (tempText.Substring((indexA + 1), (indexB - indexA - 1))).Trim();

                tempBText[j] = (tempText.Substring((indexB + 1), (indexC - indexB - 1))).Trim();

                tempCText[j] = (tempText.Substring((indexC + 1), (indexD - indexC - 1))).Trim();

                tempDText[j] = (tempText.Substring((indexD + 1), (indexNextQ - indexD - 1))).Trim();

 

                Console.WriteLine(tempQText[j]);

                Console.WriteLine(tempAText[j]);

                Console.WriteLine(tempBText[j]);

                Console.WriteLine(tempCText[j]);

                Console.WriteLine(tempDText[j]);

 

                tempText = tempText.Substring(indexNextQ, currentLength - indexNextQ);

                currentLength = tempText.Length;

            }

 

            indexAnswerRegion = text.IndexOf("参考答案");

            tempText = text.Substring(indexAnswerRegion, text.Length - indexAnswerRegion);

 

            int left, right = 0;

            left = tempText.IndexOf("一、单项选择题");

            right = tempText.IndexOf("二、多项选择题");

 

            tempText = tempText.Substring(left, right - left);

 

            Console.WriteLine(tempText);

 

            for (int i = 1, j = 0; i <= questionNum; i++, j++)

            {

                indexAnswer = tempText.IndexOf((i.ToString() + ".")) + (i.ToString()).Length + 1;

                tempAnswer[j] = tempText.Substring(indexAnswer, 1);

 

                Console.WriteLine(tempAnswer[j]);

            }

 

            for (int i = 0; i < questionNum; i++)

            {

                targetText = targetText + tempQText[i] + ","

                                     + tempAText[i] + ","

                                     + tempBText[i] + ","

                                     + tempCText[i] + ","

                                     + tempDText[i] + ","

                                     + tempAnswer[i] + "\n";

            }

            Console.WriteLine(targetText);

            //System.IO.File.WriteAllText(@"E:\目标文本.txt", targetText);

        }

 

还有一个扫描单选题题目数量的函数,如下:

static int scanQuestionNum(string str)

        {

            int leftIndex = str.IndexOf("一、单项选择题");

            int rightIndex = str.IndexOf("二、多项选择题");

            str = str.Substring(leftIndex, (rightIndex - leftIndex));

 

            int index = 1;

            while(str.IndexOf((index.ToString()+ "."))!=-1)

            {

                index++;

            }

 

            index = index - 1;

 

            Console.WriteLine("一共有" + index + "道单项选择题");

 

            return index;

        }

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的狐狸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值