用C#为Excel创建个人宏工作薄
以前一直是用C#直接操作Excel,但是发现性能无比低下,最近发现用Excel中的宏可以高速的完成批处理的功能,于是决定写一个用C#为Excel文件创建宏的程序,工程如下:
代码
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Text;
using Office = Microsoft.Office.Core;
using VBDE = Microsoft.Vbe.Interop;
using Excel = Microsoft.Office.Interop.Excel;
namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string MyFile = Path.GetFullPath(".") + @"sample.xls";
CreateWorkbook(MyFile,GetMacro());
Console.WriteLine("File Saved to " + MyFile);
Console.ReadLine();
}
#region Get Macro
private static string GetMacro()
{
StringBuilder sb = new StringBuilder();
sb.Append("Sub FormatSheet()" + " ");
sb.Append(" msgbox "http://www.cnblogs.com/huangcong/" ");
sb.Append("End Sub");
return sb.ToString();
}
#endregion
#region Create Workbook
private static void CreateWorkbook(string FileName,string Macro)
{
Excel.Application xl = null;
Excel._Workbook wb = null;
Excel._Worksheet sheet = null;
VBDE.VBComponent module = null;
bool SaveChanges = false;
try
{
if (File.Exists(FileName)) { File.Delete(FileName); }
GC.Collect();
xl = new Excel.Application();
xl.Visible = false;
wb = (Excel._Workbook)(xl.Workbooks.Add( Missin补充:软件开发 , C# ,
上一个:C#中结构与类的区别
下一个:C#中COM对象的资源释放问题解决
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,
部分文章来自网络,