要在 C# 中使用 excel:使用 Interop (Microsoft.Office.Interop.Excel)
IE:
//Added Namespaces
using System.Net;
using System.Net.Mail;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.Reflection;
private Object[,] generateExcel()
{
//Open excel file
Excel.Application objApp = new Excel.Application();
Excel.Workbooks objBooks = objApp.Workbooks;
Excel._Workbook objBook = objBooks.Open("YourPathHere", Missing.Value, Missing.Value, Missing.Value, Missing.Value,Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Excel.Sheets objSheets = objBook.Worksheets;
Excel._Worksheet objSheet = (Excel._Worksheet)objSheets.get_Item(1);
objApp.DisplayAlerts = false;
//Opens excel - remove this line for end product
objApp.Visible = true;
//Data portion
//Your range as the arguement
Excel.Range objRange = (Excel.Range)objSheet.get_Range("A6", "D6");
//Create table
Object[,] tableData = (Object[,])objRange.Value2;
//Close excel - add this in end product
objApp.Application.Quit();
return tableData;
}
这将返回所有 Excel 数据的对象数组。此时您只需执行: table[x,y].ToString();
现在你有了一个字符串来比较你的登录名!祝你好运!