使用Office Interop读取Excel文件时遇文件找不到问题求助
解决C#读取Excel文件时的“找不到文件”问题
先查路径相关问题
- 路径转义要正确:C#里字符串的反斜杠是转义字符,要么把路径写成
"C:\\Users\\aquinn\\Desktop\\Read rows test.xlsx",要么加@符号简化成@"C:\Users\aquinn\Desktop\Read rows test.xlsx",没转义的话路径会被解析错,自然找不到文件。 - 确认文件真的存在:手动打开这个路径,核对文件名(包括空格)完全一致,别打错字,比如把“test”写成“text”。
- 权限或锁定问题:右键以管理员身份运行程序试试,或者检查任务管理器里有没有残留的
EXCEL.EXE进程——之前调试没正常关闭的话,Excel会占用文件,导致读不了。
再排查PIA/Interop组件问题
- 版本要匹配:别混用不同版本的Interop包,直接用NuGet安装
Microsoft.Office.Interop.Excel,比手动引用本地PIA更稳妥,避免和你电脑上的Excel版本不兼容。 - 工作表名称要准确:确认Excel里的工作表确实叫“Sheet1”,包括大小写、空格,别把“Sheet1”写成“sheet1”或者“Sheet 1”。
- 必须释放Excel资源:代码里用完Excel对象后一定要关闭工作簿、退出Excel进程,不然残留的进程会锁死文件,下次读取就报错。
修正后的代码示例
主程序(修复路径写法)
class Program { static void Main(string[] args) { // 用@符号避免转义问题 string filePath = @"C:\Users\aquinn\Desktop\Read rows test.xlsx"; string sheetName = "Sheet1"; ExcelReader reader = new ExcelReader(); var rows = reader.ReadRowsFromExcel(filePath, sheetName); foreach (var row in rows) { Console.WriteLine(string.Join(", ", row)); } } }
ExcelReader类(添加资源释放和错误捕获)
using Microsoft.Office.Interop.Excel; using System.Collections.Generic; public class ExcelReader { public List<List<string>> ReadRowsFromExcel(string filePath, string sheetName) { Application excelApp = null; Workbook workbook = null; Worksheet worksheet = null; List<List<string>> rows = new List<List<string>>(); try { excelApp = new Application(); // 打开文件时加只读模式,减少锁定问题 workbook = excelApp.Workbooks.Open(filePath, ReadOnly: true); worksheet = (Worksheet)workbook.Sheets[sheetName]; Range usedRange = worksheet.UsedRange; int rowCount = usedRange.Rows.Count; int colCount = usedRange.Columns.Count; for (int i = 1; i <= rowCount; i++) { List<string> row = new List<string>(); for (int j = 1; j <= colCount; j++) { var cellValue = usedRange.Cells[i, j].Value; row.Add(cellValue?.ToString() ?? string.Empty); } rows.Add(row); } } catch (System.Exception ex) { Console.WriteLine($"读取失败:{ex.Message}"); } finally { // 强制释放Excel资源,避免进程残留 if (workbook != null) { workbook.Close(false); System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); } if (excelApp != null) { excelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); } } return rows; } }
换个思路:用无PIA的库
如果Interop方式始终有问题,试试EPPlus(只支持xlsx格式),不需要依赖本地Excel,NuGet安装后代码更简单:
using OfficeOpenXml; using System.Collections.Generic; using System.IO; public class ExcelReader { public List<List<string>> ReadRowsFromExcel(string filePath, string sheetName) { List<List<string>> rows = new List<List<string>>(); FileInfo fileInfo = new FileInfo(filePath); using (ExcelPackage package = new ExcelPackage(fileInfo)) { ExcelWorksheet worksheet = package.Workbook.Worksheets[sheetName]; int rowCount = worksheet.Dimension.Rows; int colCount = worksheet.Dimension.Columns; for (int i = 1; i <= rowCount; i++) { List<string> row = new List<string>(); for (int j = 1; j <= colCount; j++) { row.Add(worksheet.Cells[i, j].Text ?? string.Empty); } rows.Add(row); } } return rows; } }
内容的提问来源于stack exchange,提问作者Alex Quinn
相关产品推荐
相关产品推荐

