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 {
2,使用oledb打开excel文件数据库/或者使用Application.Excel打开文件直接操作
3,读取需要的数据。
4,保存到数据库
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");//取得文件路径
}
}
public static void DataTableToDB()
{
string _strExcelFileName = @"D:\example.xls";
DataTable dtExcel = ExcelToDataTable(_strExcelFileName,"Sheet1");
for (int i = 0; i < dtExcel.Rows.Count; i++)
{
InsertDataToAccess(dtExcel.Rows[i][0].ToString(), float.Parse(dtExcel.Rows[i][1].ToString()));
}
}二、把Excel数据读入DataTable
public static DataTable ExcelToDataTable(string strExcelFileName, string strSheetName)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strExcelFileName + ";" +"Extended Properties=Excel 5.0;";
string strExcel = string.Format("select * from [{0}$]", strSheetName);
DataSet ds = new DataSet(); using (OleDbConnection conn = new OleDbConnection(strConn))
{
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
adapter.Fill(ds, strSheetName);
conn.Close();
} return ds.Tables[strSheetName];
}三、向Access数据库表插入数据
public static void InsertDataToAccess(string _strPara,float _fPara)
{
OleDbConnection oleDbConn = new OleDbConnection();
oleDbConn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\ExcelData.mdb;User Id=admin;Password=;";
oleDbConn.Open(); string strInsertString = "INSERT INTO tb_excelData (strCollumn1,fCollumn2) VALUES (@strCollumn1,@fCollumn2)";
OleDbCommand oComm = new OleDbCommand(strInsertString, oleDbConn);
oComm.Parameters.Add("@strCollumn1", OleDbType.Char , 50);
oComm.Parameters["@strCollumn1"].Value = _strPara;
oComm.Parameters.Add("@fCollumn2", OleDbType.Double);
oComm.Parameters["@fCollumn2"].Value = _fPara;
ocomm.ExecuteNonQuery();
oleDbConn.Close();
}