Use VC ++ to call the C # encapsulated DLL library for Excel File Operations (you do not need to install the office software environment)

Source: Internet
Author: User

Use Visual Studio 2005 to create Class Libtary of C # and copy the following code.
[Csharp] view plaincopyprint?
// Class1.cs
// A simple managed DLL that contains a method to add two numbers.
Using System;
 
Namespace ManagedDLL
{
// Interface declaration.
Public interface ICalculator
{
Int Add (int Number1, int Number2 );
};
 
// Interface implementation.
Public class ManagedClass: ICalculator
{
Public int Add (int Number1, int Number2)
{
Return Number1 + Number2;
}
}
}
Open "tool =" Visual Studio 2005 Command Prompt "and input" sn.exe-k MyKeyFile. SNK "creates the key file and sets MyKeyFile. copy the SNK file from the C: \ Program Files \ Microsoft Visual Studio 8 \ vc \ bin directory to your project directory and open AssemblyInfo. cs file. Replacement code:
[Assembly: ComVisible (false)]
[Assembly: AssemblyDelaySign (false)]
[Assembly: AssemblyKeyFile ("")]
Use
[Assembly: ComVisible (true)]
[Assembly: AssemblyDelaySign (false)]
[Assembly: AssemblyKeyFile (".. \ MyKeyFile. SNK")]
Compile the connection to generate the Dll file.

To be able to call the newly created hosted DLL file, you need to register this component.
Open Visual Studio 2005 Command prompt, switch to the generated dlldirectory, and enter the Command "manage regasm.exe ManagedDLL. dll/tlb: ManagedDLL. tlb/codebase". A message is displayed, indicating that the registration is successful.
Next, create a C ++ Win32 Project, open the Code view, and import the library file generated by regasm.exe.
// Import the type library.
# Import ".. \ ManagedDLL \ bin \ Debug \ ManagedDLL. tlb" raw_interfaces_only
Note that the file path is the same as the DLL file.
The code for the complete file is as follows:
C ++ Client
// CPPClient. cpp: Defines the entry point for the console application.
// C ++ client that calla managed DLL.

# Include "stdafx. h"
# Include "tchar. h"
// Import the type library.

# Import ".. \ ManagedDLL \ bin \ Debug \ ManagedDLL. tlb" raw_interfaces_only
Using namespace ManagedDLL;
Int _ tmain (int argc, _ TCHAR * argv [])
{
// Initialize COM.
HRESULT hr = CoInitialize (NULL );

// Create the interface pointer.
ICalculatorPtr pic1c (_ uuidof (ManagedClass ));

Long lResult = 0;

// Call the Add method.
Pic1c-> Add (5, 10, & lResult );

Wprintf (L "The result is % d \ n", lResult );


// Uninitialize COM.
CoUninitialize ();
Return 0;
}

Note:
Note that the compiler option supported by the general language runtime is changed to (// clr: oldSyntax ).
The procedure is as follows: Right-click Project property =, configure property =, General =, common language runtime support =, original syntax (clr: oldSyntax)
By now, the Demo of calling the Dll file of C # In C ++ has been completed.

The following describes how to add an Excel file to the DLL file of C.
Add reference to your project:

In fact, I only added the org. in2bits. MyXls. dll file, which can be Baidu by myself.
Click C #'s project, right-click reference, right-click reference, and find the org. in2bits. MyXls. dll file, as shown in:


Add code: using org. in2bits. MyXls;
During this compilation, you may encounter the following error,

After the above operations, the DLL file has been referenced successfully. Next, you can use the classes, methods, and attributes in the DLL file.
Refer to how to debug the C # DLL.
Have A Good Time! Start your adventure ....
Refer:
Export the Excel test. reprinted the above link. Thank you here.
[Csharp]
Protected void ExportBtn_Click (object sender, EventArgs e)
{
XlsDocument xls = new XlsDocument ();
Xls. FileName = "TestList.xls ";
 
Int rowIndex = 1;
Worksheet sheet = xls. Workbook. Worksheets. Add ("test table"); // Sheet Name
 
Cells cells = sheet. Cells;
Cell cell = cells. Add (1, 1, "No ");
Cell. Font. Bold = true;
Cell = cells. Add (1, 2, "name ");
Cell. Font. Bold = true;
 
Foreach (DataRow row in table. Rows)
{
Cells. Add (rowIndex, 1, rowIndex );
Cells. Add (rowIndex, 2, "name" + rowIndex );
 
RowIndex ++;
}
Xls. Send (); // if it is changed to the Save () function, the Excel file can be saved locally. The file is in the source file directory of the C ++ project.
}
Oh, forget it. Note that org. in2bits. myXls. dll file, copy it to the directory of the EXE folder generated by VC ++ (this can be based on the Visual Studio output prompt, I did not see it, almost failed ), after debugging in the Visual Studio environment, you can go to the DLL source code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.