Re: [ExcelDna] Re: Embedding .xll in a .xlsm

1,286 views
Skip to first unread message

Naju Mancheril

unread,
Jan 28, 2013, 12:16:40 PM1/28/13
to exce...@googlegroups.com
If you want to distribute one .xll file, here is a way to do it:

(1) Make your workbook

(2) Put the workbook into your project directory (next to your .csproj file)

(3) Add it to your .NET assembly as a "file resource"
To do this, right-click the project in Visual Studio's Solution Explorer and click Properties
Click the Resources tab. It may prompt you to create a new resource file.
From the Add Resources menu, select Add Existing File.
Select the workbook from your project directory.

You can test whether this worked by checking the properties of the Resources class. For example, I made a workbook called test.xlsx and added this as a file resource with the name "test." Once I did this, I was able to write code that references "Resources.test," which is a static property of type bytes[].

(4) In your AutoOpen code, dump the bytes to a temp file and open it.

      var fileName="test2.xlsx";
      var path=Path.Combine(Path.GetTempPath(), fileName);
      using(var writer2=new FileInfo(path).OpenWrite()) {
        var bytes=Resources.test;
        writer2.Write(bytes, 0, bytes.Length);
      }
      var app=(Application)ExcelDnaUtil.Application;
      app.Workbooks.Open(path);

(5) [untested, since I never use packed files] Use ExcelDna's normal pack tool to create a single .xll file.

That's it!

Note that this opens a copy of the file. If the user wants to save changes to this file, then he will be able to save a new .xlsx, but that will be just an Excel workbook. It won't have the addin code.

Part of the difficulty here is that this is not the intended usage of the addin. It's basically supposed to be an Application/plugin thing, not a way to distribute data+code.

Vincent's solution should also work, but it will require two files. Maybe you can put the .xll on a shared drive or something? His approach has the advantage that the user can save changes to the workbook and distribute these modified files.

Jay Carr

unread,
Jan 29, 2013, 1:31:04 PM1/29/13
to exce...@googlegroups.com
The MSI would allow for a single file, that is true.  My only concern is I want people to be able to treat the file as something similar to a .xlsm file.  Open it when you want to open it, close it when you're done.  No loading process required. 

But, if that turns out to be impossible, I'll take a closer look at the MSI approach...


On Tue, Jan 29, 2013 at 12:21 PM, Jon49 <nyma...@gmail.com> wrote:
Jay, If you put it in an MSI then you will only be distributing a single file even though multiple files will exist. Also, you can put the xlsm on a website and download it the first time if they don't already have a file. That's how I do the Time Card add-in. I have a zipped xlt file that downloads, unzips, and then I create a new Excel document from the template.

--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To post to this group, send email to exce...@googlegroups.com.
Visit this group at http://groups.google.com/group/exceldna?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Naju Mancheril

unread,
Jan 29, 2013, 1:52:18 PM1/29/13
to exce...@googlegroups.com
Are you adding the .xlsm file as a resource? I thought resources were compiled into the assembly by default (not deployed as a separate file). If so, then you should be able to pack the big, resulting .dll file. Here is some documentation on .NET resources:

http://msdn.microsoft.com/en-us/library/7k989cfy(v=vs.80).aspx

Jay Carr

unread,
Jan 29, 2013, 4:36:15 PM1/29/13
to exce...@googlegroups.com
I've actually just been including the file in my file structure inside the project.  Everytime I compile it puts the latest copy of my .xlsm into bin/Debug (or bin/Release, if I have it configured that way.) 

Anyway, adding it as a resource seems to me to be the only real way to do it because it seems that that's essentailly what ExcelDNAPack is doing anyway.  But I'm having a hard time working with a .xlsm inside of the .resx that I've set up.  I can add it just fine but so far I can't figure out how to open up the resource once I'm inside of AutoOpen();

I'll keep plugging away at it though, any and all suggestions welcome.


On Tue, Jan 29, 2013 at 12:52 PM, Naju Mancheril <naju.ma...@gmail.com> wrote:
Are you adding the .xlsm file as a resource? I thought resources were compiled into the assembly by default (not deployed as a separate file). If so, then you should be able to pack the big, resulting .dll file. Here is some documentation on .NET resources:

http://msdn.microsoft.com/en-us/library/7k989cfy(v=vs.80).aspx

--

Naju Mancheril

unread,
Jan 29, 2013, 4:51:18 PM1/29/13
to exce...@googlegroups.com

Check my example code from earlier mail. You need to dump the bytes to disk and use Excel interop.

Jay Carr

unread,
Jan 30, 2013, 9:50:56 AM1/30/13
to exce...@googlegroups.com
Naju -- Thank you very much for your patience.  You're original solution was correct, I just didn't know enough about how resources work in .NET to understand it.  After working through several examples and finally getting the hang of things I read your original code and was able to successfully use it in my own solution.  So thank you a million!


On Tue, Jan 29, 2013 at 3:51 PM, Naju Mancheril <naju.ma...@gmail.com> wrote:

Check my example code from earlier mail. You need to dump the bytes to disk and use Excel interop.

--

Naju Mancheril

unread,
Jan 30, 2013, 10:21:38 AM1/30/13
to exce...@googlegroups.com
Great, you're welcome!
Reply all
Reply to author
Forward
0 new messages