It took me a while to just find a way to select data from my database and then paste it in an excel spreadsheet. Now that I've found it, it runs dirt slow. Like I said before, I've looked at a lot of different ways to accomplish this but have not been able to correctly implement any of them except for this. I'm not married to this option but it is the only one I could get to work. Could someone help me out by suggesting a quicker way to accomplish this simple task? Please see my code below.
Record850 rec850 = new Record850();
List<Record850> lst850records = new List<Record850>();
//SqlConnection connStr = new SqlConnection("Server = 172.18.211.76; Database = Processstage; User Id = brendon.davies; Password = mypassword;");
SqlConnection conn = new SqlConnection("Server = 172.18.211.76; Database = Processstage; User Id = brendon.davies; Password = mypassword;");
//SqlConnection sqlConnection1 = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.CommandText = Select_850;
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
conn.Open();
Record850 reco850 = new Record850();
string strComplete = "";
reader = cmd.ExecuteReader();
while (reader.Read())
{
try
{
reco850.OrgName = reader.GetString(0);
reco850.WholeSalerAccountDivisionCode = reader.GetString(1);
reco850.File_Process_Name = reader.GetString(2);
reco850.Pur_Ord_Num_BEG03 = reader.GetString(3);
reco850.File_Process_ID = reader.GetInt64(4);
reco850.CECode = reader.GetString(5);
reco850.CEName = reader.GetString(6);
reco850.Modified_Date = reader.GetDateTime(7);
lst850records.Add(reco850);
reco850 = new Record850();
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}
conn.Close();
eWorkSheet = (Excel.Worksheet)oSheets.get_Item("850_Template");
eWorkSheet.Activate();
int int850counter = 0;
int int850RowCounter = 3;
foreach (Record850 r850 in lst850records)
{
strComplete = lst850records[int850counter].OrgName + "\t" +
lst850records[int850counter].WholeSalerAccountDivisionCode + "\t" +
lst850records[int850counter].File_Process_Name + "\t" +
lst850records[int850counter].Pur_Ord_Num_BEG03 + "\t" +
lst850records[int850counter].File_Process_ID + "\t" +
lst850records[int850counter].CECode + "\t" +
lst850records[int850counter].CEName+ "\t" +
lst850records[int850counter].Modified_Date;
CR = (Excel.Range)eWorkSheet.Cells[int850RowCounter,3];
Clipboard.SetText(strComplete);
CR.Select();
eWorkSheet.Paste(CR, false);
Clipboard.Clear();
int850RowCounter++;
int850counter++;
strComplete = ""
}