Is there a way to have a Excel file attached to Visual C project without the need of always having the correct directory location

0 votes

I'm attempting to complete a project and am still quite new to coding. When I'm finished, I want the Excel file to be a part of the software so that you could theoretically take the project files wherever you go. I need to open an Excel file that I created as a template to make copies. I want it to be able to be utilised even if the local computer does not have the file because it is writing data to a file (rather than reading and pulling data) (I.E. it should be in files with the application).

I haven't put much effort into attempting to make it work thus far because I've been busy. Although I now have the Excel workbook opening based on file location, this is not the end result I am hoping for. I have the project file on a flash drive because I am working on this across many computers, however if I work on it on a different system, the flash drive's root directory name is the same, thus it won't function. Can I turn it into an item or something in Visual Studio so it can be opened and modified without requiring a different file location than the project directory?

 //open workbook.
      oWB = (Excel._Workbook)(oXL.Workbooks.Open(@"[workbook file location here]"));
      oSheet = (Excel._Worksheet)oWB.ActiveSheet;
Apr 4, 2023 in Others by narikkadan
• 63,700 points
699 views

1 answer to this question.

0 votes

You can add the file to your project. Either as content or as part of a resource.

As Content

  1. Right-click your project
  2. Select 'Add existing item'
  3. Choose the Excel file
  4. Right-click the added Excel file
  5. Select 'Properties'
  6. Choose 'Content' as the build type
  7. Choose 'Copy if newer

As Resource

I like the resource approach because it makes it easy to access the file later on in your project.

  1. Right-click your project
  2. Select 'Add item'
  3. Search for 'Resource'
  4. Open the added file and choose 'Files' for the types
  5. Add your Excel file with Drag & Drop to the resource
  6. Now you can access the file in C# from Resources.Name_of_your_Excel_File
answered Apr 4, 2023 by Kithuzzz
• 38,010 points

Related Questions In Others

0 votes
1 answer

Is there any way in python to auto-correct spelling mistake in multiple rows of an excel files of a single column?

Use Spellchecker for doing your stuff: import pandas ...READ MORE

answered Oct 14, 2022 in Others by narikkadan
• 63,700 points
1,694 views
0 votes
1 answer
0 votes
1 answer

Excel: Is it possible to reorder the data in 2 columns to match up if they have a certain number of characters / a string in common?

Try this: =LET(files,A1:A4, URLs,B1:B4, f,BYROW(files,LAMBDA(r,TEX ...READ MORE

answered Jan 21, 2023 in Others by narikkadan
• 63,700 points
335 views
0 votes
1 answer

Is there a way to produce a sum according to date/time stamp values of another column on excel?

Assuming the input data for the second ...READ MORE

answered Mar 25, 2023 in Others by narikkadan
• 63,700 points
444 views
0 votes
1 answer

Generate a flat list of all excel cell formulas

Hello, you'll have to follow certain steps ...READ MORE

answered Feb 18, 2022 in Others by gaurav
• 23,260 points
414 views
0 votes
1 answer

Convert a number to a letter in C# for use in Microsoft Excel [duplicate]

If you are familiar with using formulas ...READ MORE

answered Feb 23, 2022 in Database by gaurav
• 23,260 points
657 views
0 votes
1 answer

Deleting duplicate rows in Excel using Epplus

You need to re-think this… the while ...READ MORE

answered Feb 23, 2022 in Database by gaurav
• 23,260 points
1,171 views
0 votes
1 answer

Export DataTable to Excel File

Add Interop References. First we need to ...READ MORE

answered Jun 9, 2022 in JQuery by gaurav
• 23,260 points
617 views
0 votes
1 answer
0 votes
1 answer

Is there a function in excel to automatically calculate age using date of birth?

Try  =INT((YEARFRAC(TODAY(),B3,1)))  Where cell B3 contains a date like ...READ MORE

answered Mar 28, 2023 in Others by Kithuzzz
• 38,010 points
385 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP