C#读取Excel显示到repeater中
生活随笔
收集整理的這篇文章主要介紹了
C#读取Excel显示到repeater中
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先需要一個用來存儲我們需要顯示的內容,防止頁面回發丟失(添加時使用)
#region 緩存文件private DataTable excelData;/// <summary>/// 緩存已經讀取過的Datatable/// </summary>public DataTable ExcelData{get{if (ViewState["mydata"] != null){excelData = (DataTable)ViewState["mydata"];}return excelData;}set{ViewState["mydata"] = value;}}#endregion?接下來檢查上傳文件
/// <summary>/// 檢查文件上傳/// </summary>/// <param name="fileName"></param>/// <returns></returns>public string CheckFile(string fileName){if (string.IsNullOrEmpty(fileName)){return "Please ensure that you upload file exists!";}if (!fileName.ToLower().EndsWith(".xls") && !fileName.ToLower().EndsWith(".xlsx")){return "You select the file format is not correct, please try again!";}if (fileName.IndexOf(".") <= 0){return "Please select a file to upload!";}return "";}?
/// <summary>/// 連接Excel,并讀取數據源/// </summary>/// <param name="filepath">數據源路徑</param>/// <returns>Excel文件的工作薄里的數據</returns>public DataSet connExcel(string filepath){if (!string.IsNullOrEmpty(filepath)){try{string connstring = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + filepath + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'"; //此連接可以操作.xls與.xlsx文件 (支持Excel2003 和 Excel2007 的連接字符串)OleDbConnection objconn = new OleDbConnection(connstring);objconn.Open();DataTable dt = objconn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "table" });string sheetname = "Sheet1$";sheetname = dt.Rows[0]["TABLE_NAME"].ToString();objconn.Close();string strsql = "select * from [" + sheetname + "]";DataSet ds = new DataSet();OleDbDataAdapter adp = new OleDbDataAdapter(strsql, objconn);adp.Fill(ds);return ds;}catch (Exception ex){throw new Exception("Occurs when a data source connection:" + ex.Message);}}else{throw new Exception("File does not exist!");}}?
/// <summary>/// 讀取Excel/// </summary>/// <param name="path"></param>/// <returns></returns>private DataTable readExcel(string path){DataTable dt = new DataTable("myTestTable");dt.Columns.Add("ID");dt.Columns.Add("Name");dt.Columns.Add("Sex");DataView dv = new DataView(dt);DataSet ds = connExcel(path);return ds.Tables[0];}?
/// <summary>/// 上傳文件的方法/// </summary>/// <returns></returns>[ScriptMethod]private void PreviewData(){//獲取文件名string fileName = Path.GetFileName(this.fileSave.PostedFile.FileName);//檢索文件string message = CheckFile(fileName);if (!string.IsNullOrEmpty(message)){Page.ClientScript.RegisterStartupScript(this.GetType(), "ok", "alert('" + message + "')", true);return;}try{string path = Server.MapPath("/MyTest/ExcelData/");//文件是否存在,如果不存在則創建if (!Directory.Exists(path)){Directory.CreateDirectory(path);}//定義文件的隨機數Random rand = new Random();path += rand.Next(0, 9999999) + DateTime.Now.Hour + DateTime.Now.Minute + fileName;//上傳文件this.fileSave.PostedFile.SaveAs(path);//將數據內容保存到緩存中ExcelData = this.readExcel(path);this.rptData.Visible = true;this.rptData.DataSource = ExcelData;this.rptData.DataBind();}catch (Exception ex){throw ex;//Page.ClientScript.RegisterStartupScript(this.GetType(), "no", "alert('" + ex.Message + "')", true);}}?
轉載于:https://www.cnblogs.com/myblogslh/p/4387581.html
總結
以上是生活随笔為你收集整理的C#读取Excel显示到repeater中的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win32之hPrevInstance
- 下一篇: Android nDrawer