日期:2014-05-19  浏览次数:20811 次

c#读取Excel 求解 谢谢各位啦
Excel.Application       excel=   new   Excel.Application();
object       oMissing       =     System.Reflection.Missing.Value;

excel.Application.Workbooks.Open(filePath,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing);  

Excel.Workbook       book   =   excel.Workbooks[1];
Excel.Worksheet       xlSheet=(Excel.Worksheet)book.Worksheets[1];

Label1.Text=(xlSheet.get_Range( "A1 ",Type.Missing)).ToString();
Label1.Text   =   xlSheet.Cells[8,1].ToString();


调试到 Excel.Workbook       book   =   excel.Workbooks[1];这一行时候用监视看显示“book.Worksheets.Count 错误:“book.Worksheets”不存在 "

是怎么回事啊     我的文件路径是绝对路径没有错错误阿,各位大虾Help!!


------解决方案--------------------
sofa!!!
如果可以请将程序发给我,这个代码看不清楚
------解决方案--------------------
我用oledb读取excel,我的方法如下:
using System;
using System.Data;
using System.Data.OleDb;

namespace ytepc_qhb
{
/// <summary>
/// Excel 的摘要说明。
/// </summary>
public class Excel
{
public Excel()
{
//
// TODO: 在此处添加构造函数逻辑
//

}

public DataSet SelectExcel(string path,string condition)
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0; " +
"Data Source= "+path+ "; "+
"Extended Properties=Excel 8.0; ";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter myCommand = new OleDbDataAdapter( "select * FROM [sheet1$] "+condition, strConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet);
return myDataSet;
}

}
}

------解决方案--------------------
using System.Data.OleDb;
using System.Reflection;
using System.Diagnostics;
using System.Collections;
using System.Runtime.InteropServices;

private void GetRow(string FilePath)
{
try
{
//创建Application对象
Microsoft.Office.Interop.Excel.Application xApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
xApp.Visible = false;
//得到WorkBook对象, 可以用两种方式之一: 下面的是打开已有的文件
Microsoft.Office.Interop.Excel.Workbook xBook = xApp.Workbooks._Open(FilePath,
Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//xBook=xApp.Workbooks.Add(Missing.Value);//新建文件的代码
//指定要操作的Sheet,两种方式:
Microsoft.Office.Interop.Excel.Worksheet xSheet = (Microsoft.Office.Interop.Excel.Worksheet)xBook.Sheets[1];
//Excel.Worksheet xSheet=(Excel.Worksheet)xApp.ActiveSheet;
//读取数据,通过Range对象
//Microsoft.Office.Interop.Excel.Range rng1 = xSheet.get_Range( "A1 ", Type.Missing);
//Console.WriteLine(rng1.Value2);

//读取,通过Range对象,但使用不同的接口得到Range
// string ToAddress = " ", ToCompany = " ", ToPerson = " ", ToZip = " ";
s