电竞比分网-中国电竞赛事及体育赛事平台

分享

GridView數(shù)據(jù)導(dǎo)入Excel/Excel數(shù)據(jù)讀入GridView?

 悟靜 2012-02-25

GridView數(shù)據(jù)導(dǎo)入Excel/Excel數(shù)據(jù)讀入GridView?

1.導(dǎo)出Excel:
頁(yè)面增加一個(gè)按鈕,單擊事件添加如下方法:
protected void Button1_Click(object sender, EventArgs e)
{
        Export("application/ms-excel", "學(xué)生成績(jī)報(bào)表.xls");
}private void Export(string FileType, string FileName)
{
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        Response.ContentType = FileType;
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        GridView1.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}

//如果沒有下面方法會(huì)報(bào)錯(cuò)類型“GridView”的控件“GridView1”必須放在具有 runat=server 的窗體標(biāo)記內(nèi)
public override void VerifyRenderingInServerForm(Control control)
{
}


還有由于是文件操作所以要引入名稱空間IO和Text
2.讀取Excel:
private DataSet CreateDataSource()
{
        string strCon;
        strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("excel.xls") + "; Extended Properties=Excel 8.0;";
        OleDbConnection olecon = new OleDbConnection(strCon);
        OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon);
        DataSet myds = new DataSet();
        myda.Fill(myds);
        return myds;
}
protected void Button1_Click(object sender, EventArgs e)
{
        GridView1.DataSource = CreateDataSource();
        GridView1.DataBind();
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多