讀取Excel異常定義了過多字段的解決方法

2022-03-26 08:55:31 字數 879 閱讀 2039

/// /// 

從excel檔案匯入資料

///

///

檔案的全路徑

///

excel文件裡的表名稱

public static dataset class_excelsql(string excelstr, string sheetname)

string conn_str = "provider=microsoft.jet.oledb.4.0;data source=" + excelstr + ";extended properties='excel 8.0;hdr=yes;'";

try

catch (oledbexception err_my)

if (myconn_e.state == connectionstate.open)

mycomm_e.dispose();

myadap.dispose();

return mytable;

}

其中mycomm_e.commandtext = "select * from [" + sheetname + "$]";丟擲異常「"定義了過多字段"」

此問題可以有三種解決辦法

辦法一:隱藏excel多餘的列開始-格式-隱藏和取消隱藏-選擇隱藏列

辦法二:把查詢語句改為指定開始到結束列

如:"select * from [" + sheetname + "$a:iu]";其中a:iu代表查詢第a列到第iu列。

辦法三:查詢指定列

如:"select 第一列,第二列 from [" + sheetname + "$]";。

以上三種方法都可以解決「定義了過多字段」

定義了過多字段 Excel

p path excel 檔案目錄 public datatable getdatafromexcel string p path else catch exception e file.delete p path 刪除檔案 return dt string strsql select from s...

excel操作 讀取excel

讀取excel步驟 匯入xlrd模組 開啟excel 讀取需要的sheet頁 通過sheet頁編號或者sheet名字 獲取該sheet頁每行每個字段結果 import xlrd book xlrd.open workbook students.xls 開啟excel sheet book.sheet...

C 讀取Excel,匯出Excel

c 讀取excel 方法一 oledb方式 1 定義連線字串 string strconn provider microsoft.jet.oledb.4.0 data source exce檔案路徑及名字 extended properties excel 8.0 hdr false imex 1 ...