try
{
//打开Excel的数据集连接
DataSet ds = new DataSet();
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + ExcelFileFullName + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcelSql = "";
OleDbDataAdapter myCommand = null;
strExcelSql = string.Format("select * from [{0}]", sheetNames[0]); //sheetNames[0]为excel中工作薄的名称,如名称中不带$,则需在[]中加上$,如:[Customers$]
myCommand = new OleDbDataAdapter(strExcelSql, strConn);
myCommand.Fill(ds, sheetNames[0]);//填充sheet表单中数据到Dataset中
DataTable table = ds.Tables[0]; //取Dataset中的第一个表作为数据源:可根据个人需求更改
if (table == null || table.Rows == null || table.Rows.Count == 0)
return ;
//当前检查字段英文名
string fieldEnName = string.Empty;
//当前检查字段是否可空
bool isCanNull = true;
//当前检查字段的长度
int fieldLength = 0;
foreach (DataRow dr in fieldSTable.Rows)//通过字段表来检查:外层循环
{
fieldEnName = dr["Fieldenname"].ToString();
isCanNull = Convert.ToBoolean(dr["Iscannull"]);
fieldLength = Convert.ToInt32(dr["Fieldlength"]);
if (table.Columns[fieldEnName] != null)//判断Excel导出的表中是否存在指定的列
{
for (int row = 1; row < table.Rows.Count; row++)
{
//检查值是否为空
if (!isCanNull)
{
object value = table.Rows[row][fieldEnName];
if (value == null)
msgList.Add("不可空字段" + fieldEnName + ",值为空!");
}
}
}
else
{
msgList.Add("字段" + fieldEnName + "不存在!");
result = false;
}
}
}
catch (Exception ex)
{
}
return result;