我想在服務器上創建並保存excel文件,並稍後將其作爲文檔發送。在添加附件時,我收到了錯誤。任何人有任何想法,我該如何解決它?將Excel文件作爲附件發送時出錯
The process cannot access the file because it is being used by another process
請找我正在使用的是創建Excel文件和返回路徑將在以後添加文件的代碼,:
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
string attachmentPath = Convert.ToString(ConfigurationManager.AppSettings["AttachmentPath"] as object);
string path = attachmentPath + FileName;
excel.Application.Workbooks.Add(true);
int ColumnIndex = 0;
foreach (DataColumn col in table.Columns)
{
ColumnIndex++;
excel.Cells[1, ColumnIndex] = col.ColumnName;
}
int rowIndex = 0;
foreach (DataRow row in table.Rows)
{
rowIndex++;
ColumnIndex = 0;
foreach (DataColumn col in table.Columns)
{
ColumnIndex++;
excel.Cells[rowIndex + 1, ColumnIndex] = row[col.ColumnName].ToString();
}
}
excel.DisplayAlerts = false;
excel.ActiveWorkbook.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing,
false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
excel.Quit();
return path + ".xls";
下面是我用上面的路徑添加爲代碼附件的電子郵件:
if (attachmentPaths != null && attachmentPaths.Length > 0)
{
foreach (string path in attachmentPaths)
{
if (!string.IsNullOrEmpty(path))
message.Attachments.Add(new System.Net.Mail.Attachment(path));
}
}
SmtpClient smtp = new SmtpClient(objBO.SmtpDomain);
smtp.Send(message);
請提供建議。提前致謝。
我知道這是一個愚蠢的問題,但你沒有在Excel中打開的文件或任何偶然的機會嗎? – ars265
不,我正在創建代碼中的excel文件,並在下一刻將它作爲附件發送。在後面的部分中,我應該刪除創建的同一個excel文件。所以沒有問題,同時手動打開它。 – ABC
您的網站可能運行在不是所有權限的帳戶下 – meda