C#导入Excel文件,并用repreater显示

后台

 

 //导入要通知的员工
    protected void btnLoad_Click(object sender, EventArgs e)
    {
        if (flup.HasFile)
        {
            DataTable dt = TransferData(flup.PostedFile.FileName, "sheet1");
            if (dt != null)
            {
                if (dt.Columns.Contains("姓名") && dt.Columns.Contains("手机号码") && dt.Columns.Contains("机构简称"))
                {
                    rpt_EmployeeList.DataSource = dt;
                    rpt_EmployeeList.DataBind();
                }
            }
        }
        else
        {
            PageHelp.Alert(this, "", "请选择要导入的文件!");
        }
    }

    /// <summary>
    /// 读取Excel数据到DataSet
    /// </summary>
    /// <param name="excelFile">EXCEL的绝对路径</param>
    /// <param name="sheetName">EXCEL名称————表名</param>
    /// <returns></returns>
    public DataTable TransferData(string excelFile, string sheetName)
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        try
        {
            ExcelOperate excelOperate = new ExcelOperate();
            Excel.Application xApp = new Excel.ApplicationClass();

            Excel.Workbook xBook = xApp.Workbooks._Open(excelFile,
            Missing.Value, Missing.Value, Missing.Value, Missing.Value
            , Missing.Value, Missing.Value, Missing.Value, Missing.Value
            , Missing.Value, Missing.Value, Missing.Value, Missing.Value);

            Excel.Worksheet xSheet = (Excel.Worksheet)xBook.Sheets[1];
            string table = xSheet.Name;

            excelOperate.Dispose(xSheet, xBook, xApp);
            //获取全部数据           
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFile + ";Extended Properties=Excel 8.0;";
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            string strExcel = "";
            OleDbDataAdapter myCommand = null;
            strExcel = string.Format("select * from [{0}$]", table);
            myCommand = new OleDbDataAdapter(strExcel, strConn);
            myCommand.Fill(ds, sheetName);
            conn.Close();
            dt = ds.Tables[0];
            return dt;
        }
        catch (Exception ex)
        {
            PageHelp.Alert(ex.ToString());
            return null;
        }
    }


    /// <summary>
    /// 判断Excel列值是否有空值
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public DataTable ValidateDataTable(DataTable dt)
    {
        foreach (DataRow row in dt.Rows)
        {
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                if (string.IsNullOrEmpty(row[i].ToString()))
                {
                    row[i] = DBNull.Value;
                }
            }
            if (string.IsNullOrEmpty(row["姓名"].ToString()) && string.IsNullOrEmpty(row["身份证"].ToString()))
            {
                row.Delete();
            }
        }
        return dt;
    }

 

 

前台

 

 <table cellpadding="0" cellspacing="0" class="table">
                    <tbody style="margin-left: 40px">
                        <tr>
                            <th style="width: 5%">
                                姓名
                            </th>
                            <th style="width: 7%">
                                手机号码
                            </th>
                            <th style="width: 7%">
                                机构简称
                            </th>
                        </tr>
                        <asp:Repeater ID="rpt_EmployeeList" runat="server">
                            <ItemTemplate>
                                <tr>
                                    <td>
                                        <%#Eval("姓名")%>
                                    </td>
                                    <td>
                                        <%#Eval("手机号码")%>
                                    </td>
                                    <td>
                                        <%#Eval("机构简称")%>
                                    </td>
                                </tr>
                            </ItemTemplate>
                        </asp:Repeater>
                    </tbody>
                </table>

 

 

导入的excel文件格式

 

姓名手机号码机构简称
发生电45345 电风扇的法
按时的3244234  地方感到法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值