.NET-10. 其他-VSTO+VBA


VSTO+VBA


前言

主要用于Excel插件。


VSTO 外接程序介绍:

Excel、Word、PowerPoint、Project、Visio等等Office应用程序

相对简单

VSTO参考链接:

知乎教程官网参考Api

VSTO 例子:

csharp 复制代码
1. 下标从1开始
public static int GenerateRandomSeed() => Math.Abs((int)DateTime.Now.Ticks);//根据时间生成序列号
public Excel.Application excelApp;//Excel应用程序变量
excelApp = Globals.ThisAddIn.Application;//构造函数里面
Excel.Workbook wbk = excelApp.ActiveWorkbook;//wbk为活动工作簿
wbk.Path//当前路径
Excel.Sheets sheets = excelApp.ActiveWorkbook.Worksheets;//wbk为活动工作表
TaFirstList = ((dynamic[,])sheet.Range["A1", "N1"].Value).OfType<string>().ToList();
TaFirstList = ((dynamic[,])sheet.Range["A1", "N1"].Value).OfType<dynamic>().ToList();//为null的也算一个
//赋值:
var firstlist = new string[]{1,2,3,4,5,6};
Excel.Workbook Nowbook = excelApp.Application.Workbooks.Add();//新增一个工作薄
Nowbook.Sheets[1].Name = "";
Nowbook.Sheets[1].Range["A1"].Resize[1, firstlistTPlm.Length] = firstlistTPlm;
var str = wbk.Path + "\\" + wbk.Name.Split('.').First() + "test.xlsx";
Nowbook.Sheets[1].Cells[1, 2].Value = flag;
if (File.Exists(str)) File.Delete(str);
Nowbook.SaveAs(str);

//询问框
//添加一个询问框:
 public partial class InputDialog : Form
{
    public InputDialog()
    {
        InitializeComponent();
    }
    public delegate void TextEventHandler(string strText);

    public TextEventHandler TextHandler;

    private void btnOk_Click(object sender, EventArgs e)
    {
        if (null != TextHandler)
        {
            TextHandler.Invoke(name.Text);
            DialogResult = DialogResult.OK;
        }
    }

    private void txtString_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (Keys.Enter == (Keys)e.KeyChar)
        {
            if (null != TextHandler)
            {
                TextHandler.Invoke(name.Text);
                DialogResult = DialogResult.OK;
            }
        }
    }

}
//调用类
public static class InputDialogWin
{
    public static DialogResult Show(out string strText)
    {
        string strTemp = string.Empty;

        InputDialog inputDialog = new InputDialog();
        inputDialog.TextHandler = (str) => { strTemp = str; };

        DialogResult result = inputDialog.ShowDialog();
        strText = strTemp;

        return result;
    }
}
//使用:
DialogResult dialogResult= InputDialogWin.Show(out currentName);
if (dialogResult == DialogResult.Cancel) return;

VBA:

参考链接:

语言基础
教程一

csharp 复制代码
1. 开发工具=>VBA编辑器=>视图 - 立即窗口:
? 1+1 ? len("qiou.eu") //问号是一定的
csharp 复制代码
VB 基本语法部分
Sub/Function/Class/Module 【1】
数据类型 【3】
String, Number, Date以及常用方法
Array
流程控制 【2】
If While Foreach Fori Switch
Goto, With
异常处理 【1】
On Error xx
抛出及捕获异常
Excel对象模型
Range 【2】
Worksheet, Workbook, Application 【2】
不同Office产品之间的交互 (*视反响决定) 【2】
MS Office 常用内置库
FileSystemObject 以及基本IO 【2】
Dictionary 以及容器操作 【2】
RegExp 以及 高阶字符串处理技巧 【2】
JavaScript-Engine (*视反响决定) 【2】
Excel事件模型以及用户表单
Workbook, Worksheet事件模型 【1】
Excel用户表单 【2】
面向对向编程,定义自己的Class文件
简单的OOP(Object Oriented Programming)介绍 【2】
实例 (*安利我自己的 Excel Dicts/Lists库, 视反响决定) 【5】

//变量强制申明, VBA版的"use strict;"
//此段VBA人代码中的程序入口main
Option Explicit 
Public Sub main()   
    helloWorld   
End Sub
' pop out the message
Private Function helloWorld() 
    MsgBox "Hello World!" 
End Function
---
Option Explicit
Public Sub main()  
    Debug.Print getLongerStringLen("qiou.eu", "sgfxq") 
End Sub
' return the length of the longer string
Private Function getLongerStringLen(str1 As String, str2 As String) As String
    getLongerStringLen = IIf(Len(str1) > Len(str2), str1, str2)
End Function


//变量类型定义
Dim productName As String
Dim length As Integer
Dim reg As RegExp
相关推荐
唐青枫1 小时前
看懂 IL 的关键:C#.NET 栈机执行模型实战拆解
c#·.net
我才是银古1 小时前
从零构建 Windows 桌面 RPA 框架:Go 与 .NET 的跨语言协奏
c#·go·rpa
console.log('npc')2 小时前
网页移动端UI/UX Pro Max - 设计智能skill
前端·人工智能·ui·ux
逝水无殇3 小时前
C# 正则表达式详解
开发语言·后端·正则表达式·c#
朱永博3 小时前
使用Onnruntime实现Padim/Patchcore推理
开发语言·c#
吴可可1235 小时前
C# CAD二次开发添加带按钮的窗口
c#
工业HMI实战笔记5 小时前
HMI性能优化技巧:告别卡顿,打造流畅体验
前端·ui·性能优化·自动化·交互
解局易否结局5 小时前
鸿蒙多设备形态适配实战:从手表到车机的差异化 UI 设计
ui·华为·harmonyos
ltqvibe6 小时前
TokUI怎么让AI输出从文字墙变成可交互UI
人工智能·ui·交互
铅笔侠_小龙虾6 小时前
Rust 学习(8)-切片类型
学习·rust·c#