//关键地方
//select * from [Excel 8.0;HDR=YES;DATABASE=C:\\Users\\Administrator\\Desktop\\gz2013.xls].[gz201301$] 此处excel原文件要用绝对路径,否则会出问题。对于office2003的excel文件(.xls)使用[Excel 8.0说明。
cs源代码如下:
using System;
using System.Data.OleDb;
public partial class insert : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["ClerkNum"] == null)
{
Response.Redirect("login.aspx");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int count_time = 0;
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|xwx_Data.mdb;");
conn.Open();
OleDbCommand cmd = new OleDbCommand("insert into userinfo4(时间,序号,单位名称,职工代码,单位代码,薪级,姓名,岗位工资,薪级工资,校内津贴,加班工资,劳资补款,劳资扣款,应发工资) select * from [Excel 8.0;HDR=YES;DATABASE=C:\\Users\\Administrator\\Desktop\\gz2013.xls].[gz201301$]", conn);
int relsult= cmd.ExecuteNonQuery();
if (relsult > 0 && count_time == 0)
{
Label1.Text = "恭喜你,导入成功!";
count_time++;
}
else if (count_time != 0)
Label1.Text = "你已经到如果一次数据了,请确定数据不重复!";
else
Label1.Text = "真抱歉,导入失败!";
conn.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("../index.aspx");
}
}