您的位置:首页 > 数据库

C#中excel导入sql

2015-06-11 19:06 288 查看
using Microsoft.Office.Interop.Excel;

public int ledinExcel(string file, object sender, EventArgs e)
{
//try
//{
System.Data.DataTable dt = xsldata(file);
int errorcount = 0;
int insertcount = 0;
//int updatecount = 0;

//string strcon = "server=ROCKEN;database=checkatt;uid=sa;pwd=000123";
//SqlConnection conn = new SqlConnection(strcon);
//conn.Open();
for (int i = 0; i < dt.Rows.Count; i++)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into checkatt.DBO.att_table (Person_num,Per_name,Work_att,Up1,Off2,Up3,Off4,Up5,Off6)");
strSql.Append(" VALUES (@Person_num,@Per_name,@Work_att,@Up1,@Off2,@Up3,@Off4,@Up5,@Off6)");
SqlParameter[] praexcel = {
new SqlParameter("@Person_num", SqlDbType.Int),
new SqlParameter("@Per_name", SqlDbType.NVarChar,50),
new SqlParameter("@Work_att", SqlDbType.Date),
new SqlParameter("@Work_lable",  SqlDbType.NVarChar,50),
new SqlParameter("@Up1", SqlDbType.Time),
new SqlParameter("@Off2", SqlDbType.Time),
new SqlParameter("@Up3", SqlDbType.Time),
new SqlParameter("@Off4", SqlDbType.Time),
new SqlParameter("@Up5", SqlDbType.Time),
new SqlParameter("@Off6", SqlDbType.Time)};
praexcel[3].Precision = 0;
praexcel[4].Precision = 0;
praexcel[5].Precision = 0;
praexcel[6].Precision = 0;
praexcel[7].Precision = 0;
praexcel[8].Precision = 0;

praexcel[0].Value = dt.Rows[i][0];
praexcel[1].Value = dt.Rows[i][1].ToString();
praexcel[2].Value = Convert.ToDateTime(dt.Rows[i][2].ToString());
praexcel[3].Value = dt.Rows[i][3];
if (dt.Rows[i][4].ToString().Length == 0)
{
praexcel[4].Value = DBNull.Value;
}
else if (dt.Rows[i][4].ToString().Length > 5)
{
if (string.IsNullOrEmpty(dt.Rows[i][4].ToString().Trim()))
{
praexcel[4].Value = DBNull.Value;
}
else
{
praexcel[4].Value = dt.Rows[i][4].ToString().Trim();
}

}
else
{
praexcel[4].Value = dt.Rows[i][4];
}
//
if (dt.Rows[i][5].ToString().Length == 0)
{
praexcel[5].Value = DBNull.Value;
}
else if (dt.Rows[i][5].ToString().Length > 5)
{
if (string.IsNullOrEmpty(dt.Rows[i][5].ToString().Trim()))
{
praexcel[5].Value = DBNull.Value;
}
else
{
praexcel[5].Value = dt.Rows[i][5].ToString().Trim();
}
}
else
{
praexcel[5].Value = dt.Rows[i][5];
}
//
if (dt.Rows[i][6].ToString().Length == 0)
{
praexcel[6].Value = DBNull.Value;
}
else if (dt.Rows[i][6].ToString().Length > 5)
{
if (string.IsNullOrEmpty(dt.Rows[i][6].ToString().Trim()))
{
praexcel[6].Value = DBNull.Value;
}
else
{
praexcel[6].Value = dt.Rows[i][6].ToString().Trim();
}
}
else
{
praexcel[6].Value = dt.Rows[i][6];
}
//
if (dt.Rows[i][7].ToString().Length == 0)
{
praexcel[7].Value = DBNull.Value;
}
else if (dt.Rows[i][7].ToString().Length > 5)
{
if (string.IsNullOrEmpty(dt.Rows[i][7].ToString().Trim()))
{
praexcel[7].Value = DBNull.Value;
}
else
{
praexcel[7].Value = dt.Rows[i][7].ToString().Trim();
}
}
else
{
praexcel[7].Value = dt.Rows[i][7];
}
//
if (dt.Rows[i][8].ToString().Length == 0)
{
praexcel[8].Value = DBNull.Value;
}
else if (dt.Rows[i][8].ToString().Length > 5)
{
if (string.IsNullOrEmpty(dt.Rows[i][8].ToString().Trim()))
{
praexcel[8].Value = DBNull.Value;
}
else
{
praexcel[8].Value = dt.Rows[i][8].ToString().Trim();
}
}
else
{
praexcel[8].Value = dt.Rows[i][8];
}
//
if (dt.Rows[i][9].ToString().Length == 0)
{
praexcel[9].Value = DBNull.Value;
}
else if (dt.Rows[i][9].ToString().Length > 5)
{
if (string.IsNullOrEmpty(dt.Rows[i][9].ToString().Trim()))
{
praexcel[9].Value = DBNull.Value;
}
else
{
praexcel[9].Value = dt.Rows[i][9].ToString().Trim();
}
}
else
{
praexcel[9].Value = dt.Rows[i][9];
}
//if (dt.Rows[i][5].ToString().Length == 0) { praexcel[5].Value = DBNull.Value; } else { praexcel[5].Value = dt.Rows[i][5]; }
//if (dt.Rows[i][6].ToString().Length == 0) { praexcel[6].Value = DBNull.Value; } else { praexcel[6].Value = dt.Rows[i][6]; }
//if (dt.Rows[i][7].ToString().Length == 0) { praexcel[7].Value = DBNull.Value; } else { praexcel[7].Value = dt.Rows[i][7]; }
//if (dt.Rows[i][8].ToString().Length == 0) { praexcel[8].Value = DBNull.Value; } else { praexcel[8].Value = dt.Rows[i][8]; }
//if (dt.Rows[i][9].ToString().Length == 0) { praexcel[9].Value = DBNull.Value; } else { praexcel[9].Value = dt.Rows[i][9]; }
//praexcel[4].Value = dt.Rows[i][4];dt.Rows[i][9].ToString().Trim()
//praexcel[5].Value = dt.Rows[i][5];
//praexcel[6].Value = dt.Rows[i][6];
//praexcel[7].Value = dt.Rows[i][7];
//praexcel[8].Value = dt.Rows[i][8];
//praexcel[9].Value = dt.Rows[i][9];
//MessageBox.Show(dt.Rows[i][1].ToString() + dt.Rows[i][2].ToString());
try
{
if (SqlHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), praexcel) > 0)
{

insertcount++;

}
else
{
errorcount++;
}
}
catch (Exception ex)
{
MessageBox.Show("导入成功:" + insertcount.ToString() + "条数据;    共" + dt.Rows.Count.ToString() + "条记录");
return insertcount;
}
}
MessageBox.Show("导入成功:"+insertcount.ToString()+"条数据;    失败"+errorcount.ToString());
return insertcount;
//}
//catch (Exception ex)
//{
//Console.Write(ex.Message);
//    return insertcount;
//}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: