0927CODE

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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值