我尝试发送excel文件,但我收到错误 [英] I tried in sending excel file but I am getting error
本文介绍了我尝试发送excel文件,但我收到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
该进程无法访问文件'C:\ Users \ God \Desktop \ DataDump \ OneExcel.xls',因为它正由另一个进程使用。
The process cannot access the file 'C:\Users\God\Desktop\DataDump\1Excel.xls' because it is being used by another process.
int count = 0;
string connectionstring = "Server=(local);initial catalog=Test;Trusted_Connection=True";
SqlConnection sqlConnection = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
DataSet ds = new DataSet();
cmd.CommandText = "select * from Empdetails";
cmd.CommandText += " where shifttype = @par ";
cmd.Parameters.Add("@par", SqlDbType.Int).Value = j;
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection;
sqlConnection.Open();
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
System.IO.StreamWriter sw_In = new System.IO.StreamWriter(@"C:\Users\God\Desktop\DataDump\" + j + "Excel.xls");
while (reader.Read())
{
if (count == 0)
{
for (int i = 0; i < reader.FieldCount; i++)
{
sw_In.AutoFlush = true;
sw_In.Write(reader.GetName(i) + "\t");
}
sw_In.Write("\n");
count = 1;
}
for (int i = 0; i < reader.FieldCount; i++)
{
sw_In.AutoFlush = true;
sw_In.Write(reader[i].ToString() + "\t");
}
MailMessage mis = new MailMessage();
SmtpClient smtpserver = new SmtpClient("smtp.gmail.com");
smtpserver.Credentials = new System.Net.NetworkCredential("narasiman1986@gmail.com", "narasiman123");
smtpserver.Host = "smtp.gmail.com";
smtpserver.Port = 587;
smtpserver.EnableSsl = true;
mis.From = new MailAddress("narasiman1986@gmail.com", "Report");
mis.IsBodyHtml = true;
System.Net.Mail.Attachment attachment;
attachment = new
System.Net.Mail.Attachment(@"C:\Users\God\Desktop\DataDump\" + j + "Excel.xls");
mis.Attachments.Add(attachment);
mis.To.Add("narasiman1986@gmail.com");
mis.CC.Add(new MailAddress("narasiman1986@gmail.com"));
mis.Subject = "Data Dump Report";
smtpserver.Send(mis);
sw_In.Write("\n");
}
}
sqlConnection.Close();
reader.Close();
when i run the above code shows error as follows
The process cannot access the file 'C:\Users\God\Desktop\DataDump\1Excel.xls' because it is being used by another process.
the error shows in below line as follows
System.Net.Mail.Attachment(@"C:\Users\God\Desktop\DataDump\" + j + "Excel.xls");
i am sending the excel file to mail.
What I have tried:
i am sending excel file to mail. but i am getting error as follows
The process cannot access the file 'C:\Users\God\Desktop\DataDump\1Excel.xls' because it is being used by another process.
推荐答案
这里与你的代码无关,错误信息非常明确,问题不在你的代码中:
There is nothing to do with your code here, the error message is pretty explicit, the problem is not in your code:
Quote:
该进程无法访问文件'C:\ Users \ God\Desktop\DataDump \\\Excel.xls'因为它正被另一个进程使用。
The process cannot access the file 'C:\Users\God\Desktop\DataDump\1Excel.xls' because it is being used by another process.
错误信息中你不理解哪个词?
Which word you don't understand in the error message ?
这篇关于我尝试发送excel文件,但我收到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文