locked
Excel Spreadsheets RRS feed

  • Question

  • Hello,

     

    In my C# application, I have a form with a button.  When the button is pressed, an template should be downloaded from the application.  In this case, the template is an Excel file.

     

    My problem is that I dont know how to add this excel file into my Visual Studio Project/Solution, and how to call it from the code .

     

    During debug, just to see if the functionality worked, I put the template file under the "Project Name"/bin/debug directory and pointed to that directory in the code.  Which worked fine...during development.

     

    string storyImportFile = Application.ExecutablePath.Replace(Application.ProductName + ".EXE", "StoryImportTemplate.xls").ToString();

    ProcessStartInfo psi = new ProcessStartInfo(storyImportFile);

    Process.Start(psi);

     

    Now, I need to find out how to build my project with the excel file include and have code for the button point to it.

     

    Thanks in Advance.

    Thursday, November 1, 2007 11:15 PM

Answers

  • You can add the Excel file to your project by right-clicking on the project name in the Solution Explorer and selecting "Add existing item". In the add existing item dialog, make sure the Files of type is "All Files" and navigate to your template. Once the file is listed in the Solution Explorer, select it and open the properties window. Make sure the Build Action property is "None" and the Copy to Output Directory is "Copy if Newer".

     

    That will place a copy of the file in the same directory as the compiler places your executable. You can then refer to it from the source just by name: new ProcessStartInfo("StoryImportTemplate.xls"). This is because by default, the "working directory" for you application will be the same directory as the executable.

     

    You might consider adding a folder to the project like "Templates" and adding them there instead of in the same directory as the executable. In this case the path would be @"Templates\StoryImportTemplate.xls".

     

    Friday, November 2, 2007 2:31 AM

All replies

  • You can add the Excel file to your project by right-clicking on the project name in the Solution Explorer and selecting "Add existing item". In the add existing item dialog, make sure the Files of type is "All Files" and navigate to your template. Once the file is listed in the Solution Explorer, select it and open the properties window. Make sure the Build Action property is "None" and the Copy to Output Directory is "Copy if Newer".

     

    That will place a copy of the file in the same directory as the compiler places your executable. You can then refer to it from the source just by name: new ProcessStartInfo("StoryImportTemplate.xls"). This is because by default, the "working directory" for you application will be the same directory as the executable.

     

    You might consider adding a folder to the project like "Templates" and adding them there instead of in the same directory as the executable. In this case the path would be @"Templates\StoryImportTemplate.xls".

     

    Friday, November 2, 2007 2:31 AM
  • One can embed any file as an embedded resource in an .net executable by bringing the item into the project and changing its build action to be embedded. (Found off of the properties of the file in the Solutions Explorer of Visual Studio). Once running one reflects off of the exe and extracts the item into memory. Here is an article to get you started: How to embed and access resources by using Visual C#
    Friday, November 2, 2007 2:44 PM
    Moderator