正在尝试将数据从Excel工作表导入到SQL Server表中。代码由用户表单中的按钮激活。但我得到下面的错误时点击它。我使用的是工作机器,我们有Microsoft Excel 365。我已经读到这可能是一个问题,但下面是说它找不到互操作文件吗?我已经读过了,但我不能确定哪些答案与我的具体问题有关。
System.IO.FileNotFoundException:未能加载文件或程序集“websites,Version=15.0.0.0,Culture=neutral,PublicKeyToken= 71e9bce111e9429c”。系统找不到指定的文件。文件名:'office,Version=15.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c'
using Microsoft.Office.Interop.Excel;
namespace FormSqlInsertCsv
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnImport_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application xlApp;
Microsoft.Office.Interop.Excel.Workbook xlWorkbook;
Microsoft.Office.Interop.Excel.Worksheet xlWorksheet;
Microsoft.Office.Interop.Excel.Range xlRange;
int xlRow;
string strFileName;
openFD.Filter = "Excel Office |*.xls; *xlsx";
openFD.ShowDialog();
strFileName = openFD.FileName;
if(strFileName != "")
{
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkbook = xlApp.Workbooks.Open(strFileName);
xlWorksheet = (Worksheet)xlWorkbook.Worksheets["Sheet1"];
//xlRange = xlWorksheet.UsedRange;
xlRange = (Microsoft.Office.Interop.Excel.Range)xlWorksheet.UsedRange;
int i = 0;
for(xlRow = 2; xlRow <= xlRange.Rows.Count; xlRow++)
{
i++;
dgvEmployees.Rows.Add(i, xlRange.Cells[xlRow, 1].ToString(), xlRange.Cells[xlRow, 2].ToString(), xlRange.Cells[xlRow, 3].ToString(), xlRange.Cells[xlRow, 4].ToString(), xlRange.Cells[xlRow, 5].ToString(), xlRange.Cells[xlRow, 6].ToString());
}
xlWorkbook.Close();
xlApp.Quit();
}
}
}
}
1条答案
按热度按时间yyhrrdl81#
右键单击引用并从以下位置添加Office.dll引用:C/Windows/assembly/GAC_MSIL/office/15.0.0.0__71e9bce111e9429c/Office.dll