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 else14 {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 }View Code
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" />View Code经过第二天想了以后,其实连接字符串什么都没有问题,最后得出来的结论是因为导出并非标准格式的excel,不行的粘贴一下即可
导入方法
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.12.0;Data Source =" + path + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1'";10 string strConn = "Provider = Microsoft.ACE.OLEDB.15.0;Data Source =" + path + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1'";11 //此连接可以操作.xls与.xlsx文件 (支持Excel2003 和 Excel2007 的连接字符串)12 //备注: "HDR=yes;"是说Excel文件的第一行是列名而不是数据,"HDR=No;"正好与前面的相反。13 // "IMEX=1 "如果列中的数据类型不一致,使用"IMEX=1"可必免数据类型冲突。 14 15 OleDbConnection conn = new OleDbConnection(strConn);16 17 conn.Open();18 19 /*20 如果用户把sheet表名改了就是报下面的错:21 'sheet1$' 不是一个有效名称。请确认它不包含无效的字符或标点,且名称不太长。 22 如果可以动态获得Excel中各个sheet的名称能动态的选中返回哪个sheet的数据,23 即使用户更改了名称也不怕,完全可以把所有sheet的名称列出来供用户选择: 24 下面就把解决问题的代码列出来,主要用到了OleDbConnection.GetOleDbSchemaTable()方法25 */26 27 //返回Excel的架构,包括各个sheet表的名称,类型,创建时间和修改时间等28 System.Data.DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });29 30 //包含excel中表名的字符串数组31 string[] strTableNames = new string[dtSheetName.Rows.Count];32 for (int k = 0; k < dtSheetName.Rows.Count; k++)33 {34 strTableNames[k] = dtSheetName.Rows[k]["TABLE_NAME"].ToString();35 }36 37 //OleDbDataAdapter myCommand = null;38 System.Data.DataTable dt = new System.Data.DataTable();39 40 //从指定的表明查询数据,可先把所有表明列出来供用户选择41 string strExcel = "select * from [" + strTableNames[0] + "]";42 43 44 45 OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn);46 47 DataSet myDataSet = new DataSet();48 49 50 myCommand.Fill(myDataSet);51 52 return myDataSet;53 }View Code
导出方法
1 private void CreateExcel(System.Data.DataTable dt, string fileName) 2 { 3 HttpResponse resp; 4 resp = Page.Response; 5 6 resp.Buffer = true; 7 resp.ClearContent(); 8 resp.ClearHeaders(); 9 resp.Charset = "GB2312";10 11 12 // resp.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");13 resp.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);14 resp.ContentEncoding = System.Text.Encoding.Default;//设置输出流为简体中文 15 resp.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 16 17 18 19 string colHeaders = "", ls_item = "";20 21 ////定义表对象与行对象,同时用DataSet对其值进行初始化22 //DataTable dt = ds.Tables[0];23 DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的24 25 int i = 0;26 int cl = dt.Columns.Count;27 28 29 // colHeaders += "tttttttt" + "/n";30 //取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符31 for (i = 0; i < cl; i++)32 {33 if (i == (cl - 1))//最后一列,加n34 {35 colHeaders += dt.Columns[i].Caption.ToString().Trim() + "/n";36 }37 else38 {39 colHeaders += dt.Columns[i].Caption.ToString().Trim() + "/t";40 }41 42 }43 resp.Write(colHeaders);44 45 //向HTTP输出流中写入取得的数据信息46 47 48 49 //逐行处理数据 50 foreach (DataRow row in myRow)51 {52 //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据 53 for (i = 0; i < cl; i++)54 {55 if (i == (cl - 1))//最后一列,加n56 {57 ls_item += row[i].ToString().Trim() + "/n";58 }59 else60 {61 ls_item += row[i].ToString().Trim() + "/t";62 }63 64 }65 resp.Write(ls_item);66 ls_item = "";67 68 }69 resp.End();70 71 }View Code
记得引用Micorosoft.office.Interop.Excel
新闻热点
疑难解答