using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace WebApplication1
{
/// <summary>
/// tantest 的摘要说明。
/// </summary>
public class tantest : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label labelUpResult;
protected System.Web.UI.WebControls.Label labelFileExt;
protected System.Web.UI.WebControls.Label labelFileSize;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.HtmlControls.HtmlInputFile inputFile;
protected System.Web.UI.WebControls.Label labelFileName;
//属性
private string Excelsource
{
get
{
return (ViewState["Excelsource"]==null)?"":(string)(ViewState["Excelsource"]);
}
set
{
ViewState["Excelsource"] = value;
}
}
private void Page_Load(object sender, System.EventArgs e)
{
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
//检查上传文件不为空
if(inputFile.PostedFile.ContentLength > 0)
{
//设定上传文件的保存路径
string strSaveDir = "./WHHRREPORT/";
string strName = inputFile.PostedFile.FileName;
//取得文件名(抱括路径)里最后一个"."的索引
int intExt = strName.LastIndexOf(".");
//取得文件扩展名
string strExt = strName.Substring(intExt);
//这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
/*
DateTime datNow = DateTime.Now;
string strNewName = datNow.DayOfYear.ToString() + inputFile.PostedFile.ContentLength.ToString() + strExt; */
//取得文件名(包括路径)里最后一个"\"的索引
int intPath = strName.LastIndexOf("\\");
//取得文件名(不包括路径)
string strNewName = strName.Substring(intPath);
//保存文件到你所要的目录,这里是IIS根目录下的upload目录.你可以改变.
//注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
inputFile.PostedFile.SaveAs(Server.MapPath(strSaveDir + strNewName));
Excelsource=Server.MapPath(strSaveDir + strNewName).ToString();
//得到这个文件的相关属性:文件名,文件类型,文件大小
labelUpResult.Text = "上传成功!";
labelFileName.Text = "文件源:" + strName;
labelFileExt.Text = "文件类型:" + inputFile.PostedFile.ContentType + "( " + strExt + " )";
labelFileSize.Text = "文件大小:" + (inputFile.PostedFile.ContentLength / 1024).ToString() + " K Byte(s)";
}
else
{
labelUpResult.Text = "请选择你要上传的文件!";
labelFileName.Text = "";
labelFileExt.Text = "";
labelFileSize.Text = "";
}
}
private void Button2_Click(object sender, System.EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["DSN"]);
//连接excel数据源
string excelconnstring=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Excelsource+"";
excelconnstring+=@";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""" ;
System.Data.OleDb.OleDbConnection excelconn=new System.Data.OleDb.OleDbConnection(excelconnstring);
string sql="select * from [sheet1$]";
System.Data.OleDb.OleDbDataAdapter mycomm=new System.Data.OleDb.OleDbDataAdapter(sql,excelconn) ;
DataSet myds=new DataSet();
mycomm.Fill(myds,"ss");
//
SqlCommand cm= new SqlCommand();
cm.Connection=conn;
conn.Open();
for(int i=0;i<myds.Tables[0].Rows.Count;i++)
{
string updateSql1="insert into tantest(ghid,xjhao,ylao,yliao,gjj)values('"+myds.Tables[0].Rows[i]["工号"]+"','"+myds.Tables[0].Rows[i]["薪金号"]+"','"+myds.Tables[0].Rows[i]["养老基金"]+"','"+myds.Tables[0].Rows[i]["医疗基金"]+"','"+myds.Tables[0].Rows[i]["公积金"]+"')";
cm.CommandText=updateSql1;
try
{
cm.ExecuteNonQuery();
}
catch
{
}
finally
{
this.Label1.Visible=true;
this.Label1.Text="数据导入成功!";
}
}
cm.Dispose();
conn.Close();
}
}
}