我想在服务器上创建并保存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