public class ExcelUtils
{
private static string strcon = "Server=48.12.1.28;initial catalog=NBSFJ_FF;User Id=nbsfj;Password=nbsfj@123;";
public static DataSet ReadExcelToDS(string Path)
{
try
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
if (schemaTable != null)
{
string sheetName = schemaTable.Rows[0][2].ToString().Trim();
//第一张sheet的名称,这个索引为什么是[0][2]也是尝试出来的
if (sheetName != null && sheetName.Length > 0)
{
string strExcel = "select * from [" + sheetName + "]";
OleDbDataAdapter myAdp = new OleDbDataAdapter(strExcel, conn);
DataSet ds = new DataSet();
myAdp.Fill(ds);
DataTable dt = ds.Tables[0];
if (dt.Rows.Count > 0)
{
for (int i = 1; i < dt.Rows.Count; i++)
{
if (!string.IsNullOrEmpty(dt.Rows[i][0].ToString()))
{
string sql = "insert into FF_SysCode values('" + dt.Rows[i][0].ToString().Trim() + "','" + dt.Rows[i][2].ToString().Trim() + "','户籍所在地','" + dt.Rows[i][1].ToString().Trim() + "',0,'" + i + "',1,null,null,null,null)";
ExcelUtils eu = new ExcelUtils();
eu.Exec(sql);
}
}
}
}
} return null;
}
catch (Exception e)
{
//throw new WebAppException(e.Message);
}
return null;
}
public int Exec(string sql)
{
SqlConnection con = new SqlConnection(strcon);
con.Open();
SqlCommand com = con.CreateCommand();
com.CommandText = sql;
int i = com.ExecuteNonQuery();
con.Close();
return i;
}
}