1 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System; 2 using System.Collections; 3 using System.Configuration; 4 using System.Data; 5 using System.Web; 6 using System.Web.Security; 7 using System.Web.UI; 8 using System.Web.UI.HtmlControls; 9 using System.Web.UI.WebControls; 10 using System.Web.UI.WebControls.WebParts; 11 using Food.BLL; 12 using Food.Models; 13 using Food.DAL; 14 15 public partial class test_ImportXls : System.Web.UI.Page 16 { 17 protected void Page_Load(object sender, EventArgs e) 18 { 19 ImportData(); 20 } 21 22 private DataSet xsldata(string filepath) 23 { 24 string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;IMEX=1'"; 25 System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(strCon); 26 string strCom = "SELECT * FROM [Sheet1$]"; 27 Conn.Open(); 28 System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, Conn); 29 DataSet ds = new DataSet(); 30 myCommand.Fill(ds, "[Sheet1$]"); 31 Conn.Close(); 32 return ds; 33 } 34 35 private void ImportData() 36 { 37 //批量添加 38 try 39 { 40 string fileurl = @"C:Documents and SettingsAdministrator桌面InsideLink.xls"; 41 DataSet ds = new DataSet();//取得数据集 42 ds = xsldata(fileurl); 43 44 for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 45 { 46 string typename = ds.Tables[0].Rows[i]["类名"].ToString(); 47 if (typename.Length > 0) 48 { 49 string linkvalue = ds.Tables[0].Rows[i]["链接"].ToString(); 50 DBHelper.ExecuteCommand("Insert Into replacekeyword values('" + typename + "','" + linkvalue + "','','','',1,1)"); 51 //dbhelper里面包含连接字符串 52 53 } 54 else 55 { 56 break; 57 } 58 59 } 60 Response.Write("OK"); 61 } 62 catch (Exception err) 63 { 64 Response.Write("Error"); 65 } 66 } 67 }