我需要一种通过c#读取Excel文件的方法。我找到了一个例子https://coderwall.com/p/app3ya/read-excel-file-in-c
我创建了一个独立程序来测试它,它按照我的预期工作。我将代码作为子程序添加到现有程序中并遇到错误。
第一个错误,"无法隐式转换类型'对象'到Microsoft.Office.Interop.Excel._Worksheet。存在显式转换。"
第二个错误,"'对象'不包含' Value2'"。
的定义我修复了第一个错误,将(Excel._Worksheet)添加到xlWorkbook.Sheets [1];
我无法找出Value2上的第二个,需要你的帮助:"错误98'对象'不包含' Value2'的定义没有扩展方法' Value2'接受类型'对象'的第一个参数。可以找到(你错过了使用指令或汇编引用吗?)"
这是我的代码:
using System;
using System.Collections.Generic;
using System.Collections;ArrayList, ListBox, ComboBox, etc.
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XView.Properties;
using System.Reflection;
using ZedGraph;
using System.IO;
using System.Management;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
private void readExcelFile(ArrayList al)
{
string rowItems = string.Empty;
//Create COM Objects. Create a COM object for everything that is referenced
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"W:\acad\x-pak\XPak setting_tool_charts.xlsx");
Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
//iterate over the rows and columns and print to the console as it appears in the file - Excel is not zero based!!
//---start at i = 4 since in Rick's Excel file, that's where the data begins!
for (int i = 4; i <= rowCount; i++)
{
rowItems = string.Empty;
for (int j = 1; j <= colCount; j++)
{
//new line
if (j == 1)
Console.Write("\r\n");
//write the value to the console
if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null)
{
//Console.Write(xlRange.Cells[i, j].Value2.ToString() + "\t");
if (j == 1) //new line
rowItems = xlRange.Cells[i, j].Value2.ToString();
else
rowItems += "^" + xlRange.Cells[i, j].Value2.ToString();
}
}
al.Add(rowItems);
}
//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);
}
答案 0 :(得分:4)
我认为它与Microsoft.Office.Interop.Excel版本有关。我之前从未使用过代码块来解决同样的错误。我认为这对他有用。
object _xVal;
_xVal= ((Excel.Range)xlWorksheet.Cells[i, j]).Value2;
答案 1 :(得分:1)
此问题的原因是目标框架!它使用的是.Net 3.5,因为主程序是在2008年创建的。这个子程序今天被添加,它假设&#34;嵌入互操作类型=真&#34; (参考资料:Microsoft.Office.Interop.Excel)。
我转到了Project Properties并将Target Framework设置为&#34; .Net Framework 4 Client Profile&#34;。这产生了43个错误(无法记住错误名称,但表示可能需要添加对Microsoft.CSharp.dll的引用)。我添加了Microsoft.CSharp引用,所有错误都消失了!
答案 2 :(得分:0)
我尝试了相同的示例教程,并遇到了相同的问题。我通过以下方法解决了该问题:
首先,创建一个像这样的变量:
var sno = (Excel.Range)xlWorksheet.Cells[i,1];
然后像这样访问Value2
属性:
string serialno = sno.Value2.ToString();
我正在使用.net Framework 4.5