2017-06-22 3 views
-4

Hier ist mein Code:Eine nicht behandelte Ausnahme des Typs ‚System.Runtime.InteropServices.COMException‘ aufgetreten

private void buttonexport_Click(object sender, EventArgs e) 
{ 

    string data; 
    string format; 
    int i, j = 0; 

    DateTime dt = DateTime.Now; 
    string fileName = "Audit_Reports_" + string.Format("{0:MM-dd-yyyy}", dt); 
    string path = @"Y:\Audit Reports\"; 
    if (fileName.Length >= 1) 
    { 
     MyConn.Close(); 
     DialogResult result = MessageBox.Show("Do you want to save to excel?", "Save", MessageBoxButtons.YesNo); 
     if (result == DialogResult.Yes) 
     { 
      Cursor.Current = Cursors.WaitCursor; 
      DataTable dbdatabase = new DataTable(); 
      DataTable dtContent = new DataTable(); 
      Excel.Application xlAppA; 
      Excel.Workbook xlWorkbookA; 
      Excel.Worksheet xlWorkSheetA; 
      object misValue = System.Reflection.Missing.Value; 

      dtContent.Columns.Add("Agent Name"); 
      dtContent.Columns.Add("Total Pts Avail"); 
      dtContent.Columns.Add("Total Pts Gained"); 
      dtContent.Columns.Add("Rating"); 
      dtContent.Columns.Add("Ticket ID"); 
      dtContent.Columns.Add("Order ID"); 
      dtContent.Columns.Add("Observer"); 
      dtContent.Columns.Add("Date"); 
      dtContent.Columns.Add("Issue Type"); 
      dtContent.Columns.Add("Rep Used an appropriate greeting"); 
      dtContent.Columns.Add("Rep Used the First Person Plural Voice throughout Correspondence"); 
      dtContent.Columns.Add("Rep Able to reply within SLA"); 
      dtContent.Columns.Add("Rep Adjusted to Customer’s Proficiency and Avoided Using Jargon"); 
      dtContent.Columns.Add("Rep Exhibited English Grammar Proficiency"); 
      dtContent.Columns.Add("Rep Maintained Professionalism"); 
      dtContent.Columns.Add("Rep Accessed Correct Customer Account and Made Accurate Record"); 
      dtContent.Columns.Add("Rep Gave Right Information and Ensured First-Contact Resolution"); 
      dtContent.Columns.Add("Rep Took the Proper E-Mail Template and Accurately Modified it"); 
      dtContent.Columns.Add("Rep Provided Self-Help Options and Descriptive Details"); 
      dtContent.Columns.Add("Rep Provided Closing Statement"); 
      dtContent.Columns.Add("Coaching Points/Comments"); 
      dtContent.Columns.Add("RED FLAG?"); 
      dtContent.Columns.Add("RED FLAG Description"); 
      dtContent.Columns.Add("RED FLAG Comments"); 
      dtContent.Columns.Add("WOW Points"); 
      dtContent.Columns.Add("WOW Comments"); 
      dtContent.Columns.Add("Source"); 
      dtContent.Columns.Add("Emp ID"); 


      DataRow forexport = dtContent.NewRow(); 


      forexport[0] = "Agent Name"; 
      forexport[1] = "Total Pts Avail"; 
      forexport[2] = "Total Pts Gained"; 
      forexport[3] = "Rating"; 
      forexport[4] = "Ticket ID"; 
      forexport[5] = "Order ID"; 
      forexport[6] = "Observer"; 
      forexport[7] = "Date"; 
      forexport[8] = "Issue Type"; 
      forexport[9] = "Rep Used an appropriate greeting"; 
      forexport[10] = "Rep Used the First Person Plural Voice throughout Correspondence"; 
      forexport[11] = "Rep Able to reply within SLA"; 
      forexport[12] = "Rep Adjusted to Customer’s Proficiency and Avoided Using Jargon"; 
      forexport[13] = "Rep Exhibited English Grammar Proficiency"; 
      forexport[14] = "Rep Maintained Professionalism"; 
      forexport[15] = "Rep Accessed Correct Customer Account and Made Accurate Record"; 
      forexport[16] = "Rep Gave Right Information and Ensured First-Contact Resolution"; 
      forexport[17] = "Rep Took the Proper E-Mail Template and Accurately Modified it"; 
      forexport[18] = "Rep Provided Self-Help Options and Descriptive Details"; 
      forexport[19] = "Rep Provided Closing Statement"; 
      forexport[20] = "Coaching Points/Comments"; 
      forexport[21] = "RED FLAG?"; 
      forexport[22] = "RED FLAG Description"; 
      forexport[23] = "RED FLAG Comments"; 
      forexport[24] = "WOW Points"; 
      forexport[25] = "WOW Comments"; 
      forexport[26] = "Source"; 
      forexport[27] = "Emp ID"; 

      dtContent.Rows.Add(forexport); 

      xlAppA = new Excel.Application(); 
      xlWorkbookA = xlAppA.Workbooks.Add(misValue); 
      xlWorkSheetA = (Excel.Worksheet)xlWorkbookA.Worksheets.get_Item(1); 

      MyConn.Open(); 
      MySqlCommand cmdSelectAccounts = new MySqlCommand("SELECT * FROM `database`.audit_rawdata;", this.MyConn); 
      MySqlDataAdapter myAdapter3 = new MySqlDataAdapter(); 
      cmdSelectAccounts.CommandTimeout = 0; 
      myAdapter3.SelectCommand = cmdSelectAccounts; 
      DataTable dtAccounts = new DataTable(); 
      myAdapter3.Fill(dtAccounts); 
      MyConn.Close(); 
      try 
      { 
       for (i = 0; i <= dtAccounts.Rows.Count - 1; i++) 
       { 
        for (j = 0; j <= dtAccounts.Columns.Count - 1; j++) 
        { 

         Excel.Range range = xlWorkSheetA.UsedRange; 
         Excel.Range cell = xlWorkSheetA.Cells[i + 1, j + 1]; 
         Excel.Borders border = cell.Borders; 
         border.LineStyle = Excel.XlLineStyle.xlContinuous; 
         border.Weight = 1d; 
         data = dtAccounts.Rows[i].ItemArray[j].ToString(); 
         xlWorkSheetA.Cells[i + 1, j + 1] = data; 
         xlWorkSheetA.Cells[i + 1, j + 1].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; 
         data = ""; 

        } 
       } 
      } 
      catch (Exception dataerror) 
      { 
       MyConn.Close(); 
       MessageBox.Show("Unable to save. Error : " + dataerror); 
      } 

      xlWorkbookA.Application.DisplayAlerts = false; 
      xlWorkbookA.CheckCompatibility = false; 
      xlWorkbookA.DoNotPromptForConvert = true; 
      xlAppA.StandardFont = "Segoe UI"; 

      xlWorkbookA.SaveAs(path + fileName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); 
      xlWorkbookA.Close(true, misValue, misValue); 
      xlAppA.Quit(); 

      releaseObject(xlWorkSheetA); 
      releaseObject(xlWorkbookA); 
      releaseObject(xlAppA); 

      MyConn.Close(); 
      Cursor.Current = Cursors.Default; 
      MessageBox.Show("File Saved!!"); 
     } 
    } 
} 

[enter image description here]

ich alles versucht habe, ich weiß, das Problem zu beheben. Was könnte dieses Problem verursachen? Jede Ideenidee wird geschätzt!

+0

Was auf der Erde? [ask] – MickyD

+0

Die Fehler, die das Problem sein könnten, sind alle in dem Bild angegeben: 1- Dateiname oder Pfad existiert nicht, 2- Datei wird verwendet, 3- Arbeitsmappe, die versucht zu speichern, hat den gleichen Namen wie a Derzeit geöffnetes Arbeitsbuch ... Hast du sie überprüft? –

+0

1. Setzen Sie einen Haltepunkt auf die Zeile "xlWorkbookA.SaveAs". 2. Stellen Sie sicher, dass "Pfad + Dateiname" tatsächlich ein vollständiger gültiger Pfad mit einem Dateinamen ist. 3. Vielleicht prüfen, ob dieser Pfad existiert? –

Antwort

1

Sie setzen Sicherheit auf Y:\Audit Reports\ ist kann schreiben können lesen. Ich hoffe es hilft dir.

string fileName = "Audit_Reports_" + string.Format("{0:MM-dd-yyyy}", dt) + ".xls"; 

enter image description here

+0

Vielen Dank, aber das Problem ist immer noch da. – theycallmesteezy

+0

Sie müssen Erweiterungsdatei für Dateiname hinzufügen. –

+0

danke Problem ist jetzt gelöst – theycallmesteezy

Verwandte Themen