日期:2014-05-18  浏览次数:20494 次

有办法实现客户端Excel导入数据库操作吗?
asp.net是在server端执行C#代码的,有办法实现excel导入操作吗?
请给个思路或者代码,多谢啦。

------解决方案--------------------
把excel上传到服务器上再进行操作
------解决方案--------------------
多个excel导入。

思路:本地读取excel放入dataset中,然后读取dataset中的table存入数据库。

--------------------------------
private bool Morefile()
{
//遍历File表单元素
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
//状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder( "文件信息分别为: <hr color=red> ");
int fileCount;
int filecount = files.Count;
try
{
for (fileCount = 0; fileCount < files.Count; fileCount++)
{
//定义访问客户端文件的对象
System.Web.HttpPostedFile postedFile = files[fileCount];
string fileName, fileExtension;
//取得文件名
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != String.Empty)
{
//取得文件的扩展名
fileExtension = System.IO.Path.GetExtension(fileName);

int count=fileName.Length;
string newname = fileName.Remove(count -4, 4);

/////////////////////////////////////////////////////////////////////////////

string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + postedFile.FileName + ";Extended Properties=Excel 8.0; ";
string query = "SELECT * FROM [Sheet1$] ";

OleDbCommand oleCommand = new OleDbCommand(query, new OleDbConnection(ConnStr));
OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand);
DataSet myDataSet = new DataSet();

oleAdapter.Fill(myDataSet, "[Sheet1$] ");

// 数据绑定
this.GridView1.DataSource = myDataSet;
this.GridView1.DataMember = "[Sheet1$] ";
this.GridView1.DataBind();

/////////////////////////////////////////////////////////////////////////////

DataSet ds1 = new DataSet();

ds1 = mypack.select_all_type(this.ddltarget.SelectedValue.ToString());

///////////////////////////////////////////////////////////////////////////

Response.Write( "导入开始 ");
string source = postedFile.FileName;
string mystring = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + source + ";Extended Properties=Excel 8.0 ";
OleDbConnection cnnxls = new OleDbConnection(mystring);
OleDbDataAdapter myDa = new OleDbDataAdapter( "select * from [Sheet1$] ", cnnxls);
DataSet myDs = new DataSet();
myDa.Fill(myDs);

if (myDs.Tables[0].Rows.Count > 0)
{
string strSql = " ";
string CnnString = "Provider=SQLOLEDB;database=packmis;server=(local);uid=sa;pwd= ";
OleDbConnection conn = new OleDbConnection(CnnString);
conn.Open();
OleDbCommand myCmd = null;