0

Excelファイルに送信される行をフィルタリングしようとしています。行データをExcelにエクスポートする方法を理解できましたが、バージョンドロップダウンを追加したため、フィルター処理されたデータをExcelシートに送信する必要があります。ドロップダウンから見たいバージョンを選択するとどうなりますか。

元。バージョンは12.1です。

ビューには、バージョン番号12.1が関連付けられているデータのみが返されます。フィルター処理された行をExcelファイルにダウンロードしたい場合。ダウンロードしてExcelファイルボタンを押すと、フィルターされた行ではなくすべての行が表示されることを除いて、Excelファイルでファイルが開きます。私がしなければならなかったのは、適切なviewbag変数とviewdata変数を呼び出しに追加することだけで、それは機能するだろうと思いました。私のコードを見て、私が間違っていることを理解するのを手伝ってくれませんか?以下のコードを投稿しました。より良い診断のためにさらに情報が必要な場合はお知らせください。頻繁にチェックします。ご協力いただきありがとうございます!

Excelコントローラー

[Authorize]
        public ActionResult PaExcelOutput(string sortField, string currentFilter, string searchString, int? page, string VERS)
        {

            int pageNumber = (page ?? 1); // start page number

            var PaExcel = from P in db.iamp_mapping
                          select P;


            ViewBag.CurrentField = sortField = String.IsNullOrEmpty(sortField) ? "IAMP_PK" : sortField; // Provides the field to sort
            ViewBag.CurrentFilter = searchString; // Provides the view with the current filter string

            ViewData["SelectedVersion"] = VERS;
            ViewData["currentFilter"] = currentFilter;
            ViewData["VERS"] = new SelectList(PaExcel.Select(x => x.VERSION).Distinct());

            if (!String.IsNullOrEmpty(VERS))
            {
                PaExcel = PaExcel.Where(p => p.VERSION.ToUpper().Contains(VERS));
            }

            if (Request.HttpMethod == "GET")
            {
                searchString = currentFilter; //sets the currentFilter equal to Searchstring
            }

            else
            {
                page = 1;                   // defaults to page 1
            }




            return View(PaExcel);
        }

Excel出力ビュー

@model IEnumerable<DBFirstMVC.Models.iamp_mapping>

<!--This is what the MVC uses to output the correct rows to Excel -->

@{
    Layout = null;
    Response.AddHeader("Content-Type", "application/vnd.ms-excel");  
}




<table>
    <tr>
        <th>
            PA
        </th>
        <th>
            VERSION
        </th>
        <th>
            INVESTMENT AREA
        </th>
        <th>
            MAJOR PROGRAM
        </th>
        <th>
            PA SUB PROGRAM
        </th>
        <th>
            VP
        </th>
        <th>
            DIRECTOR
        </th>
        <th>
            SPA
        </th>
        <th>
            PA INITIATIVE
        </th>
        <th>
            NOTES
        </th>
        <th>
            TEAM BC
        </th>
        <th>
            PA CC
        </th>
        <th>
            PA DESCRIPTION
        </th>
        <th>
            PA MANAGER
        </th>
        <th>
            SPA CC
        </th>
        <th>
            TIER 1
        </th>
        <th>
            TIER 2
        </th>
        <th>
            TIER 3
        </th>
        <th>
            TIER 4
        </th>
        <th>
            CFP VIEW NV/NONNV
        </th>
        <th>
            CFP VIEW
        </th>
        <th>
            CREATION DATE
        </th>
        <th>
            CAF PROJECT NAME
        </th>
        <th>
            CAF IA
        </th>
        <th>
            CAF MP
        </th>
        <th>
            CAF LEAD DIR
        </th>
        <th>
            CAF LEAD MGR
        </th>
        <th>
            CAF CC DIR
        </th>
        <th>
            CAF CC MGR
        </th>
        <th>
            CAF CC
        </th>
        <th>
            CAF ID
        </th>
        <th>
            CREATED ON
        </th>
        <th>
            LAST EDITED
        </th>
        <th>
            PROJECT STATUS
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.PA)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.VERSION)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.INVESTMENT_AREA)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MAJOR_PROGRAM)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PA_SUB_PROGRAM)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.VP)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DIRECTOR)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.SPA)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PA_INITIATIVE)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NOTES)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TEAM_BC)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PA_CC)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PA_DESCRIPTION)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PA_MANAGER)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.SPA_CC)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TIER_1)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TIER_2)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TIER_3)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TIER_4)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CFP_VIEW_NV_NONNV)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CFP_VIEW)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CREATION_DATE)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CAF_PROJECT_NAME)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CAF_IA)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CAF_MP)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CAF_LEAD_DIR)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CAF_LEAD_MGR)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CAF_CC_DIR)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CAF_CC_MGR)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CAF_CC)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CAF_ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CREATED_ON)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LAST_EDITED)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PROJECT_STATUS)
        </td>

    </tr>
}

</table>

インデックスビュー

<!--For stack overflow, This is where the actionlink that the download to excel button is located it is at the bottom of this code snippet called PAExcelOutput-->


@model PagedList.IPagedList<DBFirstMVC.Models.iamp_mapping>


@{
    ViewBag.Title = "PA Mapping";

}

@using PagedList;


<h2 class="corporate sifr">@ViewBag.Title</h2>


<div class="crossband">
@using (Html.BeginForm())
{

    <div class="lefty">        
        Filter by Version: @Html.DropDownList("VERS", null, "All", new { @class = "text" }) and by Criteria: @Html.TextBox("SearchString", "", new { @class = "text" })        
    </div>    
    <input type = "submit" class = "button1" value = "Go" />
}

<div class="righty">
    @Html.ActionLink("Add a new PA to the database", "Create", "Pa", null, new { @class = "button1" })

</div>
</div>

<div class="crossband">
    <div class="lefty">   
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
    of @Model.PageCount
    &nbsp;&nbsp;&nbsp;&nbsp;
    @if (Model.HasPreviousPage)
    {
        @Html.ActionLink("<<", "", new { page = 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, VERS = ViewBag.SelectedVersion })
        @Html.Raw(" ");
        @Html.ActionLink("< Prev", "", new { page = Model.PageNumber - 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, VERS = ViewBag.SelectedVersion })
    }
    else
    {
        @:<<
        @Html.Raw(" ");
        @:< Prev
    }

    @if (Model.HasNextPage)
    {
        @Html.ActionLink("Next >", "", new { page = Model.PageNumber + 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, VERS = ViewBag.SelectedVersion })
        @Html.Raw(" ");
        @Html.ActionLink(">>", "", new { page = Model.PageCount, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, VERS = ViewBag.SelectedVersion })
    }
    else
    {
        @:Next >
        @Html.Raw(" ")
        @:>>
    }
    </div>
    <div class="righty">
        Showing Records @Model.FirstItemOnPage to @Model.LastItemOnPage from @Model.TotalItemCount records
    </div>
</div>


<table>
    <tr>
        <th>
            @Html.ActionLink("PA", "", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "PA", VERS = ViewBag.SelectedVersion })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "PA")
            {<img id="asc" src="@Url.Content("~/Images/ico_tablesortoffset_asc.gif")" alt = "Asc Arrow" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "PA")
            {<img id="desc" src="@Url.Content("~/Images/ico_tablesortoffset_desc.gif")" alt = "Desc Arrow" />}
        </th>
        <th>        
            @Html.ActionLink("MAJOR PROGRAM", "", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "MAJOR_PROGRAM", VERS = ViewBag.SelectedVersion })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "MAJOR_PROGRAM")
            {<img id="asc" src="@Url.Content("~/Images/ico_tablesortoffset_asc.gif")" alt = "Asc Arrow" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "MAJOR_PROGRAM")
            {<img id="desc" src="@Url.Content("~/Images/ico_tablesortoffset_desc.gif")" alt = "Desc Arrow" />}           
        </th>
        <th>
            @Html.ActionLink("INVESTMENT AREA", "", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "INVESTMENT_AREA", VERS = ViewBag.SelectedVersion })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "INVESTMENT_AREA")
            {<img id="asc" src="@Url.Content("~/Images/ico_tablesortoffset_asc.gif")" alt = "Asc Arrow" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "INVESTMENT_AREA")
            {<img id="desc" src="@Url.Content("~/Images/ico_tablesortoffset_desc.gif")" alt = "Desc Arrow" />}   
        </th>
        <th>
            @Html.ActionLink("Version", "", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "VERSION", VERS = ViewBag.SelectedVersion })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "VERSION")
            {<img id="asc" src="@Url.Content("~/Images/ico_tablesortoffset_asc.gif")" alt = "Asc Arrow" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "VERSION")
            {<img id="desc" src="@Url.Content("~/Images/ico_tablesortoffset_desc.gif")" alt = "Desc Arrow" />}   
        </th>
        <th>
            @Html.ActionLink("VP/SR. DIRECTOR", "", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "VP", VERS = ViewBag.SelectedVersion })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "VP")
            {<img id="asc" src="@Url.Content("~/Images/ico_tablesortoffset_asc.gif")" alt = "Asc Arrow" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "VP")
            {<img id="desc" src="@Url.Content("~/Images/ico_tablesortoffset_desc.gif")" alt = "Desc Arrow" />}   
        </th>
        <th>
            @Html.ActionLink("DIRECTOR/SR. MGR", "", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "DIRECTOR", VERS = ViewBag.SelectedVersion })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "DIRECTOR")
            {<img id="asc" src="@Url.Content("~/Images/ico_tablesortoffset_asc.gif")" alt = "Asc Arrow" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "DIRECTOR")
            {<img id="desc" src="@Url.Content("~/Images/ico_tablesortoffset_desc.gif")" alt = "Desc Arrow" />}   
        </th>       
        <th></th>
    </tr>
@{
    var row_class = "odd";
}

@foreach (var item in Model)
{
    row_class = row_class == "odd" ? "even" : "odd";    
    <tr class="@row_class">
        <td>
            @Html.DisplayFor(modelItem => item.PA)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MAJOR_PROGRAM)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.INVESTMENT_AREA)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.VERSION)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.VP)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DIRECTOR)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.IAMP_PK }) |

            @Html.ActionLink("Delete", "Delete", new { id = item.IAMP_PK })
        </td>
    </tr>
}

</table>

<div class="crossband">
    <div class="lefty">
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
    of @Model.PageCount
    &nbsp;&nbsp;&nbsp;&nbsp;
    @if (Model.HasPreviousPage)
    {
        @Html.ActionLink("<<", "", new { page = 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, VERS = ViewBag.SelectedVersion })
        @Html.Raw(" ");
        @Html.ActionLink("< Prev", "", new { page = Model.PageNumber - 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, VERS = ViewBag.SelectedVersion })
    }
    else
    {
        @:<<
        @Html.Raw(" ");
        @:< Prev
    }

    @if (Model.HasNextPage)
    {
        @Html.ActionLink("Next >", "", new { page = Model.PageNumber + 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, VERS = ViewBag.SelectedVersion })
        @Html.Raw(" ");
        @Html.ActionLink(">>", "", new { page = Model.PageCount, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, VERS = ViewBag.SelectedVersion })
    }
    else
    {
        @:Next >
        @Html.Raw(" ")
        @:>>
    }
    </div>
    <div class="righty">
    Showing Records @Model.FirstItemOnPage to @Model.LastItemOnPage from @Model.TotalItemCount records
    </div>
</div>
<br/><br/>
<div class="righty">
@Html.ActionLink("Download in Excel Format", "PaExcelOutput", "Pa", new { VERS = ViewBag.SelectedVersion, currentFilter = ViewBag.currentFilter, sortOrder = ViewBag.CurrentSort, @class = "button1" })

</div>
4

3 に答える 3

1

相互運用機能を使用して、このCodeWorkedExcelを試してください。

コード:

private System.Data.DataTable GetExcelData(string fileName)
        {
            Application excelApp = null;
            Workbook excelWorkBook = null;
            Worksheet excelSheet;
            Range range;

            try
            {
                //creat a Application object
                excelApp = new Application();

                //   get   WorkBook  object
                excelWorkBook = excelApp.Workbooks.Open(fileName, Missing.Value, Type.Missing,
                        Missing.Value, Missing.Value, Missing.Value,
                        Missing.Value, Missing.Value, Missing.Value,
                        Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                        Missing.Value, Missing.Value);

                //   get   WorkSheet object
                excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelWorkBook.Sheets[1];



                int columnCount = excelSheet.UsedRange.Columns.Count;

                excelSheet.Columns.ClearFormats();
                excelSheet.Rows.ClearFormats();

                columnCount = excelSheet.UsedRange.Columns.Count;
                int rowsCount = excelSheet.UsedRange.Rows.Count;

                Range last = excelSheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing);
                range = excelSheet.get_Range("A2", last);
                object[,] values = (object[,])range.get_Value(Type.Missing);

                System.Data.DataTable dt = new System.Data.DataTable("TicketUpload");
                for (int c = 1; c <= columnCount; c++)
                {
                    if (excelSheet.Cells[1, c].Value2 != null)
                    {
                        string columnName = excelSheet.Columns[c].Address;
                        Regex reg = new Regex(@"(\$)(\w*):");
                        if (reg.IsMatch(columnName))
                        {
                            Match match = reg.Match(columnName);
                            dt.Columns.Add(Convert.ToString(excelSheet.Cells[1, c].Value2).ToUpper(), typeof(string));
                        }
                    }
                }

                for (int i = 1; i <= values.GetLength(0); i++)
                {
                    DataRow dr = dt.NewRow();
                    for (int j = 1; j <= dt.Columns.Count; j++)
                    {
                        if (values[i, j] == null)
                            dr[j - 1] = DBNull.Value;
                        else
                            dr[j - 1] = values[i, j];
                    }
                    dt.Rows.Add(dr);
                }

                range = null;
                excelSheet = null;
                if (excelWorkBook != null)
                    excelWorkBook.Close(false, Missing.Value, Missing.Value);
                excelWorkBook = null;
                if (excelApp != null)
                    excelApp.Quit();
                excelApp = null;

                return dt;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                range = null;
                excelSheet = null;
                if (excelWorkBook != null)
                    excelWorkBook.Close(false, Missing.Value, Missing.Value);
                excelWorkBook = null;
                if (excelApp != null)
                    excelApp.Quit();
                excelApp = null;
            }
        }
于 2014-09-12T10:30:12.610 に答える
0

わかりました。1時間前にこの質問を投稿したばかりですが、それを理解しました。同じ問題が発生している場合は、他の人がこの回答を参考にしてくれることを願っています。私のExcel呼び出しの問題は、多くの変数が必要だったということでした。

ダウンロードボタンのアクションリンクは以前は次のようになりました

<div class="righty">
@Html.ActionLink("Download in Excel Format", "PaExcelOutput", "Pa", new { VERS = ViewBag.SelectedVersion, currentFilter = ViewBag.currentFilter, sortOrder = ViewBag.CurrentSort, @class = "button1" })

</div>

これに変更しました

<div class="righty">

@Html.ActionLink("Download in Excel Format", "PaExcelOutput", new { VERS = ViewBag.SelectedVersion, currentFilter = ViewBag.currentFilter, sortOrder = ViewBag.CurrentSort }, new { @class = "button1" })

</div>

文からを削除"Pa"することで、ビューバッグが変数をExcel出力に渡さない原因となっていた余分な変数を削除しました。

actionlinkメソッドは次のようになります。

ActionLink(HtmlHelper, String, String, Object, Object)

今私は知っています...そして知ることは戦いの半分です!

于 2012-07-12T15:19:05.320 に答える
0
<div class="toggle-contents">
        <table width="100%" id="qualitygoal">
            <tr>
                <td class="even" align="left">
                    @Html.Label("Project Id")
                </td>
                <td class="even" align="left">
                    @ViewBag.ProjectId
                </td>

            </tr>
            <tr>

                <td class="even" align="left">
                    @Html.Label("Project Name")
                </td>
                <td class="even" align="left">
                    @ViewBag.ProjectName
                </td>

            </tr>
            <tr>
                     <td class="even" align="left">
                    @Html.Label("Upload File")
                </td>
                <td class="even" align="left">
                <input type="file" name="file" /><br />
                </td>
                     </tr>
        </table>
        <table width="100%" id="goal">


            @using (Html.BeginForm("Index", "TicketDataUpload", FormMethod.Post, new { enctype = "multipart/form-data" }))
            { 

                <br />

                <div align="right">
                    <input type="submit" name="Submit" id="Submit" value="Upload" />
                    <input type="submit" name="Submit" value="NewTemplate" class="Add" id="NewTemplate" />
                    <input type="button" value="Close" class="cancel" />

                </div>
            }


        </table>
    </div>
于 2015-02-04T15:40:48.910 に答える