110,534
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Data;
namespace ScoreReportSystem
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
// TODO: Implement Functionality Here
test();
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
private static void test()
{
ExcelReader er=new ExcelReader("D:\\score2009");
System.Data.DataTable dt=er.getFirstSheetData();
PrintTable(dt);
}
private static void PrintTable(DataTable table,int top=0)
{
if(top==0)
{
top=table.Rows.Count;
}
int count=table.Columns.Count;
for(int row=0;row<top;row++)
{
for(int i=0;i<count;i++)
{
Console.Write(table.Rows[row][i].ToString()+"\t");
}
Console.WriteLine();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
namespace ScoreReportSystem
{
/// <summary>
/// Description of MyClass.
/// </summary>
public class ExcelReader
{
private string _fileName;
public ExcelReader(string fileName)
{
_fileName=fileName;
}
public DataTable getFirstSheetData()
{
string cnstr=string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES;IMEX=1';",_fileName);
string selectCommandText="select top 5 * from [$A1:R65536]";
OleDbDataAdapter oda=new OleDbDataAdapter(selectCommandText,cnstr);
DataTable dt=new DataTable();
oda.Fill(dt); //出错的地方
return dt;
}
}
}
OleDbDataAdapter oda=new OleDbDataAdapter(selectCommandText,cnstr);
cnstr是string 当然错了
要
OleDbConnection connetion = new OleDbConnection(cnstr);
connetion.Open();
OleDbDataAdapter oda=new OleDbDataAdapter(selectCommandText,connetion);