*需要從GridView “gvGrade"中將顯示數據導出 (網上搜來的)
{
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:
前臺:
<!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.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;
}
}