Ứng dụng lập trình VSTO

Liên hệ QC
C#
Cho mình hỏi, làm sao để mở file theo đường dẫn mà nó không bị chế độ read-only nhỉ. (mình tạo add-in, gắn vào button để mở file)
Mã:
 private void button1_Click(object sender, RibbonControlEventArgs e)
        {
            string workbookPath = "C:\\Users\\Admin\\Desktop\\Tam tinh 3 ho mong tru 500.xlsx";
            COMExcel.Application exApp = new COMExcel.Application();
            COMExcel.Workbook exBook = exApp.Workbooks.Open(workbookPath,
                    0, true, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
                    true, false, 0, true, false, false);
            exApp.Visible = true;
        }
 
Gửi các anh link siêu nhanh của Embarcadero Rad Studio 10.3.1 Rio Architect 26.0.33219.4899
Link tải
Bài đã được tự động gộp:

Delphi rất hay nhưng không có tài liệu và hướng dẫn, mong các anh bớt chút time hướng dẫn chi tiết 1 bài tạo menu Ribbon với ạ
 
Còn đây là cách viết cho C#.
1. Tạo mới dữ án C# như hình dưới.
View attachment 204359
2. Nhấp chuột phải vào Class1.cs sau đó chọn Rename, gõ tên mới và nhấn Enter, nháy Yes (đổi tên Class thành FuncVBA, hoặc tên tùy ý theo ý mình, Xem hình).
View attachment 204360
3. Cài thư viện cho C# (Nếu máy đã cài rồi thì có thể bỏ qua bước này và bước 4, nếu chưa có thì tải file này ở đây) bằng cách mở Explorer lên và chọn file exceldnadoc.0.2.3.nupkg (file mới tải về) nhấp chuột phải và chọn lệnh Copy.
Trở lại dữ án C# và vào Menu Tools --> Library Package Manager --> Package Manager Settings.
View attachment 204361
4. Sau đó thực hiện các bước như hình.
View attachment 204362
5. Cập nhật thư hiện bằng cách vào menu Tools --> Library Package Manager --> Package Manager Console
View attachment 204363
6. Trên cửa sổ dòng lệnh sau PM> gõ dòng lệnh Install-Package ExcelDnaDoc -Version 0.2.3 và nhấn Enter
View attachment 204364
7. Cũng trên dòng lệnh đó gõ tiếp lệnh Install-Package ExcelDna.AddIn và nhấn Enter.
View attachment 204365
8. Cũng trên dòng lệnh đó gõ tiếp lệnh Install-Package ExcelDna.Registration và nhấn Enter.
View attachment 204366
9. Nháy chuột vào Class.cs (Cụ thể là FuncVBA đã đặt tên ở bước 2) và sửa lại code trong Class này như sau:
PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HamVBA
{
    using ExcelDna.Integration;
    public class FuncVBA
    {
        [ExcelFunction(Name = "VnSum",
                        Description = "Hàm tính tổng của 2 số thực",
                        HelpTopic = "DocTest-AddIn.chm!1002")]//Tên File Help nếu có
        public static double VnSum(
            [ExcelArgument(Description = "Giá trị thứ nhất là số thực\nCó thể chọn địa chỉ của ô tính")] double SoA,
            [ExcelArgument(Description = "Giá trị thứ hai cũng là số thực\nCó thể chọn địa chỉ của ô tính")] double SoB)
        {
            return SoA + SoB;
        }
    }
}
10. Nhấn F5 để chạy.
11. Vào Excel chọn một ô và nháy nút Fx và thực hiện các bước như hình.

View attachment 204367
12. Gõ các tham số cho hàm và nháy Ok.
View attachment 204368
13. Đóng Excel lại, Mở Menu Project --> HamVBA Properties.
View attachment 204369
14. Nháy vào thẻ Debug và xem file xll đã được tạo ra.

15. Mở Explorer và tìm thư mục chứa Project và chọn thư mục HamVBA --> Bin --> Debug có chứa các tệp tin được tạo ra trong có có têp *.xll.
uc

16. Copy tệp tin xll (Ở đâu là Tệp HamVBA-AddIn.xll) này đến máy tính bất kỳ và sử dụng thôi.
17. Mở Excel và vào File  Options và thực hiện như hình để chèn file xll vào file Excel là dùng.
uc

Em đang thắc mắc khi đã cài Excel-DNA các thư viện đi kèm rồi em xin hỏi làm sao để load cùng ExcelDna.IntelliSense.xll để hiển thị gợi ý hàm UDF đã viết khi gõ trên Sheet ạ
Khi nhấn vào Fx trên thanh công thức thì có hiện
1680915145116.png
Khi gõ trên Sheet thì không hiện hướng dẫn a, thay vì phải load ExcelDna.IntelliSense.xll thủ công nó sẽ hiện như thế này
1680915228233.png
 
Em đang thắc mắc khi đã cài Excel-DNA các thư viện đi kèm rồi em xin hỏi làm sao để load cùng ExcelDna.IntelliSense.xll để hiển thị gợi ý hàm UDF đã viết khi gõ trên Sheet ạ
Khi nhấn vào Fx trên thanh công thức thì có hiện
View attachment 288648
Khi gõ trên Sheet thì không hiện hướng dẫn a, thay vì phải load ExcelDna.IntelliSense.xll thủ công nó sẽ hiện như thế này
View attachment 288649
Bạn tạo thêm cái Class mới (Ví dụ đặt tên là clsStartUp), bạn tiếp tục thêm thư viện using ExcelDna.IntelliSense; và using ExcelDna.Integration; vào Class vừa mới thêm vào.
Phía sau tên Class bạn thêm vào : IExcelAddIn (Cụ thể public class clsStartUp : IExcelAddIn ). Phía trong cái Class này bạn thêm vào 2 cái void như sau:
Mã:
public void AutoOpen()
        {
            IntelliSenseServer.Install();
        }

 public void AutoClose()
        {
            IntelliSenseServer.Uninstall();
        }
Bây giờ bạn chạy thử và xem kết quả.
 
Khi em làm theo cách trên thì code báo lỗi này ạ

Severity Code Description Project File Line Suppression State
Error The "FindRibbons" task failed unexpectedly.
System.IO.FileNotFoundException: Could not load file or assembly 'VB_ExcelAddInPro, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
File name: 'VB_ExcelAddInPro, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'

Server stack trace:
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.Load(String assemblyString)
at System.UnitySerializationHolder.GetRealObject(StreamingContext context)
at System.Runtime.Serialization.ObjectManager.ResolveObjectReference(ObjectHolder holder)
at System.Runtime.Serialization.ObjectManager.DoFixups()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Remoting.Channels.CrossAppDomainSerializer.DeserializeObject(MemoryStream stm)
at System.Runtime.Remoting.Messaging.SmuggledMethodReturnMessage.FixupForNewAppDomain()
at System.Runtime.Remoting.Channels.CrossAppDomainSink.SyncProcessMessage(IMessage reqMsg)

Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at Microsoft.Build.Framework.ITask.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog]. ExcelDNA_AddInPro
 
Bạn Up cái Project của bạn lên đây tui xem thử.
 
Tui sử dụng C# còn bạn dùng VB.Net thì tui không biết rồi.
 
Em mày mò theo bug đưa ra thì cuối bug có hướng dẫn tạo 1 khoá Registry thì đã hết lỗi trên ạ
Mã:
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].    ExcelDNA_AddInPro
1680942199541.png
 
Web KT
Back
Top Bottom