这是绑定的. <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <script language="VB" runat="server"> Sub Page_Load(Sender As Object, E As EventArgs) Dim strConn As String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.Mappath("grade.xls") & ";Extended Properties=Excel 8.0" '该句表示连接到Excel文件 Dim conn As New OleDbConnection(strConn) '建立Connection对象 Dim adp As New OleDbDataAdapter("SELECT * FROM [Sheet1$]", conn) '建立DataAdapter对象 Dim ds As New DataSet() '建立DataSet对象 adp.Fill(ds, "link") '填充DataSet对象 MyDataGrid.DataSource=DS.Tables("link").DefaultView '指定数据源 MyDataGrid.DataBind() '执行绑定 End Sub </script> <html> <body> <h4 align="center">读取Excel文件</h4> <asp:DataGrid id="MyDataGrid" width="100%" HeaderStyle-BackColor="#BCBCBC" runat="server"/> <body> </html>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="VB" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
Dim strConn As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.Mappath("grade.xls") & ";Extended Properties=Excel 8.0" '该句表示连接到Excel文件
Dim conn As New OleDbConnection(strConn) '建立Connection对象
Dim adp As New OleDbDataAdapter("SELECT * FROM [Sheet1$]", conn) '建立DataAdapter对象
Dim ds As New DataSet() '建立DataSet对象
adp.Fill(ds, "link") '填充DataSet对象
MyDataGrid.DataSource=DS.Tables("link").DefaultView '指定数据源
MyDataGrid.DataBind() '执行绑定
End Sub
</script>
<html>
<body>
<h4 align="center">读取Excel文件</h4>
<asp:DataGrid id="MyDataGrid" width="100%" HeaderStyle-BackColor="#BCBCBC" runat="server"/>
<body>
</html>
private void btnExport_Click(object sender, System.EventArgs e)
{
int PaperTemplateID=int.Parse(txtPaperTemlateID.Text); string msg = "";
//设置上传目录和上传文件名
string directory = this.Server.MapPath("Temp"); //获取上传路径
string saveFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"; //设置上传文件名
string filePath = directory + @"\" + saveFileName; //上传文件全路径
System.IO.File.Copy(this.Server.MapPath("Template") + @"\PaperTemplate.xls",filePath); System.Data.OleDb.OleDbConnection oleConn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath + ";Extended Properties=Excel 8.0;");//
System.Data.OleDb.OleDbCommand oleCommand = new OleDbCommand();
try
{
BPaperTemplate objBPT=new BPaperTemplate(Session["PersonID"].ToString());
DataSet ds=objBPT.ExportPaperTemplateContentByExcel(PaperTemplateID);
if(ds==null)
throw new Exception("导出试卷到Excel出错"); ds.DataSetName="TestPaper"; oleCommand.Connection = oleConn;
oleConn.Open(); //插入试卷基本信息
oleCommand.CommandText = "INSERT INTO [试卷基本信息$] " +
"( [试卷名称], [总分], [描述], [试卷状态], [创建时间], [考试时间长度]) " +
"VALUES ( '"+ds.Tables[0].Rows[0]["Name"]+"','"+ds.Tables[0].Rows[0]["TotalScore"]+"','"+ds.Tables[0].Rows[0]["Description"]+"','"+ds.Tables[0].Rows[0]["State"]+"','"+ds.Tables[0].Rows[0]["CreateTime"]+"','"+ds.Tables[0].Rows[0]["TestLong"]+"')";
oleCommand.ExecuteNonQuery();
oleConn.Close();
string ExcelName = ds.Tables[0].Rows[0]["Name"].ToString();
this.SendExclFile(filePath, ExcelName); msg = "文件导出成功。";
}
catch(Exception err)
{
Common.ErrorNavigate.IExceptionMana(this.Page,err.Message);
} this.lblMsg.Text = msg;
}
/// <summary>
/// 发送 Excel 文件
/// </summary>
/// <param name="ms"></param>
private void SendExclFile(string filePath, string ExcelName)
{
Response.ClearContent();
Response.ContentType="application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=test.xls");
Response.WriteFile(filePath);
Response.Flush();
Response.End();
}
BPaperTemplate objBPT=new BPaperTemplate(Session["PersonID"].ToString());
有問題可不可以把所有源碼發給我Email:[email protected]
謝謝!