Visual Basic, .NET, ASP, VBScript
 

   
   
     

Форум - VBA

Страница: 1 |

 

  Вопрос: Excel-DNA: Программа для конвертации VBA Excel в С Добавлено: 29.09.12 13:33  

Автор вопроса:  Григорий
Всем доброе время суток.
После долгих поисков удалось найти программу Excel-DNA .
http://exceldna.codeplex.com/
Остальные платные.
У меня установлена программа EXCEL 2007.
но я не могу понять некоторые нюансы по установке программы.

Ответить

  Ответы Всего ответов: 3  

Номер ответа: 1
Автор ответа:
 Григорий



Вопросов: 21
Ответов: 35
 Профиль | | #1 Добавлено: 29.09.12 13:40
Getting Started with ExcelDna
Do this first:

    The .NET Framework 2.0 Runtime or a later version must be installed. The .NET Framework Version 2.0 Redistributable Package is available from Microsoft.
    Get the most recent release of ExcelDna: Download Excel-DNA version 0.29, unzip in a convenient directory.
    Macro security in Excel must not be 'Very High' or 'High' (if set to Medium -- it will prompt whether to enable each macro library). To use the .NET macros you will have to 'Enable' at the prompt.

1. Create a user-defined function in Visual Basic

    Make a copy of ExcelDna.xll in a convenient directory, calling the copy Test1.xll.
    Create a new text file, called Test1.dna (the same prefix as the .xll file), with contents:
  1. <DnaLibrary>
  2.     <![CDATA[
  3.  
  4.         Public Module MyFunctions
  5.  
  6.  
  7.             Function AddThem(x, y)
  8.                 AddThem = x + y
  9.             End Function
  10.  
  11.  
  12.         End Module
  13.     ]]>
  14.     </DnaLibrary>

    Load Test1.xll in Excel (either File->Open or Tools->Add-Ins and Browse...).
    You should be prompted whether to Enable Macros, click Enable.
    Enter =AddThem(4,2) into a cell - you should get 6.
    There should also be an entry for AddThem in the function wizard, under the category Test1.


Troubleshooting

    If you are not prompted to Enable Macros and nothing else happens, your security level is probably on High. Set it to Medium.
    If you get a message indicating the .Net 2.0 runtime could not be loaded, you might not have the .NET Framework 2.0 installed. Install it.
    If Excel crashes with an unhandled exception, an access violation or some other horrible error, either during loading or when running the function, please let me know. This shouldn't happen, and I would like to know if it does.
    If a window appears with the title 'ExcelDna Error Display' then there were some errors trying to compile the code in the .dna file. Check that you have put the right code into the .dna file.
    If Excel prompts for Enabling Macros, and then the function does not work and does not appear in the function wizard, you might not have the right filename for the .dna file. The prefix should be the same as the .xll file and it should be in the same directory.
    Otherwise, if something goes wrong, let me know, or post on the discussion list.
2. Creating a user-defined function in C#
Change the contents of Test1.dna to:

  1.   <DnaLibrary Language="CS">
  2.     <![CDATA[
  3.  
  4.         using ExcelDna.Integration;

  5.                 public class MyFunctions
  6.                 {
  7.                         [ExcelFunction(Description="Joins a string to a number", Category="My functions")]
  8.                         public static string JoinThem(string str, double val)
  9.                         {
  10.                                 return str + val;
  11.                         }
  12.                 }
  13.     ]]>
  14.     </DnaLibrary>

    Reload the .xll, either from File->Open or in Tools->Add-Ins.
    Check with the formula =JoinThem("abc", 123)
    If the first example worked, this one should too.

3. Making the functions from a compiled library available
ExcelDna can also load any compiled .NET library. Public static functions with a compatible signature are exported to Excel.

    Create a file called TestLib.cs containing the following code:
  1. using ExcelDna.Integration;

  2. public class MyFunctions
  3. {
  4.         [ExcelFunction(Description="Multiplies two numbers", Category="Useful functions")]
  5.         public static double MultiplyThem(double x, double y)
  6.         {
  7.                 return x * y;
  8.         }
  9. }

    You need to reference the ExcelDna.Integration.dll assembly (the ExcelFunction attribute is defined there). Copy the file ExcelDna.Integration.dll next to your source file, or reference it in your project. You need not redistribute this file - a copy is embedded as a resource in the redistributable .xll
    Compile TestLib.cs to TestLib.dll: from the command-line: c:\windows\microsoft.net\framework\v2.0.50727\csc.exe /target:library /reference:ExcelDna.Integration.dll TestLib.cs
    Modify Test1.dna to contain:
  1. <DnaLibrary>
  2.                 <ExternalLibrary Path="TestLib.dll" />
  3.         </DnaLibrary>

    Reload the .xll and check =MultiplyThem(2,3)
    If you are compiling your assembly to target .NET 4, you need to tell Excel-DNA to load the right version of the CLR:
  1. <DnaLibrary RuntimeVersion="v4.0" >
  2.                 <ExternalLibrary Path="TestLib.dll" />
  3.         </DnaLibrary>

Ответить

Номер ответа: 2
Автор ответа:
 Artyom



Разработчик

Вопросов: 130
Ответов: 6602
 Профиль | | #2 Добавлено: 01.10.12 02:03
Зачем это все нужно когда уже с 2005 года есть VSTO?

Ответить

Номер ответа: 3
Автор ответа:
 Григорий



Вопросов: 21
Ответов: 35
 Профиль | | #3 Добавлено: 08.10.12 17:58
Здравствуйте, Артём.
VSTO установить не удалось.
Поставил Visual Studio 2010.
Как можно переделать макрос VBA Excel в надстройку СOM
Или записать макрос по новой в в C# .

Ответить

Страница: 1 |

Поиск по форуму



© Copyright 2002-2011 VBNet.RU | Пишите нам