這裏是我的代碼:類型的「System.Runtime.InteropServices.COMException」發生未處理的異常
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!!");
}
}
}
我已經試過我知道解決問題的一切。什麼可能導致這個問題?任何想法的主意是讚賞!
? [問] – MickyD
可能是這個問題的錯誤全部在你所顯示的圖像中陳述: 1-文件名或路徑不存在,2-正在使用文件,3-正在嘗試保存的工作簿具有與目前打開的工作簿...你檢查了他們嗎? –
1.在'xlWorkbookA.SaveAs'行放置一個斷點。 2.確認'path + fileName'實際上是一個具有文件名的完整有效路徑。 3.也許檢查這條路是否存在? –