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

来源:互联网 发布:世界网络发展史 编辑:程序博客网 时间:2024/05/16 07:26


一、编程思想

   跟上一篇类似的思想,只是这次多利用了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;

        }

0 0