Note that there are some explanatory texts on larger screens.

plurals
  1. POError when sending Excel file as attachment
    primarykey
    data
    text
    <p>I am trying to create and save an excel file on server and later send it as a document. At the time of adding attachment, I am getting below error. Anyone have any idea, how can I resolve it ?</p> <pre><code>The process cannot access the file because it is being used by another process </code></pre> <p>Please find the code which I am using to create excel file and return path, that will be later added as document :</p> <pre><code>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"; </code></pre> <p>Below is the code where I use above path to add as an attachment to the email :</p> <pre><code> if (attachmentPaths != null &amp;&amp; attachmentPaths.Length &gt; 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); </code></pre> <p>Kindly provide suggestions. Thanks, in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload