主要有两部分:
1、用一个Iframe来上传文件 upload.aspx
2、用Ajax实现无刷新显示 index.aspx
代码如下
一、index.aspx
<html>
<head runat="server">
<title></title>
<script>
function callBack(fileName)
{
document.getElementById('<%=hidFileFullName.ClientID %>').value=fileName;
document.all.hidBt.click();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<atlas:ScriptManager ID="ScriptManager1" runat=server></atlas:ScriptManager>
<atlas:UpdatePanel ID="UpdatePanel1" runat=server>
<ContentTemplate>
<table border="0" cellpadding="0" cellspacing="0">
<tr height=30 valign=middle width=500>
<td><iframe id="file" name="file" frameborder=no noresize scrolling=no width=100% height=30 src="ImportFromExcelIframe.aspx"></iframe></td><td><input type=submit value="" style="width:0" id="hidBt" /></td>
</tr>
<tr>
<td colspan=2></td>
</tr>
<tr>
<td colspan=2>
<asp:DataGrid ID="d1" runat=server ShowHeader=False BackColor="White" BorderColor="White" BorderWidth="0px" CellPadding="3" CellSpacing="1" GridLines="None">
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<SelectedItemStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<ItemStyle BackColor="WhiteSmoke" BorderColor="White" BorderStyle="Dotted" BorderWidth="1px"
ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
</asp:DataGrid>
</td>
</tr>
</table>
</ContentTemplate>
</atlas:UpdatePanel>
<input type=hidden id="hidFileFullName" runat=server />
<input type=hidden id="hidColumns" runat=server />
</form>
</body>
</html>
index.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
Bind();
}
DataTable GetDataTable()
{
DataTable dt = null;
System.Data.OleDb.OleDbConnection oExcelConn = null;
try
{
string sExcelConn = "";
sExcelConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + this.hidFileFullName.Value + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
oExcelConn = new System.Data.OleDb.OleDbConnection(sExcelConn);
if (oExcelConn.State != ConnectionState.Open)
oExcelConn.Open();
DataTable oSheets = oExcelConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
string sTableName = string.Empty;
sTableName = oSheets.Rows[0]["TABLE_NAME"].ToString();
if (oSheets.Rows.Count > 0)
{
dt = new DataTable();
System.Data.OleDb.OleDbDataAdapter daCashFlow = new System.Data.OleDb.OleDbDataAdapter();
System.Data.DataSet dsCashFlow = new DataSet();
daCashFlow.SelectCommand = new System.Data.OleDb.OleDbCommand("Select top 10 * From [" + sTableName + "]");
daCashFlow.SelectCommand.Connection = oExcelConn;
daCashFlow.Fill(dt);
}
}
catch (Exception ex)
{
if (oExcelConn.State != ConnectionState.Closed)
oExcelConn.Close();
oExcelConn.Dispose();
}
finally
{
if (oExcelConn.State != ConnectionState.Closed)
oExcelConn.Close();
oExcelConn.Dispose();
}
return dt;
}
void Bind()
{
DataTable dt = GetDataTable();
if (dt != null)
{
DataTable dtCopy = new DataTable();
for (int i = 0; i < dt.Columns.Count; i++)
dtCopy.Columns.Add("", typeof(string));
DataRow dr = dtCopy.NewRow();
string strOption = string.Empty;
DataSet dsOption = DataAccessHelper.ExecuteDataset("select LdtFldName,LdtFldDescStringCode from Sys_ListDt where LdtLstNo='MB.ExpenseTrx' and LdtFldDisplay=1");
foreach (DataRow drOption in dsOption.Tables[0].Rows)
strOption += string.Format("<option value={1}>{0}</option>", Strings.GetString(drOption["LdtFldDescStringCode"].ToString()), drOption["LdtFldName"].ToString());
for (int o = 0; o < dt.Columns.Count; o++)
dr[o] = string.Format("<select id='selOption{0}'>{1}</select>", o.ToString(), strOption);
dtCopy.Rows.Add(dr);
foreach (DataRow drBuffer in dt.Rows)
{
DataRow drTemp = dtCopy.NewRow();
for (int ii = 0; ii < dt.Columns.Count; ii++)
drTemp[ii] = drBuffer[ii].ToString();
dtCopy.Rows.Add(drTemp);
}
d1.DataSource = dtCopy;
d1.DataBind();
}
return;
}
二、 upload.aspx
<html>
<head>
<title></title>
</head>
<body leftmargin="0" topmargin="0">
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>选择文件:</td><td align=left><asp:FileUpload ID="fileImport" runat="server" /><asp:Button ID="Button1" runat="server" Text="UpLoadFile" OnClick="Button1_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
upload.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string sFilePath = string.Empty;
string sFileName = string.Empty;
sFilePath = Server.MapPath("../UpLoad") + "//temp//";
if (!System.IO.Directory.Exists(sFilePath))
System.IO.Directory.CreateDirectory(sFilePath);
if (this.fileImport.PostedFile.FileName.Trim() != "")
{
sFileName = System.IO.Path.GetFileName(fileImport.PostedFile.FileName);
fileImport.PostedFile.SaveAs(sFilePath + sFileName);
this.ClientScript.RegisterStartupScript(GetType(), "js", "window.top.callBack(/"" + Convert.ToString(sFilePath + sFileName).Replace("//", "/") + "/")", true);
}
else
return;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return;
}
}