首页 > 解决方案 > 新手 ASP.net MVC 导出到 Excel

问题描述

我有一个应用程序,可以记录机构中的所有人。我有一个这样的页面在此处输入图像描述

当我导出到 excel 时,一切正常: 在此处输入图像描述

允许我这样做的功能是:

public void Test()
    {
        List<UserViewModel> emplist = db.users.Select(x => new UserViewModel
        {
            nom = x.nom,
            prenom = x.prenom,
            mobile = x.mobile,
            fixe = x.fixe,
            astreinte = x.astreinte
        }).ToList();

        ExcelPackage.LicenseContext = LicenseContext.Commercial;
        ExcelPackage pck = new ExcelPackage();
        ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Report"); 

我有所有数据,但它只是一行一行的,不是很清楚阅读或找到特定的东西。然后我有另一个按类别排序的页面:在此处输入图像描述

一个类别的代码是:

    <asp:repeater id="Repeater3" datasourceid="SqlDataSource3" runat="server">
                       <headertemplate>
          <table id="users" class="display">
           <thead> 
               <tr>
              <th><b>Nom</b></th>
              <th><b>Prenom</b></th>
              <th><b>Fixe</b></th>
              <th><b>Mobile</b></th>
              <th><b>Astreinte</b></th>
            </tr>
           </thead> 
              <tbody>
        </headertemplate>
           
        <itemtemplate>
           
          <tr>
            <td> <%# Eval("nom") %> </td>
            <td> <%# Eval("prenom") %> </td>
            <td> <%# Eval("fixe") %> </td>
            <td> <%# Eval("mobile") %> </td>
            <td> <%# Eval("astreinte") %> </td>
          </tr>
           
        </itemtemplate>
        <footertemplate>
            </tbody>
          </table>
        </footertemplate>
               
                   </asp:repeater>

               <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
             ConnectionString="<%$ ConnectionStrings:HD_TracabiliteConnectionString %>" 
             SelectCommand="SELECT * FROM [users] INNER JOIN catuser ON users.user_num = catuser.user_num WHERE catuser.categorie = 3" 
             ProviderName="<%$ ConnectionStrings:HD_TracabiliteConnectionString.ProviderName %>"></asp:SqlDataSource>
  </div> 

我想要做的是通过转换这一行SelectCommand="SELECT * FROM [users] INNER JOIN catuser ON users.user_num = catuser.user_num WHERE catuser.categorie = 3" 并将其集成到我的函数 Test() 按类别导出到 excel ?拜托,我真的需要帮助,求求你

标签: c#webformsexport-to-excel

解决方案


推荐阅读