create excel add-凯发k8网页登录

main content

create excel add-in from matlab

supported platform: microsoft® windows® only.

this example shows how to use matlab® compiler™ to generate a microsoft excel® add-in containing a custom function for use within excel. the function mymagic returns an n-by-n magic square matrix that has equal row and column sums. the target system does not require a licensed copy of matlab to run the add-in.

before you begin, verify that you have met all of the excel target requirements for matlab compiler. for details, see .

note

to generate the visual basic® files, enable trust access to the vba project object model in excel. if you do not do this, you can manually create the add-in by importing the .bas file into excel.

create function in matlab

in matlab, locate the matlab code that you want to deploy as an excel add-in.

for this example, compile the function mymagic.m located in matlabroot\toolbox\matlabxl\examples\xlmagic.

function y = mymagic(x)
y = magic(x)

at the matlab command prompt, enter mymagic(5).

the output is a 5-by-5 square.

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

create excel add-in using library compiler app

package the function into an excel add-in using the library compiler app. alternatively, if you want to create an excel add-in from the matlab command window using a programmatic approach, see create excel add-in using compiler.build.exceladdin.

  1. to open the library compiler app, type librarycompiler at the matlab prompt.

    alternatively, on the matlab apps tab, on the far right of the apps section, click the arrow. in application deployment, click library compiler.

  2. in the matlab compiler project window, select excel add-in in the type section.

    library compiler with excel add-in type selected and mymagic.m in the exported function section

  3. in the library compiler app project window, specify the files of the matlab application that you want to deploy.

    1. in the exported functions section of the toolstrip, click add exported function to the project.

    2. in the add files window, browse to the example folder, and select the function you want to package. click open.

    the function is added to the list of exported function files. repeat this step to package multiple files in the same application.

  4. in the packaging options section of the toolstrip, decide whether to include the matlab runtime installer in the generated application by selecting one of the options:

    • runtime downloaded from web — generate an installer that downloads the matlab runtime and installs it along with the deployed matlab application. you can specify the file name of the installer.

    • runtime included in package — generate an application that includes the matlab runtime installer. you can specify the file name of the installer.

      note

      the first time you select this option, you are prompted to download the matlab runtime installer.

  5. next, define the name of your add-in and verify the class mapping for the .m file that you are building into your application.

    1. the library name field is automatically populated with mymagic as the name of the assembly. rename it as xlmagic. the same name is followed through in the implementation of the add-in.

    2. verify that the function defined in mymagic.m is mapped into xlmagicclass. double-click on the class to change the class name.

    library compiler class name section with class name xlmagicclass for the method mymagic

  6. customize the packaged application and its appearance:

    • library information — editable information about the deployed application. the generated installer uses this information to populate the installed application metadata. see customize the installer.

    • additional installer options — edit the default installation path for the generated installer and selecting custom logo. see change the installation path .

    • files required for your library to run — additional files required by the generated application to run. these files are included in the generated application installer. see manage required files in compiler project.

    • files installed for your end user — files that are installed with your application.

      see specify files to install with application.

    • additional runtime settings — platform-specific options for controlling the generated executable. see additional runtime settings.

    library compiler with project name xlmagic and class name xlmagicclass

package the application

when you are finished selecting your packaging options, save your library compiler project and generate the packaged application.

  1. click package.

    in the save project dialog box, specify the location to save the project.

  2. in the package dialog box, verify that open output folder when process completes is selected.

    when the packaging process is complete, examine the generated output in the target folder.

    • three folders are generated: for_redistribution, for_redistribution_files_only, and for_testing.

      for more information about the files generated in these folders, see .

    • the log file packaginglog.html contains packaging results.

create excel add-in using compiler.build.exceladdin

as an alternative to the library compiler app, you can create an excel add-in using a programmatic approach. if you have already created an add-in using the library compiler, see install add-in in excel.

  1. in matlab, locate the matlab code that you want to deploy as a standalone application. for this example, compile using the file mymagic.m located in matlabroot\toolbox\matlabxl\examples\xlmagic.

    appfile = fullfile(matlabroot,'toolbox','matlabxl','examples','xlmagic','mymagic.m');
  2. build the excel add-in using the compiler.build.exceladdin function. use name-value arguments to set the library and class names, and enable the generation of visual basic files.

    buildresults = compiler.build.exceladdin(appfile, ...
    'addinname','xlmagic', ...
    'classname','xlmagicclass', ...
    'generatevisualbasicfile','on');

    you can specify additional options in the compiler.build command by using name-value arguments. for details, see .

    the object buildresults contains information on the build type, generated files, included support packages, and build options.

    the function generates the following files within a folder named mymagicexceladdin in your current working directory:

    • dlldata.c

    • gettingstarted.html

    • includedsupportpackages.txt

    • xlmagic.def

    • xlmagic.bas

    • xlmagic.rc

    • xlmagic.xla

    • xlmagic_1_0.dll

    • xlmagic_dll.cpp

    • xlmagic_idl.h

    • xlmagic_idl.idl

    • xlmagic_idl.tlb

    • xlmagic_idl_i.c

    • xlmagic_idl_p.c

    • xlmagicclass_com.cpp

    • xlmagicclass_com.hpp

    • mccexcludedfiles.log

    • mwcomtypes.h

    • mwcomtypes_i.c

    • mwcomtypes_p.c

    • readme.txt

    • requiredmcrproducts.txt

    • unresolvedsymbols.txt

    note

    the generated add-in does not include matlab runtime or an installer. to create an installer using the buildresults object, see .

install add-in in excel

  1. open microsoft excel.

  2. click the file tab, click options, and then click the add-ins category.

  3. in the manage box, click excel add-ins, and then click go. the add-ins dialog box appears.

  4. click browse and locate the add-in xlmagic.xla.

  5. you are prompted to copy xlmagic.xla to the addins folder associated with your user name. you can choose to copy the add-in or run it directly. for this example, select, yes. the add-in is copied and added to your workbook.

  6. click ok to close the add-ins dialog box

test add-in

  1. select a grid of 3-by-3 cells in the excel workbook.

  2. enter the following custom function in the formula bar:

    =mymagic(3)
    as you type my in the formula bar, mymagic appears as a custom function in excel.
  3. press ctrl shift enter on the keyboard.

    the selected cells display the following output:

    8	1	6
    3	5	7
    4	9	2
  4. for additional examples, open the file xlmagic.xls located in matlabroot\toolbox\matlabxl\examples\xlmagic. this spreadsheet contains three custom vba macros that demonstrate various ways of using the compiled matlab function.

distribute add-in

to distribute your add-in to end users, see .

see also

|

related topics

    网站地图