您的位置:首页 > 编程语言 > ASP

asp.net 将Excel数据导入到GridView和SQL数据库

2011-11-12 10:58 441 查看
using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

namespace WebApplication1

{

public partial class WebForm2 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

#region 连接Excel 读取Excel数据 并返回DataSet数据集合

/// <summary>

/// 连接Excel 读取Excel数据 并返回DataSet数据集合

/// </summary>

/// <param name="filepath">Excel服务器路径</param>

/// <param name="tableName">Excel表名称</param>

/// <returns></returns>

public static System.Data.DataSet ExcelSqlConnection(string filepath, string tableName)

{

string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";

OleDbConnection ExcelConn = new OleDbConnection(strCon);

try

{

string strCom = string.Format("SELECT * FROM [Sheet1$]");

ExcelConn.Open(); //打开数据链接,得到一个数据集

OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, ExcelConn);

DataSet ds = new DataSet(); //创建dataset对象

myCommand.Fill(ds, "[" + tableName + "$]");

ExcelConn.Close();

return ds;

}

catch

{

ExcelConn.Close();

return null;

}

}

#endregion

#region 导入的execl

protected void Button2_Click(object sender, EventArgs e)

{

SqlConnection cn = new BSqlDataProvider().GetSqlConnection();

cn.Open();

if (FileUpload1.HasFile == false)//HasFile用来检查FileUpload是否有指定文件

{

Response.Write("<script>alert('请您选择Excel文件')</script> ");

return;//当无文件时,返回

}

string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名

if (IsXls != ".xls")

{

Response.Write("<script>alert('只可以选择Excel文件')</script>");

return;//当选择的不是Excel文件时,返回

}

string filename = FileUpload1.FileName; //获取Execle文件名 DateTime日期函数

string savePath = Server.MapPath(("~\\upfiles\\") + filename);//Server.MapPath 获得虚拟服务器相对路径

FileUpload1.SaveAs(savePath); //SaveAs 将上传的文件内容保存在服务器上

DataSet ds = ExcelSqlConnection(savePath, filename); //调用自定义方法

DataRow[] dr = ds.Tables[0].Select(); //定义一个DataRow数组

int rowsnum = ds.Tables[0].Rows.Count;

if (rowsnum == 0)

{

Response.Write("<script>alert('Excel表为空表,无数据!')</script>"); //当Excel表为空时,对用户进行提示

}

else

{

for (int i = 0; i < dr.Length; i++)

{

string spdm = dr[i]["商品代码"].ToString();//日期 excel列名【名称不能变,否则就会出错】

string jijie = dr[i]["季节"].ToString();

string boduan = dr[i]["波段"].ToString();

string s_chan = dr[i]["生产商"].ToString();

string f_shi = dr[i]["方式"].ToString();

string c_ku = dr[i]["仓库"].ToString();

string insertstr = "insert into AA_ANSD values('" + spdm + "','" + jijie + "','" + boduan + "','" + s_chan + "','" + f_shi + "','" + c_ku + "')";

SqlCommand cmd = new SqlCommand(insertstr, cn);

try

{

cmd.ExecuteNonQuery();

}

catch (MembershipCreateUserException ex) //捕捉异常

{

Response.Write("<script>alert('导入内容:" + ex.Message + "')</script>");

}

}

Response.Write("<script>alert('Excle表导入成功!');location='CMT_Entry.aspx?CMD=0'</script>");

}

cn.Close();

}

#endregion

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: