Code
Gradview读取Excel表并插入数据库
public class inputExcel
{
public DataTable InputExcel(string Path)
{
try
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "select * from [Sheet1$]";
OleDbDataAdapter myCommand = null;
myCommand = new OleDbDataAdapter(strExcel, strConn);
DataTable dt = new DataTable();
myCommand.Fill(dt);
conn.Close();
return dt;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
//GridView读取Excel表
protected void btnInexcel_Click(object sender, EventArgs e)
{
try
{
if (this.FileUpload.HasFile)
{
string filename = this.FileUpload.FileName.ToString().Trim();
string path = MapPath("../inExcel/upfile/" + filename);
this.FileUpload.SaveAs(path); //上传文件
inputExcel input = new inputExcel();
DataTable inputdt = new DataTable();
inputdt = input.InputExcel(path); //传参取得dt
if (Session["inputdt"] != null)
Session.Remove("inputdt");
Session.Add("inputdt", inputdt);
if (inputdt.Rows.Count > 0)
{
this.GridView2.DataSource = inputdt; //GridView读取Excel表,并显示数据
this.GridView2.DataBind();
}
}
else
throw new Exception("请选择导入表的路径");
}
catch (Exception ex)
{
Response.Write("<script language='javascript'>alert('" + ex.Message + "');</script>");
}
}
//将Gridview数据插入数据库
protected void Button1_Click(object sender, EventArgs e)
{
DataTable inputdt = new DataTable();
inputdt = (DataTable)Session["inputdt"];
InputDB inputdb = new InputDB();
try
{
inputdb.InputDb(inputdt);
Response.Write("<SCRIPT>alert('数据已成功导入到数据库!');</SCRIPT>");
}
catch (Exception ex)
{
Response.Write("<SCRIPT>alert('数据导入到数据库失败!" + ex.Message + "');</SCRIPT>");
}
}
Gradview读取Excel表并插入数据库
public class inputExcel
{
public DataTable InputExcel(string Path)
{
try
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "select * from [Sheet1$]";
OleDbDataAdapter myCommand = null;
myCommand = new OleDbDataAdapter(strExcel, strConn);
DataTable dt = new DataTable();
myCommand.Fill(dt);
conn.Close();
return dt;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
//GridView读取Excel表
protected void btnInexcel_Click(object sender, EventArgs e)
{
try
{
if (this.FileUpload.HasFile)
{
string filename = this.FileUpload.FileName.ToString().Trim();
string path = MapPath("../inExcel/upfile/" + filename);
this.FileUpload.SaveAs(path); //上传文件
inputExcel input = new inputExcel();
DataTable inputdt = new DataTable();
inputdt = input.InputExcel(path); //传参取得dt
if (Session["inputdt"] != null)
Session.Remove("inputdt");
Session.Add("inputdt", inputdt);
if (inputdt.Rows.Count > 0)
{
this.GridView2.DataSource = inputdt; //GridView读取Excel表,并显示数据
this.GridView2.DataBind();
}
}
else
throw new Exception("请选择导入表的路径");
}
catch (Exception ex)
{
Response.Write("<script language='javascript'>alert('" + ex.Message + "');</script>");
}
}
//将Gridview数据插入数据库
protected void Button1_Click(object sender, EventArgs e)
{
DataTable inputdt = new DataTable();
inputdt = (DataTable)Session["inputdt"];
InputDB inputdb = new InputDB();
try
{
inputdb.InputDb(inputdt);
Response.Write("<SCRIPT>alert('数据已成功导入到数据库!');</SCRIPT>");
}
catch (Exception ex)
{
Response.Write("<SCRIPT>alert('数据导入到数据库失败!" + ex.Message + "');</SCRIPT>");
}
}