Excel导入&导出

*需要从GridView “gvGrade"中将显示数据导出 (网上搜来的)

  protected   void  bnToExcel_Click( object  sender, EventArgs e)
    
{
        Export(
"application/ms-excel""学生成绩报表.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);
        gvGrade.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
    }

 2.将Excel内容直接导到Access:

前台:

<% @ Page Language="C#" AutoEventWireup="true" CodeFile="FromExcel.aspx.cs" Inherits="Admin_FromExcel"  %>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< html  xmlns ="http://www.w3.org/1999/xhtml"   >
< head  runat ="server" >
    
< title > Untitled Page </ title >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
    
< div >
        
< asp:FileUpload  ID ="fuExcel"  runat ="server"  Style ="z-index: 100; left: 176px; position: absolute;
            top: 146px"
  />
        
< asp:Button  ID ="bnsubmit"  runat ="server"  OnClick ="bnsubmit_Click"  Style ="z-index: 101;
            left: 258px; position: absolute; top: 196px"
 Text ="提交"   />
        
< asp:DropDownList  ID ="ddDepart"  runat ="server"  AutoPostBack ="True"  DataSourceID ="dsGetDepart"
            DataTextField
="Department"  DataValueField ="Department"  Style ="z-index: 102; left: 179px;
            position: absolute; top: 96px"
>
        
</ asp:DropDownList >
        
< asp:AccessDataSource  ID ="dsGetDepart"  runat ="server"  DataFile ="~/App_Data/#SGMS_Data.mdb"
            SelectCommand
="SELECT DISTINCT [Department] FROM [Course]" ></ asp:AccessDataSource >
        
< asp:Label  ID ="Label1"  runat ="server"  Height ="27px"  Style ="z-index: 103; left: 60px;
            position: absolute; top: 94px"
 Text ="请选择专业:"  Width ="98px" ></ asp:Label >
        
< asp:CheckBox  ID ="cbCourse"  runat ="server"  OnCheckedChanged ="cbCourse_CheckedChanged"
            Style
="z-index: 104; left: 324px; position: absolute; top: 96px"  Text ="课程"  AutoPostBack ="True"   />
        
< asp:DropDownList  ID ="ddCourse"  runat ="server"  AutoPostBack ="True"  DataSourceID ="dsCourse"
            DataTextField
="CourseName"  DataValueField ="CourseName"  Style ="z-index: 106; left: 388px;
            position: absolute; top: 95px"
 Enabled ="False" >
        
</ asp:DropDownList >
        
< asp:AccessDataSource  ID ="dsCourse"  runat ="server"  DataFile ="~/App_Data/#SGMS_Data.mdb"
            SelectCommand
="SELECT DISTINCT [CourseName] FROM [Course] WHERE ([Department] = ?)" >
            
< SelectParameters >
                
< asp:ControlParameter  ControlID ="ddDepart"  Name ="Department"  PropertyName ="SelectedValue"
                    Type
="String"   />
            
</ SelectParameters >
        
</ asp:AccessDataSource >
    
    
</ div >
    
</ form >
</ body >
</ html >

 后台:

using  System;
using  System.Data;
using  System.Data.OleDb;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Text.RegularExpressions;

public   partial   class  Admin_FromExcel : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
// If not Login
        if (Session["UserName"].ToString().Trim() == "")
        
{
            Response.Redirect(
@"~Login.aspx");
        }

    }

    
protected void bnsubmit_Click(object sender, EventArgs e)
    
{

        
if (this.fuExcel.PostedFile != null)
        
{
            
Check&Save

            
Connect Excel & fill excelDs

            
Connect DB & fill dbDs

            
Insert data to DB
        }

        
else
        
{
            Response.Write(
"<script language='javascript'>alert('文件路径有误,请查证!');</script>");
        }

    }


    
protected void cbCourse_CheckedChanged(object sender, EventArgs e)
    
{
        ddCourse.Enabled 
= !ddCourse.Enabled;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值