Как экспортировать данные из asp.net MVC в Excel - PullRequest
0 голосов
/ 18 февраля 2019

привет и добрый день .. моя проблема связана с тем, как экспортировать данные из моего представления в Excel, и проблема в том, что они возвращают нулевое значение.Но я не знаю, почему он возвращает нулевое значение, даже если я использую тот же процесс из представления данных или представления индекса.

мой контроллер

using myNewWeb.Models;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ClosedXML;
using ClosedXML.Excel;
using System.IO;
using static DataLibrary.BusinessOperations.BookProcessors;

namespace myNewWeb.Controllers
{
    public class BookController : Controller
    {
     public ActionResult DownloadExcel()
        {
            var data = viewAllBooks();
            //List<BookModel> ExcelDLBooks = new List<BookModel>();
            XLWorkbook excelDL = new XLWorkbook();
            excelDL.Worksheets.Add(data);
            excelDL.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            excelDL.Style.Font.Bold = true;

            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "";
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment;filename= Datails.xlsx");

            MemoryStream myMemory = new MemoryStream();
            excelDL.SaveAs(myMemory);
            myMemory.WriteTo(Response.OutputStream);
            Response.Flush();
            Response.End();

            return View();
    }
 private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            }
            catch
            {
                obj = null;
            }
            finally
            {
                GC.Collect();
            }
        }
}

мои процессоры

using DataLibrary.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataLibrary.BusinessOperations
{
    public class BookProcessors
    {
        public static DataTable viewAllBooks()
        {
            try
            {
                Connection viewAll = new Connection();
                viewAll.cmdType = CommandType.StoredProcedure;
                viewAll.SQL = "insertSelectUpdateDeleteBooks";
                viewAll.ParamAndValue = new string[,]
                {
                {"@bookid", "" },
                {"@authorid", "" },
                {"@bookcatid", "" },
                {"@booktitle", ""},
                {"@isbn", ""},
                {"@pubplace", "" },
                {"@pubdate", "" },
                {"@bookphoto", "" },
                {"@statementtype", "Select" }
                };
                return viewAll.GetData();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}

GetData () находится в моем файле DataAccessLayer

My View или ixdex View

@model IEnumerable<myNewWeb.Models.BookModel>
@using (Html.BeginForm("DownloadExcel", "Book", FormMethod.Post))
{
    <table id="booksTable" style="margin-top:10px" class="table table-striped table-bordered table-responsive">
        <thead>
            <tr>
                <th>Image</th>
                <th>Book Tittle</th>
                <th>Book Author</th>
                <th>ISBN</th>
                <th>Category</th>
                <th>DDC</th>
                <th>Publish Place</th>
                <th>Publish Date</th>
                <th>Status</th>
                <th>Action</th>
            </tr>
        </thead>
        @foreach (var item in Model)
        {
            <tr>
                <td></td>
                <td>@Html.DisplayFor(Model => item.bookTitle)</td>
                <td>@Html.DisplayFor(Model => item.authorName)</td>
                <td>@Html.DisplayFor(Model => item.ISBN)</td>
                <td>@Html.DisplayFor(Model => item.CategoryName)</td>
                <td>@Html.DisplayFor(Model => item.DDC)</td>
                <td>@Html.DisplayFor(Model => item.pubPlace)</td>
                <td>@Html.DisplayFor(Model => item.pubDate)</td>
                <td></td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                    @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                    @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
                </td>
            </tr>
        }
    </table>
 <button type="submit">Excel</button>
}
...