Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am exporting my gridview to excel and i am saving it in the folder which is in my project and after that i am setting a password to the excel file.

Problem is i cannot find the path

ERROR:

Sorry, we couldn't find ../../files/Test.xlsx. Is it possible it was moved, renamed or deleted?

What I have tried:

 public static void Setpassword()
        {           
            string filename = "../../files/Test.xlsx";
            string password = "1245";

            if (!File.Exists(filename))
                return;

            Microsoft.Office.Interop.Excel.Application oexcel;
            Microsoft.Office.Interop.Excel.Workbook obook;

            oexcel = new Microsoft.Office.Interop.Excel.Application();
            oexcel.DisplayAlerts = false;
           
            obook = oexcel.Workbooks.Open(filename, 0, false, 5, "", "", true, System.Reflection.Missing.Value, "\t", false, false, 0, true, 1, 0);

//ERROR : Sorry, we couldn't find ../../files/Test.xlsx. Is it possible it was moved, renamed or deleted?

            try
            {
                obook.SaveAs(filename, Password: password, ConflictResolution: Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges);
            }
            finally
            {
                obook.Close();
                oexcel.Quit();
            }
        }
Posted
Updated 13-Mar-16 23:07pm
Comments
VR Karthikeyan 17-Mar-16 8:46am    
Have you tried the solution 2?

1 solution

Hi, don't mention like "../../files/Test.xlsx" to store in the directory of the project.
Use like the following,
C#
string filename = Environment.CurrentDirectory + "\Test.xlsx";
//Environment.CurrentDirectory - represents the current working directory of the project.

Or you can use this also,
C#
string filename = AssemblyDirectory + "\Test.xlsx";

public static string AssemblyDirectory
{
   get
   {
      string codeBase = Assembly.GetExecutingAssembly().CodeBase;
      UriBuilder uri = new UriBuilder(codeBase);
      string path = Uri.UnescapeDataString(uri.Path);
      return Path.GetDirectoryName(path);
   }
}
//Here AssemblyDirectory is a string property, which also represents the directory of the current project.
 
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