VSTO的简单用法
一直听说vsto这个名词,还真不知道什么意思,今天了解了一下,原来他的全程是Visual Studio Tools For Office,说他是VBA的替身(VBA俺也不是很懂),刚才上网查询做了个例子,我把代码贴出来
第一步:导入DLL
要用到第三方插件,路径在:X:\Program Files \Microsoft Visual Studio [10.0|11.0|12.0]\Visual Studio Tools for Office\PIA\Office[13|14|15]\Microsoft.Office.Interop.Excel.dll
第二步:代码实现
1 string excelFilePath = string.Format("{0}ExcelTest1.xlsx", AppDomain.CurrentDomain.BaseDirectory); 2 using (ExcelHandler handler = new ExcelHandler(excelFilePath, false)) 3 { 4 handler.OpenOrCreate(); 5 handler.DeleteWorksheet("TestSheet1"); 6 //创建一个Worksheet 7 Worksheet sheet = handler.AddWorksheet("TestSheet1"); 8 //A1-E5 9 Range range = handler.GetRange(sheet, 1, 1, 5, 5); 10 handler.SetRangeFormat(range); 11 range.Font.Bold = true;//加粗 12 System.Data.DataTable table = new System.Data.DataTable(); 13 table.Columns.AddRange(new DataColumn[] { new DataColumn("列1"), new DataColumn("列2"), new DataColumn("列3") }); 14 Random random = new Random(20); 15 for (int i = 0; i < 10; i++) 16 { 17 table.Rows.Add(random.Next(100), random.Next(100), random.Next(100)); 18 } 19 //导入数据 20 handler.ImportDataTable(sheet, "导入表格", true, new string[] { "列1", "列2", "列3" }, 1, 1, table); 21 handler.Save(); 22 }