标签:
1 private DataSet loadSouce(string path) 2 { 3 4 5 //连接EXCEL数据源 6 //string strPath = this.getPath(); 7 //string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=" + "\"" + "Excel 8.0;HDR=Yes;IMEX=1" + "\""; 8 9 string strConn = "Provider=Microsoft.Ace.OleDb.15.0;data source=" + path + ";Extended Properties=‘Excel 15.0; HDR=Yes; IMEX=1‘"; //此连接可以操作.xls与.xlsx文件 (支持Excel2003 和 Excel2007 的连接字符串) 10 //备注: "HDR=yes;"是说Excel文件的第一行是列名而不是数据,"HDR=No;"正好与前面的相反。 11 // "IMEX=1 "如果列中的数据类型不一致,使用"IMEX=1"可必免数据类型冲突。 12 13 OleDbConnection conn = new OleDbConnection(strConn); 14 15 conn.Open(); 16 17 /* 18 如果用户把sheet表名改了就是报下面的错: 19 ‘sheet1$‘ 不是一个有效名称。请确认它不包含无效的字符或标点,且名称不太长。 20 如果可以动态获得Excel中各个sheet的名称能动态的选中返回哪个sheet的数据, 21 即使用户更改了名称也不怕,完全可以把所有sheet的名称列出来供用户选择: 22 下面就把解决问题的代码列出来,主要用到了OleDbConnection.GetOleDbSchemaTable()方法 23 */ 24 25 //返回Excel的架构,包括各个sheet表的名称,类型,创建时间和修改时间等 26 System.Data.DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); 27 28 //包含excel中表名的字符串数组 29 string[] strTableNames = new string[dtSheetName.Rows.Count]; 30 for (int k = 0; k < dtSheetName.Rows.Count; k++) 31 { 32 strTableNames[k] = dtSheetName.Rows[k]["TABLE_NAME"].ToString(); 33 } 34 35 //OleDbDataAdapter myCommand = null; 36 System.Data.DataTable dt = new System.Data.DataTable(); 37 38 //从指定的表明查询数据,可先把所有表明列出来供用户选择 39 string strExcel = "select * from [" + strTableNames[0] + "]"; 40 41 42 43 OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn); 44 45 DataSet myDataSet = new DataSet(); 46 47 48 myCommand.Fill(myDataSet); 49 50 return myDataSet; 51 } 52 53 54 private void CreateExcel(System.Data.DataTable dt, string fileName) 55 { 56 HttpResponse resp; 57 resp = Page.Response; 58 59 resp.Buffer = true; 60 resp.ClearContent(); 61 resp.ClearHeaders(); 62 resp.Charset = "GB2312"; 63 64 65 // resp.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); 66 resp.AppendHeader("Content-Disposition", "attachment;filename=" + fileName); 67 resp.ContentEncoding = System.Text.Encoding.Default;//设置输出流为简体中文 68 resp.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 69 70 71 72 string colHeaders = "", ls_item = ""; 73 74 ////定义表对象与行对象,同时用DataSet对其值进行初始化 75 //DataTable dt = ds.Tables[0]; 76 DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的 77 78 int i = 0; 79 int cl = dt.Columns.Count; 80 81 82 colHeaders += "tttttttt" + "\n"; 83 //取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符 84 for (i = 0; i < cl; i++) 85 { 86 if (i == (cl - 1))//最后一列,加n 87 { 88 colHeaders += dt.Columns[i].Caption.ToString().Trim() + "\n"; 89 } 90 else 91 { 92 colHeaders += dt.Columns[i].Caption.ToString().Trim() + "\t"; 93 } 94 95 } 96 resp.Write(colHeaders); 97 98 //向HTTP输出流中写入取得的数据信息 99 100 101 102 //逐行处理数据 103 foreach (DataRow row in myRow) 104 { 105 //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据 106 for (i = 0; i < cl; i++) 107 { 108 if (i == (cl - 1))//最后一列,加n 109 { 110 ls_item += row[i].ToString().Trim() + "\n"; 111 } 112 else 113 { 114 ls_item += row[i].ToString().Trim() + "\t"; 115 } 116 117 } 118 resp.Write(ls_item); 119 ls_item = ""; 120 121 } 122 resp.End(); 123 124 }
1 protected void BtnLoad_Click(object sender, EventArgs e) 2 { 3 //dataDR datadr = new dataDR(); 4 //datadr.delete("delete from dbo.yuepingjueyuerbiao"); 5 string fileName = DateTime.Now.ToString("yyMMddhhmmss") + ".xls"; 6 string path = Server.MapPath("~/upload/excel/") + fileName; 7 8 FileUpload1.SaveAs(path); 9 if (path != null) 10 { 11 DataSet ds=loadSouce(path); 12 } 13 else 14 { 15 Response.Write("<script>alert(‘没有上传文件‘)</script>"); 16 } 17 } 18 19 protected void Button1_Click(object sender, EventArgs e) 20 { 21 CreateExcel(helps.GetDataTable("select * from tb_teacher"), "Test.xls"); 22 }
1 <div> 2 3 <asp:FileUpload ID="FileUpload1" runat="server" /><asp:Button ID="BtnLoad" runat="server" Text="上传" OnClick="BtnLoad_Click" /> 4 5 </div> 6 7 <asp:Button ID="Button1" runat="server" Text="下载" style="height: 21px" OnClick="Button1_Click" />
标签:
原文地址:http://www.cnblogs.com/yuanjiehot/p/4349255.html