代码是网上找的,修改了下,如下,还有一个FileUpload1控件代码不知道怎么写?请帮忙,非常感谢还有GridView1不能同时定义DataSource和SqlDataSource的话,是不是意味着GridView1不能绑定SqlDataSource数据源来显示,而只能当做导入的空表? //导入EXCEL代码    protected void Button1_Click(object sender, EventArgs e)
    {
        
        GridView1.DataSource = createDataSource();        GridView1.DataBind(); 
    }    private DataSet createDataSource()
    {
        string strCon;
        strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:/WebSite8/Files/Employee.xls;Extended Properties=Excel 8.0;";
        OleDbConnection con = new OleDbConnection(strCon);
        OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        return ds;
    }
    protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
    {        
            foreach (TableCell tc in e.Row.Cells)
        {
            tc.Attributes["style"] = "border-color:Black";
        }
        if (e.Row.RowIndex != -1)
        {
            int id = GridView1.PageIndex * GridView1.PageSize + e.Row.RowIndex + 1;
            e.Row.Cells[0].Text = id.ToString();
        }    } 

解决方案 »

  1.   

    呵呵 我给你整一个看看FileUpload1
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = FileUpload1.FileName;//上传文件名字
            string size = FileUpload1.PostedFile.ContentLength.ToString();
            string type = FileUpload1.PostedFile.ContentType;
            string type2 = name.Substring(name.LastIndexOf(".") + 1);
            string ipath = Server.MapPath("upimg") + "\\" + name;
            string fpath = Server.MapPath("upfile") + "\\" + name;
            string path="F:\\aaa\\"+FileUpload1.FileName;
            string wpath = "upimg\\" + name;
            if (type2 == "jpg" || type2 == "gif" || type2 == "bmp" || type2 == "png")
            {
                FileUpload1.SaveAs("F:\\aaa\\"+FileUpload1.FileName);
               // Image1.ImageUrl="F:\\aaa\\"+FileUpload1.FileName;
                Label1.Text = "你传图片的名字是" + name + "<br>文件大小为" + size + "<br>文件类型为" + type2 + "<br>文件路径为" + ipath;
            }
            
            SqlConnection cn = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=sa");
            SqlCommand cmd = new SqlCommand("insert into Image(imageName,imagepath) values('" + name + "','" + path + "')", cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        protected void Button2_Click(object sender, EventArgs e)
        {    SqlConnection cn = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=sa");
        SqlCommand cmd = new SqlCommand("select imageName from Image where imageID='" + Convert.ToInt32(TextBox1.Text) + "'", cn);
        cn.Open();
        string a = cmd.ExecuteScalar().ToString();
        cn.Close();
        Image1.ImageUrl = "F:\\aaa\\" + a;
         }
    }
    Gridview导出excel. #region 导出为Excel
        public override void VerifyRenderingInServerForm(Control control)
        {
            // Confirms that an HtmlForm control is rendered for
        }    private void ToExcel(Control ctl, string FileName)
        {
            HttpContext.Current.Response.Charset = "UTF-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
            ctl.Page.EnableViewState = false;
            System.IO.StringWriter tw = new System.IO.StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            ctl.RenderControl(hw);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();
        }    private void toExcelClk()
        {
            gvSysUser.AllowPaging = false;
            gvSysUser.AllowSorting = false;
            gvSysUser.DataBind();
            ToExcel(gvSysUser, "OFS_Data.xls");
            gvSysUser.AllowPaging = true;
            gvSysUser.AllowSorting = true;
            gvSysUser.DataBind();
        }
        #endregion