Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code as follows

that excel old file has to be deleted and new file has to be replace automatically.
C#
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("reply@gmail.com", "hdfdf");
        smtpserver.Host = "smtp.gmail.com";
        smtpserver.Port = 587;
        smtpserver.EnableSsl = true;
        mis.From = new MailAddress("reply@gmail.com", "PreseaIntakeDetails");
        mis.IsBodyHtml = true;
        mis.To.Add("gdf@gmail.com");
        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 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.
Posted
Updated 14-Mar-14 2:40am
v2

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Mar-14 9:44am    
Correct, a 5; and I added my 2 cents in Solution 3.
—SA
Tejas Vaishnav 14-Mar-14 10:12am    
it will be true when we are playing with System.IO.File, but when we are using Excel interop to create excel file then we need to explicitly check that, file is already exist at that place or not, if exists then delete it.
DirectoryInfo dir_file = new DirectoryInfo(dirpath);
foreach (FileInfo fi in dir_file.GetFiles())
{
string filepath = "";
filepath = dir_file + "\\" + fi;

if (!File.Exists(filepath))
{

System.IO.FileInfo fi_del = new System.IO.FileInfo(filepath);
try
{
fi_del.Delete();
}
catch (System.IO.IOException e)
{
//Console.WriteLine(e.Message);
MessageBox.Show(e.Message);
}
}
}
may b this will help u
 
Share this answer
 
v2
I think in your existing code please make this type of modification...


C#
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;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900