我很新C#,写的代码相对比较笨拙。我在线学了很多课程,其中许多都说有几种方法来解决问题。现在我编写了一个程序,可以加载一个 .Doc Word 文件,并使用 if 语句查找相关信息。
现在我的问题是,这个程序需要很长时间才能完成,大约需要30分钟到1小时的时间才能完成下面的代码。
有什么好的想法可以使我的小程序不那么笨重吗?我希望这些解决方案可以极大地增加我的知识,所以提前感谢大家!
问候 克里斯
现在我的问题是,这个程序需要很长时间才能完成,大约需要30分钟到1小时的时间才能完成下面的代码。
有什么好的想法可以使我的小程序不那么笨重吗?我希望这些解决方案可以极大地增加我的知识,所以提前感谢大家!
问候 克里斯
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int id = 0;
public int[] iD = new int[100];
public string[] timeOn = new string[100];
public string[] timeOff = new string[100];
public string[] dutyNo = new string[100];
public string[] day = new string[100];
private void button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document document = application.Documents.Open("c:\\Users\\Alien\\Desktop\\TESTJOBS.doc");
//the following for will loop for all words
int count = document.Words.Count;
for (int i = 1; i <= count; i++)
{
// the following if statement will look for the first word that is On
// this is then (on the file) proceded by 04:00 (thus i+2/3/4 respectively)
if (document.Words[i].Text == "On")
{
iD[id] = id;
// Console.WriteLine("ID Number ={0}", iD[id]);
dutyNo[id] = document.Words[i - 14].Text;
// Console.WriteLine("duty No set to:{0}", dutyNo[id]);
timeOn[id] = document.Words[i + 2].Text + document.Words[i + 3].Text + document.Words[i + 4].Text;
// Console.WriteLine("on time set to:{0}", timeOn[id]);
// the following if (runs if the last word was not "On" and then searches for the word "Off" which procedes "On" in the file format)
// this is then (on the file) proceded by 04:00 (thus i+2/3/4 respectively)
}
else if (document.Words[i].Text == "Off")
{
timeOff[id] = document.Words[i + 2].Text + document.Words[i + 3].Text + document.Words[i + 4].Text;
//Console.WriteLine("off time set to:{0}", timeOff[id]);
// the following if (runs if the last word was not "Off" and then searches for the word "Duty" which procedes "Off" in the file format)
// this is then (on the file) proceded by 04:00 (thus i+2/3/4 respectively)
}
else if (document.Words[i].Text == "Days" && !(document.Words[i + 3].Text == "Type"))
{
day[id] = document.Words[i + 2].Text;
//Console.WriteLine("day set to:{0}", day[id]);
//we then print the whole new duty out to ListBox1
listBox1.Items.Add(string.Format("new duty ID:{0} Time on:{1} Time off:{2} Duty No:{3} Day:{4}", iD[id], timeOn[id], timeOff[id], dutyNo[id], day[id]));
id++;
}
}
for (int i = 1; i <= 99; i++)
{
Console.WriteLine("new duty ID:{0} Time on:{1} Time off:{2} Duty No:{3} Day:{4}", iD[id], timeOn[id], timeOff[id], dutyNo[id], day[id]);
}
}
}
}