本文介绍了如何检查文件夹中存在的文件并替换新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的代码如下 excel旧文件必须删除,新文件必须自动替换。My code as followsthat excel old file has to be deleted and new file has to be replace automatically.private void Preseaintake(){ try { SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = "Presea_Intakerpts '" + dtfromdate.Text.Trim() + "', '" + dttodate.Text.Trim() + "'"; SqlDataAdapter adp = new SqlDataAdapter(cmd); DataTable table = new DataTable(); adp.Fill(table); Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook xlWorkBook; Microsoft.Office.Interop.Excel.Worksheet xlsheet; object misvalue = System.Reflection.Missing.Value; xlWorkBook = (Microsoft.Office.Interop.Excel.Workbook)xlapp.Workbooks.Add(1); xlsheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.ActiveSheet; for (int i = 0; i < table.Columns.Count; i++) { xlsheet.Cells[1, i + 1] = table.Columns[i].ColumnName; } for (int i = 0; i < table.Rows.Count; i++) { for (int j = 0; j < table.Columns.Count; j++) { xlsheet.Cells[i + 2, j + 1] = table.Rows[i][j].ToString().Trim(); } } xlWorkBook.SaveAs(Application.StartupPath + "\\Preseaintake\\newtest.xls", misvalue, misvalue, misvalue, misvalue, misvalue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misvalue, misvalue, misvalue, misvalue, misvalue); xlWorkBook.Close(true, misvalue, misvalue); xlsheet = null; xlapp = null; } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK); return; }}private void sendmail(){ try { MailMessage mis = new MailMessage(); SmtpClient smtpserver = new SmtpClient("smtp.gmail.com"); smtpserver.Credentials = new NetworkCredential("[email protected]", "hdfdf"); smtpserver.Host = "smtp.gmail.com"; smtpserver.Port = 587; smtpserver.EnableSsl = true; mis.From = new MailAddress("[email protected]", "PreseaIntakeDetails"); mis.IsBodyHtml = true; mis.To.Add("[email protected]"); mis.Subject = "PreseaIntake Report"; System.Net.Mail.Attachment attachment; attachment = new System.Net.Mail.Attachment(Application.StartupPath + "\\Preseaintake\\newtest.xls"); mis.Attachments.Add(attachment); smtpserver.Send(mis); mis.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK); return; }} i想删除旧文件并替换为新的。 我该怎么办? 在我上面的代码我有什么变化制作。 请帮帮我。 问候, narasiman P.i want to delete the old file and replace with new one.for that how can i do?in my above code what changes i have to made.please help me.regards,narasiman P.推荐答案string filePath = Application.StartupPath + "\\Preseaintake\\newtest.xls";if(System.IO.File.Exists(filePath)) System.IO.File.Delete(filePath)xlWorkBook.SaveAs(filePath, misvalue, misvalue, misvalue, misvalue, misvalue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misvalue, misvalue, misvalue, misvalue, misvalue); xlWorkBook.Close(true, misvalue, misvalue); xlsheet = null; xlapp = null; 这篇关于如何检查文件夹中存在的文件并替换新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-15 09:12