C 呼叫Excel的巨集

2021-07-07 02:47:24 字數 1443 閱讀 1976

用到了三個元件

using excel = microsoft.office.interop.excel;

using office = microsoft.office.core;

using vbide = microsoft.vbe.interop;

因為涉及到大批量的excel檔案的處理

首先 先要把文字檔案中儲存的vba函式倒入到excel中 手動倒入,假設有幾千個excel 將會窒息

假設 "1.txt"檔案內容為

sub deletelink()

cells.hyperlinks.delete

end sub

將文字檔案寫到乙個str中 此str比較特殊,因為要按照vba的格式寫 我們必須考慮回車換行 再者 假如檔案中存在雙引號,我們還要特殊處理

streamreader reader=new streamreader("c:\\code.txt",system.text.encoding.getencoding("gb2312"));

string str = reader.readline();

string result = str + "\r\n";

while ((str=reader.readline())!=null)

result = result + str;

}textbox1.text = result;

"\r", "\"+");

reader.close();

處理完後 就要倒入巨集,然後執行了

code

excel.workbook workbook = null;

vbide.vbcomponent vbcomponent = null;

string strcode;

object omissing = system.reflection.missing.value;

object missing = system.reflection.missing.value;

vbcomponent = workbook.vbproject.vbcomponents.add(vbide.vbext_componenttype.vbext_ct_stdmodule);

#region

strcode = "sub ceshi()\r\n" +

"cells.hyperlinks.delet\r\n"+

"end sub";

#endregion

vbcomponent.codemodule.addfromstring(strcode);

trycatch

vbcomponent = null;

workbook = null;

gc.collect();

excel巨集的用法

在不少時間excel中並沒有一些我們想要的函式,這時候我們可以在 xls x 的中定義巨集,定義了巨集後需要注意兩項問題 我這裡希望對乙個字串拆分,比如 希望將a列中 1,2,3,10,11 的資料拆分為c,d,e,f,g 5列。此時在excel的選單 工具 巨集 visual basic 編輯器 ...

Excel巨集教程

excel巨集教程 巨集的介紹與基本使用 1 1 a 6 5 避免迴圈引用 在上述公式賦值過程中,應避免在公式中引用被賦值的單元格,防止迴圈引用錯誤。6 新增批註 可按如下方法格給單元格新增批註 dim 批註文字 as string 批註文字 批註示例 準備批註文字 activecell.addco...

C 操作Excel 呼叫Excel模板匯出 一

1.通過呼叫excel模板,替換替換其中要替換的單無格。下面直接看 using system using system.collections.generic using system.linq using system.web using system.web.ui using system.we...