.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 小时前
Qt核心机制:信号与槽详解
开发语言·c++·qt·ui·qt6.3
国家一级摸鱼选手8 小时前
[Unity]几何碰撞检测:SAT 分离轴定理详解(从数学原理到 C# 实现)
unity·c#·游戏引擎·分离轴定理(sat)·多边形相交
Jeff-Tsai9 小时前
Day 22:XML、JSON 与 MessagePack 的分工
.net·架构设计·erp·definition-driven
汤姆宝库21 小时前
BPE分词器的完整实现:字节对合并与词表构建
开发语言·人工智能·c#
风云1 天前
从 NLite 到 Vane.Container 1.0:压箱十六年的自研 DI 容器开源
.net·di·mini 容器·nlite
Risehuxyc1 天前
C# 发展历史
开发语言·c#
kyle~1 天前
C# --- 反射
开发语言·c#
FRing031 天前
vue2干货总结10(json-server、vant-ui、postcss插件、axios二次封装、图形验证码、toast轻提示)
开发语言·前端·vue.js·ui·json·vue2·postcss
Jeff-Tsai1 天前
Day 21:业务逻辑定制与 Plugin 的四个时点
.net·架构设计·erp·definition-driven