using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Excel=Microsoft.Office.Interop.Excel;
namespace WebApplication3._56
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Hashtable users = readExcel();
String S = "";
foreach (DictionaryEntry user in users)
{
Console.WriteLine(user.Key+":"+user.Value);
S = S + user.Key + ":" + user.Value + ";";
}
Response.Write(S);
}
public static Hashtable readExcel()
{
Excel.Application ExcelApp = new Excel.Application(); ;
try
{
//2.打开已经存在的工作簿
string path = "C:\\Users\\zewei.cao\\Desktop\\Copy of users.xlsx";
//ExcelApp.Workbooks.Open(path, ReadOnly: true);
Hashtable h = new Hashtable();
Excel.Workbook wb = ExcelApp.Application.Workbooks.Open(path, ReadOnly: true);
Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets.get_Item(1);
int rowsint = ws.UsedRange.Cells.Rows.Count; //得到行数
Excel.Range rng1 = ws.Cells.get_Range("A2", "A" + rowsint);
Excel.Range rng2 = ws.Cells.get_Range("B2", "B" + rowsint);
Excel.Range rng3 = ws.Cells.get_Range("C2", "C" + rowsint);
Excel.Range rng4 = ws.Cells.get_Range("D2", "D" + rowsint);
Excel.Range rng5 = ws.Cells.get_Range("E2", "E" + rowsint);
Excel.Range rng6 = ws.Cells.get_Range("F2", "F" + rowsint);
object[,] arry1 = (object[,])rng1.Value2; //get range’s value
object[,] arry2 = (object[,])rng2.Value2;
object[,] arry3 = (object[,])rng3.Value2;
object[,] arry4 = (object[,])rng4.Value2;
object[,] arry5 = (object[,])rng5.Value2;
object[,] arry6 = (object[,])rng6.Value2;
string[,] arry = new string[rowsint - 1, 6];
for (int i = 1; i <= rowsint - 2; i++)
{
arry[i - 1, 0] = arry1[i, 1].ToString();
arry[i - 1, 1] = arry2[i, 1].ToString();
arry[i - 1, 2] = arry3[i, 1].ToString();
arry[i - 1, 3] = arry4[i, 1].ToString();
arry[i - 1, 4] = arry5[i, 1].ToString();
arry[i - 1, 5] = arry6[i, 1].ToString();
}
return h;
}
catch
{
return null;
}
finally
{
//5.关闭工作簿
ExcelApp.ActiveWorkbook.Close();
//6.退出excel
ExcelApp.Quit();
}
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3._56.WebForm1" %>
<%@ Import Namespace ="WebApplication3._56" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<h1 align="center">用户详细信息</h1>
<table border="1px" width="800px" align="center" cellpadding="0px" cellspacing="0px">
<th>
<td>用户名</td>
<td>年龄</td>
<td>性别</td>
<td>地址</td>
<td>民族</td>
<td>电话</td>
<td>编辑</td>
</th>
<%Hashtable users = WebForm1.readExcel();
int i = 0;
foreach (DictionaryEntry u in users)
{
i++;
%>
<tr>
<td><%=i %></td>
<td><%=u.Key %></td>
<td><%=u.Value %></td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
<% } %>
</table>
</body>
</html>