导入Excel小工具
昨天作了一个小工具,将Excel中的数据导入数据库。
1.用C#语言实现。
2.asp.net是一个媒介。
前台代码略。。。
Default.aspx.cs代码:
public partial class _Default : System.Web.UI.Page
{
#region Attribute
//DataAccess da = new DataAccess();
DataSet ds;
SqlHelp sh = new SqlHelp();
#endregion
#region Methods
//PageLoad
protected void Page_Load(object sender, EventArgs e)
{
//if (!IsPostBack)
//{
// DropDownListBind();
//}
}
//DridView Create Columns
private DataTable CreateColumn(DataSet ds)
{
this.GridView1.Columns.Clear();
DataTable dt = ds.Tables[0];
if (dt != null)
{
if (dt.Rows.Count > 0)
{
for (int i = 1; i <= dt.Columns.Count; i++)
{
BoundField bf = new BoundField();
bf.DataField = "F" + i; //This's the attribute column of table
bf.HeaderText = dt.Rows[0][i-1].ToString();
GridView1.Columns.Add(bf);
}
dt.Rows.Remove(dt.Rows[0]); //Remove the select row
}
}
return dt;
}
//Bind DropDownList
//public void DropDownListBind()
//{
// ds = sh.DropDownListHelp();
// DropDownList1.DataSource = ds.Tables[0];
// DropDownList1.DataValueField = ds.Tables[0].Columns[1].ColumnName;
// DropDownList1.DataTextField = ds.Tables[0].Columns[1].ColumnName;
// DropDownList1.DataBind();
// DropDownList1.Items.Insert(0,new ListItem("--Choose Table--",""));
//}
/// <summary>
/// Display Excel Data
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button_Input_Click(object sender, EventArgs e)
{
string filePath = TextBox_FileName.Text.ToString();
string sheetName = "Sheet1$";
if (filePath != "")
{
ds = sh.InputExeclFileHelp(filePath, sheetName);
if (ds != null)
{
DataTable dt = CreateColumn(ds);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
else
{
ClientScript.RegisterStartupScript(typeof(Page),"a","<script type='text/javascript'>alert('Please choose file!');</script>");
}
}
//Input the data to database
protected void Button_Output_Click(object sender, EventArgs e)
{
string number = "";
string name = "";
string sex = "";
string classNum = "";
string profession = "";
string school = "";
int m = 0;
if (GridView1.Columns.Count > 0)
{
for (int i = 0; i < GridView1.Rows.Count;i++ )
{
number = GridView1.Rows[i].Cells[0].Text.ToString();
name = GridView1.Rows[i].Cells[1].Text.ToString();
sex = GridView1.Rows[i].Cells[2].Text.ToString();
classNum = GridView1.Rows[i].Cells[3].Text.ToString();
profession = GridView1.Rows[i].Cells[4].Text.ToString();
school = GridView1.Rows[i].Cells[5].Text.ToString();
m += sh.InputDataToDatabase(number,name,sex,classNum,profession,school);
}
if (m == GridView1.Rows.Count)
{
ClientScript.RegisterStartupScript(typeof(Page), "a", "<script type='text/javascript'>alert('Successfully,there are " + m + " records!');</script>");
}
else
{
ClientScript.RegisterStartupScript(typeof(Page), "a", "<script type='text/javascript'>alert('" + (GridView1.Rows.Count-m) + " records don't input database!');</script>");
}
}
else
{
ClientScript.RegisterStartupScript(typeof(Page), "a", "<script type='text/javascript'>alert('No Data!');</script>");
}
}
#endregion
}
SqlHelp.cs代码:
/// <summary>
///SqlHelp类
/// </summary>
public class SqlHelp
{
#region Attribute
DataAccess da = new DataAccess();
DataSet ds = new DataSet();
#endregion
#region Constructed Function
public SqlHelp()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
#endregion
#region Methods
//Bind DropDownList Helper
//public DataSet DropDownListHelp()
//{
// string sql = "select * from Tables";
// return da.GetDataSet(sql);
//}
/// <summary>
/// Input Excel File Helper
/// </summary>
/// <param name="filePath"></param>
/// <param name="sheetName"></param>
/// <returns></returns>
public DataSet InputExeclFileHelp(string filePath,string sheetName)
{
string connStr = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + filePath + ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'";
string sqlStr = string.Format("select * from [{0}]", sheetName);
OleDbConnection conn = new OleDbConnection(connStr);
try
{
conn.Open();
OleDbCommand ocmd = new OleDbCommand(sqlStr, conn);
OleDbDataAdapter odda = new OleDbDataAdapter(ocmd);
odda.Fill(ds);
return ds;
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
return null;
}
finally
{
conn.Close();
}
}
//Input the data to database helper
public int InputDataToDatabase(string number,string name,string sex,string classNum,string profession,string school)
{
string sql = string.Format("insert into Student(Number,Name,Sex,Class,Profession,School) values('{0}','{1}','{2}','{3}','{4}','{5}')", number, name, sex, classNum, profession, school);
string connString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
int i = da.ExecuteSql(sql,connString);
if (i > 0)
{
return i;
}
else
{
return 0;
}
}
#endregion
}

浙公网安备 33010602011771号