.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
相关推荐
叫我少年11 小时前
C#基础数据类型
c#·数据类型
Larry_Yanan11 小时前
QML面试常见问题(一)QML中组件呈现方式的方法有哪些
开发语言·c++·qt·ui·面试
唐青枫12 小时前
C#.NET YARP 跨域配置详解:网关统一处理 CORS
c#·.net
测试员周周14 小时前
【Appium 系列】第12节-智能路由 — API测试 vs UI 测试的自动选择
开发语言·人工智能·python·功能测试·ui·appium·测试用例
程序leo源14 小时前
Qt信号与槽深度详解
c语言·开发语言·数据库·c++·qt·c#
BU摆烂会噶16 小时前
【LangGraph】节点内调用与状态隔离
android·人工智能·python·ui·langchain·人机交互
努力努力再努力wz16 小时前
【C++高阶数据结构系列】:时间轮定时器详解:原理分析与代码实现,带你从零手撕时间轮!(附时间轮的实现源码)
c语言·开发语言·数据结构·c++·qt·算法·ui
ZC跨境爬虫16 小时前
跟着 MDN 学 HTML day_58:(构建行星数据表——HTML表格高级实战指南)
前端·javascript·ui·html·音视频
BU摆烂会噶17 小时前
【LangGraph】作为节点添加与状态共享
android·人工智能·python·ui·langchain·人机交互
ZC跨境爬虫19 小时前
跟着 MDN 学 HTML day_60:(表单与按钮技能测试实战)
服务器·前端·javascript·数据库·ui·html