所以我想从excel文件中的某些框中获取数据,我写了以下代码。
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Excel;
using _Excel = Microsoft.Office.Interop.Excel;
namespace Work
{
class Excel
{
string path = "";
_Application excel = new _Excel.Application();
Workbook wb;
Worksheet ws;
public Excel(string path, int Sheet)
{
this.path = path;
wb = excel.Workbooks.Open(path);
ws = (_Excel.Worksheet)wb.Worksheets[Sheet];
}
}
}
这是我的程序:
using System;
namespace Work
{
class Program
{
static void Main(string[] args)
{
string file_name = "C:\\Users\\Aidan\\Desktop\test.xlsx";
Excel excel = new Excel(string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=No'", file_name), 1);
}
}
}
由于某些原因,我得到以下信息:
Unhandled exception: System.IO.FileNotFoundException: Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Interop.Microsoft.Office.Interop.Excel, Version=1.9.0.0, Culture=neutral, PublicKeyToken=null'. 系统找不到指定的文件。 文件名:'Interop.Microsoft.Office.Interop.Excel, Version=1.9.0.0, Culture=neutral, PublicKeyToken=null'。
有谁知道有什么办法吗?
EDIT.Ive找到了一个新的方法在网上写代码,它仍然给我同样的错误,但有点不同:所以我在网上找到了一种新的写代码的方法, 但它还是给我同样的错误, 但有点不同:
Unhandled exception: System.IO.FileNotF. System.IO.FileNotFoundException: Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'. 系统找不到指定的文件。 文件名:'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'。
这是新的代码(替换了Excel类)。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
namespace Work
{
class Program
{
const int n = 30;
static void getExcelFile()
{
//Create COM Objects. Create a COM object for everything that is referenced
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\Users\Orna\Desktop\test.xlsx");
Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
//iterate over the rows and columns and print to the console as it appears in the file
//excel is not zero based!!
for (int i = 1; i <= n; i++)
{
d[i - 1] = new Date(xlRange.Cells[i, 1].ToString(), xlRange.Cells[i, 2].ToString(), (int)xlRange.Cells[i, 23], (int)xlRange.Cells[i, 4], (int)xlRange.Cells[i, 5], (int)xlRange.Cells[i, 6], (int)xlRange.Cells[i, 7], (int)xlRange.Cells[i, 8], (int)xlRange.Cells[i, 9], (int)xlRange.Cells[i, 10], (int)xlRange.Cells[i, 11] , (int)xlRange.Cells[i, 12], (int)xlRange.Cells[i, 13], (int)xlRange.Cells[i, 14]);
}
//cleanup
GC.Collect();
GC.WaitForPendingFinalizers();
//rule of thumb for releasing com objects:
// never use two dots, all COM objects must be referenced and released individually
// ex: [somthing].[something].[something] is bad
//release com objects to fully kill excel process from running in the background
Marshal.ReleaseComObject(xlRange);
Marshal.ReleaseComObject(xlWorksheet);
//close and release
xlWorkbook.Close();
Marshal.ReleaseComObject(xlWorkbook);
//quit and release
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
}
顺便说一下,不要介意日期类,那是别的东西,问题并不是来自于它。
根据你的描述,你想从excel表中查询数据。
你可以试试下面的代码来解决这个问题。
请先安装nuget包->LightWeightExcelReader。
using LightWeightExcelReader;
using System;
using System.Collections.Generic;
namespace _4._4Excel
{
class Program
{
static void Main(string[] args)
{
Excel.getExcelFile();
}
}
class Excel
{
public static void getExcelFile()
{
//Instatiate a spreadsheet reader by file path:
var excelReader = new ExcelReader(@"\...\test1.xlsx");
//Get a worksheet by index:
var sheetReader = excelReader[0];
var dictionaryOfCellValues = new Dictionary<string, object>();
//Use ReadNext() to read the next cell in the spreadsheet:
while (sheetReader.ReadNext())
{
dictionaryOfCellValues.Add(sheetReader.Address, sheetReader.Value);
}
IEnumerable<object> cellsFromA1ToD4 = sheetReader["A1", "C4"];
foreach (object obj in cellsFromA1ToD4)
Console.WriteLine(obj);
Console.ReadKey();
}
}
}
Excel。
结果。
对于用C#从Excel中读取数据的最简单、最快的方法,我不推荐使用Interop的开销,它太笨重、太繁琐了。
有一个很棒的小库,使用起来超级简单快速,叫做LightWeightExcelReader。https:/github.comChrisHodgesLightweightExcelReader。. 请查看readme文件中的一个简单的例子,以实现你想要的。
如果你需要写和读Excel文件,我建议你使用以下软件。https:/github.comClosedXMLClosedXML 而是