我的例子在Windows应用程序(C#)上经过了测试:
string xlsFilePath = "G:\\Book1.xls";
string connectionString;
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + xlsFilePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
OleDbConnection conn = new OleDbConnection(connectionString);
String strQuery = "SELECT * FROM [Sheet1$]"; //可以更改工作表名称
OleDbDataAdapter da = new OleDbDataAdapter(strQuery, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Sheet1");
DataTable dt = ds.Tables[0];
dataGridView1.DataSource = dt;
conn.Close();
运行效果:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using My_Class;
using System.Data.SqlClient;
using System.IO;
public partial class daoru : System.Web.UI.Page
{
protected static string lj = "";
protected static string getErrMsg = "";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string filePath = "";
if (FJ.PostedFile.FileName == "")
{
Response.Write("<script language=javascript>alert('请选择要上传的文件!');</script>");
return;
}
else
{
filePath = FJ.PostedFile.FileName;//取得文件路径
string sql_excel = "select 序号,单位名称,企业人员分布,姓名,性别,出生日期,身份证号,工作时间,工龄,个人身份,薪酬,学历,技术等级名称,级别,工种,持证上岗情况, 技术岗位人员所占比例,取得资格证书时间,颁发证书单位 from OPENROWSET('MICROSOFT.JET.OLEDB.4.0','Excel 8.0;HDR=YES;DATABASE=" + filePath + "',ygxxb$)";
DataSet ds = new DataSet();
ds = conn.executeQuery(sql_excel);
DataTable dt = new DataTable();
dt = ds.Tables[0];
GridView1.DataSource = dt;
GridView1.DataBind();
if (ds.Tables[0].Rows.Count != 0)
{
string sql = "";
//////////////链接数据库////////////////////////////
SqlConnection myConnection = new SqlConnection();
string strConn = System.Configuration.ConfigurationSettings.AppSettings["sqlserver"];
myConnection.ConnectionString = strConn;
SqlCommand da = new SqlCommand();
try
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
sql = "insert into ygxx(qy_id,bm_id,name,sex,birthday,card,work_time,gongling,grsf,xc,culture,dj_name,zd_id,gz_id,sfcz,szbl,qdzg_time,bfdw,xh) values ('" + ds.Tables[0].Rows[i]["单位名称"].ToString() + "','" + ds.Tables[0].Rows[i]["企业人员分布"].ToString() + "','" + ds.Tables[0].Rows[i]["姓名"].ToString() + "','" + ds.Tables[0].Rows[i]["性别"].ToString() + "','" + ds.Tables[0].Rows[i]["出生日期"].ToString() + "','" + ds.Tables[0].Rows[i]["身份证号"].ToString() + "','" + ds.Tables[0].Rows[i]["工作时间"].ToString() + "','" + ds.Tables[0].Rows[i]["工龄"].ToString() + "','" + ds.Tables[0].Rows[i]["个人身份"].ToString() + "','" + ds.Tables[0].Rows[i]["薪酬"].ToString() + "','" + ds.Tables[0].Rows[i]["学历"].ToString() + "','" + ds.Tables[0].Rows[i]["技术等级名称"].ToString() + "','" + ds.Tables[0].Rows[i]["级别"].ToString() + "','" + ds.Tables[0].Rows[i]["工种"].ToString() + "','" + ds.Tables[0].Rows[i]["持证上岗情况"].ToString() + "','" + ds.Tables[0].Rows[i]["技术岗位人员所占比例"].ToString() + "','" + ds.Tables[0].Rows[i]["取得资格证书时间"].ToString() + "','" + ds.Tables[0].Rows[i]["颁发证书单位"].ToString() + "','" + ds.Tables[0].Rows[i]["序号"].ToString() + "')";
da = new SqlCommand(sql, myConnection);
if (da.Connection.State.ToString() == "Closed")
{
da.Connection.Open();
}
da.ExecuteNonQuery();
}
}
catch (Exception ex)
{
getErrMsg = ex.Message.ToString();
Response.Write(ex.Message.ToString());
}
finally
{
da.Connection.Close();
da = null;
}
if (getErrMsg == "" || getErrMsg == null)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script language='Javascript'>alert('导入成功!')</script>");
return;
}
else
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script language='Javascript'>alert('导入失败!')</script>");
return;
}
}
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("http://" + Request.ServerVariables["HTTP_HOST"] + Request.ApplicationPath.ToString() + "/module" + "/FileFlow.XLS");//取得文件路径
}
}
要显示 就直接绑定上面的DataSet就成了。