1

I wrote a program in WPF that has to write in an Excel file. The Excel File already exists (I created it so the file exists!), but I want that if I publish my program that users also have this Excel file automatically.

So how can I fill this gap with the "?" (There should be the Excel file)

At the moment, I only open the file:

if (Properties.Settings.Default.AlsExcel == true)
{
    Excel.Application objExcel = new Excel.Application();
    objExcel.Visible = false;
    objExcel.WindowState = Excel.XlWindowState.xlNormal;

    object missing = Missing.Value;
    string Name = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DienstplanEx.xlsx");
    Excel.Workbook objWorkbook = objExcel.Workbooks.Open(Name, missing, missing, missing,
        missing, missing, missing, missing, missing, missing, missing, missing, missing,
        missing, missing);
    Excel.Worksheet worksheet = (Excel.Worksheet)objWorkbook.Worksheets["Tabelle1"];
    if (!Directory.Exists(Pfad))
    {
        Directory.CreateDirectory(Pfad);
    }
    objWorkbook.SaveAs(Pfad + "\\Plan.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    objWorkbook.Close();
    objExcel.Quit();
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Nick_SMI
  • 43
  • 1
  • 6

1 Answers1

0

If I understand your question correctly, you are asking only how to include a file to the program you are distributing.

What you have to do is include the Excel file into your project and set the Build Action to Content in Properties window for that file. If you have an installer project, this file should then get included into your installation (if not, you should add Content Files as a Project Output on your installer).

Check these two resources for more help.

Community
  • 1
  • 1
the berserker
  • 1,553
  • 3
  • 22
  • 39
  • Exactly! Sorry for my bad English :/ – Nick_SMI Dec 27 '15 at 11:27
  • And How can I use this file? I need a path to this file...!? – Nick_SMI Dec 27 '15 at 11:30
  • I'm not sure what your scenario is but - you could host your file just next to the executable and access it with ```Path.Combine``` and ```AppDomain.CurrentDomain.BaseDirectory``` or perhaps ```System.Reflection.Assembly.GetEntryAssembly().Location``` and handle the relative path from there - or if you'd install the file to user's ```%AppData%``` folder, then you can get it with ```Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)``` - see also [this link](http://stackoverflow.com/questions/867485/c-sharp-getting-the-path-of-appdata) – the berserker Dec 27 '15 at 21:18