读取excel内容,并显示
1.VB版本
注意:excel版本的不同和对文件的读写权限
读取文件后台代码:
Imports System.Data.OleDb
Imports System.Data
Partial Class ceshi_Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
bangding()
End If
End Sub
Protected Sub bangding()
Dim path As String = Server.MapPath("upload/Test3.xls")
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + ";" + "Extended Properties=Excel 8.0;"
Dim conn As OleDbConnection = New OleDbConnection(strConn)
conn.Open()
Dim strExcel As String = ""
Dim myCommand As OleDbDataAdapter = Nothing
Dim ds As DataSet = Nothing
strExcel = "select * from [sheet1$]"
myCommand = New OleDbDataAdapter(strExcel, strConn)
ds = New DataSet()
myCommand.Fill(ds, "table1")
conn.Close()
Repeater2.DataSource = ds.Tables(0).DefaultView
Repeater2.DataBind()
End Sub
End Class
读取文件前台代码:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="ceshi_Default" %>
<!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></title>
<style type="text/css">
*
{
margin:0px; padding:0px;
}
</style>
</head>
<body style="text-align:center;">
<form id="form1" runat="server">
<div style=" width:900px; text-align:left; padding:0px; margin:0px; height:422px; background-image: url('back.jpg')">
<asp:Repeater ID="Repeater2" runat="server">
<HeaderTemplate>
<table style="width:73%; border:0px; margin-top:105px; margin-left:98px; color:White; font-size:13px;">
<tr><td colspan="4" style="word-spacing:1px; border-bottom:1px solid blue">
This is a general notice that there will be a system maintenance affecting product registration services from </td></tr>
<tr style="width:100%;">
<td style="width:35%; text-align:left;">Area</td><td style="width:20%; text-align:center;">Time Zone</td><td style="width:20%; text-align:center;">From</td><td style="width:25%; text-align:center;">To</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label ID="Label1" runat="server" Text='<%# Eval("Area") %>'></asp:Label></td>
<td style="text-align:center;"><asp:Label ID="Label2" runat="server" Text='<%# Eval("Time") %>'></asp:Label></td>
<td style="text-align:center;"><asp:Label ID="Label3" runat="server" Text='<%# Eval("From") %>'></asp:Label></td>
<td style="text-align:center;"><asp:Label ID="Label4" runat="server" Text='<%# Eval("To") %>'></asp:Label></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
<div style="margin-left:98px; color:White; font-size:13px; margin-top:5px; width:750px; height:40px; border-bottom:1px solid blue">
The system will not be able to access during the maintenance hours, please save your work and log off before the maintenance start time .<br/>
thank you for your understanding and support, we are sorry for any inconvenience caused.
</div>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
上传文件后台:
Partial Class ceshi_upfile
Inherits System.Web.UI.Page
'上传按钮
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim b As Boolean = Upload(FileUpload1) '上传excel文件
If (Not b) Then
Return
End If
' Dim name As String = FileUpload1.FileName
' Dim filepath As String = Server.MapPath("upload/") + name
End Sub
'上传文件方法
Private Function Upload(ByVal myFileUpload As FileUpload) As Boolean
Dim flag As Boolean = False
'是否允许上载
Dim fileAllow As Boolean = False
'设定允许上载的扩展文件名类型
Dim allowExtensions As String() = {".xls"}
'取得网站根目录路径
Dim path As String = HttpContext.Current.Request.MapPath("upload/")
'检查是否有文件案
If myFileUpload.HasFile Then
'取得上传文件之扩展文件名,并转换成小写字母
Dim fileExtension As String = System.IO.Path.GetExtension(myFileUpload.FileName).ToLower()
'检查扩展文件名是否符合限定类型
For i As Integer = 0 To allowExtensions.Length - 1
If fileExtension = allowExtensions(i) Then
fileAllow = True
End If
Next
If (fileAllow) Then
Try
'存储文件到文件夹
myFileUpload.SaveAs(path + myFileUpload.FileName)
lblMes.Text = "文件导入成功"
flag = True
Catch ex As Exception
lblMes.Text += ex.Message
flag = False
End Try
Else
lblMes.Text = "不允许上载:" + myFileUpload.PostedFile.FileName + ",只能上传xls的文件,请检查!"
flag = False
End If
Else
lblMes.Text = "请选择要导入的excel文件!"
flag = False
End If
Return flag
End Function
End Class
2.C#版本(转载牛腩的)
- // 上传按钮
- protected void btnUp_Click(object sender, EventArgs e)
- {
- bool b = Upload(fuExcel); // 上传excel文件
- if (!b)
- {
- return;
- }
- string name = fuExcel.FileName;
- string filepath = Server.MapPath("~/upload/") + name;
- DataSet ds = ExcelDataSource(filepath, ExcelSheetName(filepath)[0].ToString());
- GridView1.DataSource = ds;
- GridView1.DataBind();
- }
- //上传文件方法
- private bool Upload(FileUpload myFileUpload)
- {
- bool flag = false;
- //是否允许上载
- bool fileAllow = false;
- //设定允许上载的扩展文件名类型
- string[] allowExtensions = { ".xls" };
- //取得网站根目录路径
- string path = HttpContext.Current.Request.MapPath("~/upload/");
- //检查是否有文件案
- if (myFileUpload.HasFile)
- {
- //取得上传文件之扩展文件名,并转换成小写字母
- string fileExtension = System.IO.Path.GetExtension(myFileUpload.FileName).ToLower();
- //检查扩展文件名是否符合限定类型
- for (int i = 0; i < allowExtensions.Length; i++)
- {
- if (fileExtension == allowExtensions[i])
- {
- fileAllow = true;
- }
- }
- if (fileAllow)
- {
- try
- {
- //存储文件到文件夹
- myFileUpload.SaveAs(path + myFileUpload.FileName);
- lblMes.Text = "文件导入成功";
- flag = true;
- }
- catch (Exception ex)
- {
- lblMes.Text += ex.Message;
- flag = false;
- }
- }
- else
- {
- lblMes.Text = "不允许上载:" + myFileUpload.PostedFile.FileName + ",只能上传xls的文件,请检查!";
- flag = false;
- }
- }
- else
- {
- lblMes.Text = "请选择要导入的excel文件!";
- flag = false;
- }
- return flag;
- }
- //该方法实现从Excel中导出数据到DataSet中,其中filepath为Excel文件的绝对路径, sheetname为excel文件中的表名
- public DataSet ExcelDataSource(string filepath, string sheetname)
- {
- string strConn;
- strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
- OleDbConnection conn = new OleDbConnection(strConn);
- OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "]", strConn);
- DataSet ds = new DataSet();
- oada.Fill(ds);
- conn.Close();
- return ds;
- }
- //获得Excel中的所有sheetname。
- public ArrayList ExcelSheetName(string filepath)
- {
- ArrayList al = new ArrayList();
- string strConn;
- strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
- OleDbConnection conn = new OleDbConnection(strConn);
- conn.Open();
- DataTable sheetNames = conn.GetOleDbSchemaTable
- (System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
- conn.Close();
- foreach (DataRow dr in sheetNames.Rows)
- {
- al.Add(dr[2]);
- }
- return al;
- }

浙公网安备 33010602011771号