Ich bin sehr neu bei C# und habe einen ziemlich klobigen Code geschrieben. Ich habe viele Kurse online gemacht und viele sagen, dass es mehrere Möglichkeiten gibt, Probleme anzugehen. Jetzt habe ich ein Programm erstellt, das eine .Doc Word-Datei lädt und dann mit if-Anweisungen nach den relevanten Informationen sucht.Wie beschleunigt man Word-Interop-Verarbeitung?
Jetzt ist mein Problem mit meiner Lösung, dass dieses Programm für immer dauert !!! Ich spreche von 30 Minuten - 1 Stunde, um den folgenden Code zu vervollständigen.
Irgendwelche Ideen, wie ich mein kleines Programm etwas weniger klobig machen kann? Ich hoffe, dass die Lösungen dazu mein Wissen wesentlich erhöhen werden, also danke im Voraus jeder!
Grüße chris
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]);
}
}
}
}
Das ist, was passiert, wenn man offen docs von Aliens ... (_ "Open (" c: \\ Users \ \ Alien \\ Desktop \\ TESTJOBS.doc "_) –
haha sie strahlten sie einfach auf mich runter! –
Haben Sie Timing-Diagnose (Stoppuhr-Klasse) in den Code zu sehen, was die Zeit nimmt? – ChrisF