[重奖100分]帮我看一下,读Excel问题
public static String[] GetExcelSheetNames(string excelFile)
{
OleDbConnection objConn = null;
System.Data.DataTable dt = null;
try
{
//string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + excelFile + ";Extended Properties='Excel 8.0; HDR=NO; IMEX=1'"; //此连接只能操作Excel2007之前(.xls)文件
string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + excelFile + ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'"; //此连接可以操作.xls与.xlsx文件
objConn = new OleDbConnection(strConn);
objConn.Open();
dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return null;
}
String[] excelSheets = new String[dt.Rows.Count];
int i = 0;
foreach (DataRow row in dt.Rows)
{
excelSheets[i] = row["TABLE_NAME"].ToString();
i++;
}
return excelSheets;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
finally
{
if (objConn != null)
{
objConn.Close();
objConn.Dispose();
}
if (dt != null)
{
dt.Dispose();
}
}
}
public static DataTable GetExcelToDataTableBySheet(string FileFullPath, string SheetName)
{
//string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + FileFullPath + ";Extended Properties='Excel 8.0; HDR=NO; IMEX=1'"; //此连接只能操作Excel2007之前(.xls)文件
try
{
string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + FileFullPath + ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'"; //此连接可以操作.xls与.xlsx文件
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataSet ds = new DataSet();
OleDbDataAdapter odda = new OleDbDataAdapter(string.Format("SELECT * FROM [{0}]", SheetName), conn); //("select * from [Sheet1$]", conn);
odda.Fill(ds, SheetName);
conn.Close();
return ds.Tables[0];
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
}
调用
string[] abc = GetExcelSheetNames(Server.MapPath("/accessories/xls/" + pname));
loadExcelSheet(GetExcelToDataTableBySheet(Server.MapPath("/accessories/xls/a" + pname), abc[0]));
为什么会出现下述错误?????
[NullReferenceException: 未将对象引用设置到对象的实例。]
system_dingcang_loadExcel.Button1_Click(Object sender, EventArgs e) +630
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
各位大侠,谁能给我解答一下,重奖100分,谢谢啦
下跪 --------------------编程问答-------------------- 1,Server.MapPath("/accessories/xls/" + pname)打印出来看什么,存在吗? 还有Server.MapPath("/accessories/xls/a" + pname)存在吗?
2,abc.Length>0才能继续执行 abc[0] --------------------编程问答-------------------- --------------------编程问答-------------------- 只是一段,NullReferenceException: 未将对象引用设置到对象的实例,这一般是没有初始实例化
补充:.NET技术 , C#