学习CAD二开需要与EXCEL交互,从Nuget下了netOffice库。
学习到以下代码,但是总是出错,不知道怎么处理,错误在
“Excel.Worksheet sheet =(Excel.Worksheet) book.Worksheets[1];”这一行,
错误的原因:NetOffice.NetOfficeException:“Factory is not initialized with NetOffice assemblies.”
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = NetOffice.ExcelApi;
namespace _02创建图形
{
public class ExcelDemo
{
[CommandMethod("ExtToExcelDemo")]
public void ExtToExcelDemo()
{
Excel.Application excelApp = new Excel.Application();
Excel.Workbook book = excelApp.Workbooks.Add();
Excel.Worksheet sheet =(Excel.Worksheet) book.Worksheets[1];
sheet.Cells[1, 1].Value = "吸湿性";
book.Save();
excelApp.Quit();
excelApp.Dispose();
}
}
}