- HTML code
<body> <form id="form1" runat="server"> <div> <INPUT id="File1" type="file" name="File1" runat="server"/> <asp:Button id="Button1" runat="server" Text="上传表1" OnClick="Button1_Click"></asp:Button> <asp:Button id="Button2" runat="server" Text="保存" OnClick="Button2_Click"></asp:Button> </div> <div> <asp:GridView ID="GridView1" runat="server" CellPadding="3" BackColor="White" BorderColor="#CCCCCC" BorderWidth="1px" Font-Size="12px" > <RowStyle ForeColor="#000066" /><SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /><PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" CssClass="ms-formlabel DataGridFixedHeader"/><HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> </asp:GridView> </div> </form></body>
- C# code
protected void Button1_Click(object sender, EventArgs e) { string strPath = Request.PhysicalApplicationPath + "\\upfile\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls"; File1.PostedFile.SaveAs(strPath); string mystring = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '" + strPath + "';Extended Properties=Excel 8.0"; OleDbConnection cnnxls = new OleDbConnection(mystring); OleDbDataAdapter myDa = new OleDbDataAdapter("select * from [Sheet1$]", cnnxls); DataSet myDs = new DataSet(); myDa.Fill(myDs); GridView1.DataSource = myDs.Tables[0]; GridView1.DataBind(); }
实现的简单的上传excle后绑定在gridview
但因使用的电脑中的excle文件如果没叫管理员解锁是传不了,如果用ExcelHelper应该不用解锁就能上传
要怎么加上应用ExcelHelper呢?
------解决方案--------------------------------------------------------
如果说可以读取的话,将excel中的数据读出来绑定到DataTable中
然后再与GridView绑定
- C# code
DataTable table = new DataTable(); StreamReader reader = new StreamReader(path + fileName, Encoding.GetEncoding("GB2312")); string columnName = reader.ReadLine(); string[] columns = columnName.Split('\t'); string strBody = reader.ReadToEnd(); //关闭流并释放资源 reader.Close(); reader.Dispose(); File.Delete(path + fileName);//删除临时文件信息 strBody = strBody.Replace("\r\n", "あ");//看一下行与行之间是用什么分割的 string[] rows = strBody.Split('あ'); int rowsCount = rows.Length; //向DataTable中添加DataColumn DataColumn column = new DataColumn("proName"); table.Columns.Add(column); DataColumn column1 = new DataColumn("price"); table.Columns.Add(column1); DataColumn column2 = new DataColumn("stock"); table.Columns.Add(column2); DataColumn column3 = new DataColumn("pic"); table.Columns.Add(column3); DataColumn column4 = new DataColumn("descript"); table.Columns.Add(column4); for (int i = 0; i < rows.Length; i++) { if (rows[i].Length > 0) { string[] rowsInfo = rows[i].Split('\t'); if (rowsInfo.Length == columns.Length) { int columnsCount = rowsInfo.Length; DataRow row = table.NewRow(); string name = rowsInfo[0]; string price = rowsInfo[7]; string sku = rowsInfo[9]; string original = GetOriginal(rowsInfo[36]); string descript = rowsInfo[25]; int m = table.Columns.Count; row["proName"] = name;//商品名称 row["price"] = price;//商城价格 row["stock"] = sku;//库存量 row["pic"] = "yxuploadfile/product/original/" + original;//商品原图 row["descript"] = descript;//商品描述 table.Rows.Add(row); if (i>=13) { price = rowsInfo[7]; } } } } return table;