C# 将特定数据导出到Excel
我想用LINQ MVC 5将数据导出到Excel。目前,它可以工作并导出所有数据,但我只想导出特定的数据,这是我通过筛选(索引控制器)选择的。以下是我所拥有的: 型号:C# 将特定数据导出到Excel,c#,asp.net-mvc,linq,export-to-excel,C#,Asp.net Mvc,Linq,Export To Excel,我想用LINQ MVC 5将数据导出到Excel。目前,它可以工作并导出所有数据,但我只想导出特定的数据,这是我通过筛选(索引控制器)选择的。以下是我所拥有的: 型号: public class Oplata { public int ID { get; set; } [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode =
public class Oplata
{
public int ID { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name="Data")]
public DateTime DataOplaty { get; set; }
[DisplayFormat(DataFormatString = "{0} km")]
[Display(Name = "Przebieg")]
public int PrzebiegOplaty { get; set; }
[Display(Name = "Rodzaj")]
public RodzajOplaty RodzajOplaty { get; set; }
[DisplayFormat(DataFormatString = "{0:c}")]
[Display(Name = "Kwota")]
public int KwotaOplaty { get; set; }
[DataType(DataType.MultilineText)]
[Display(Name = "Szczegóły")]
public string DodatkoweInformacjeOplaty { get; set; }
[Display(Name = "Numer rejestracyjny")]
public string TablicaRejstracyjna { get; set; }
public int PojazdID { get; set; }
public virtual Pojazd Pojazd { get; set; }
}
public enum RodzajOplaty
{
Naprawa, Przegląd, Ubezpieczenie, Olej, Inne
}
控制器:
public ActionResult Index(string oplataTyp, string szukajRej, DateTime? fromDate, DateTime? toDate)
{
var oplaty = from p in db.Oplaty select p;
// Daty
var MinDate = (from d in db.Oplaty select d.DataOplaty).Min();
var MaxDate = (from d in db.Oplaty select d.DataOplaty).Max();
if (!fromDate.HasValue) fromDate = MinDate;
if (!toDate.HasValue) toDate = DateTime.Today.AddDays(1);
if (toDate < fromDate) toDate = fromDate.GetValueOrDefault(DateTime.Now.Date).Date.AddDays(1);
ViewBag.fromDate = fromDate;
ViewBag.toDate = toDate;
oplaty = db.Oplaty.Where(c => c.DataOplaty >= fromDate && c.DataOplaty < toDate);
// Filtrowanie danych
var TypLista = new List<RodzajOplaty>();
var TypWyszukaj = from d in db.Oplaty orderby d.RodzajOplaty select d.RodzajOplaty;
TypLista.AddRange(TypWyszukaj.Distinct());
ViewBag.oplataTyp = new SelectList(TypLista);
RodzajOplaty RodzajOplaty;
if (Enum.TryParse<RodzajOplaty>(oplataTyp, out RodzajOplaty))
{
oplaty = oplaty.Where(x => x.RodzajOplaty == RodzajOplaty);
}
if (!String.IsNullOrEmpty(szukajRej))
{
oplaty = oplaty.Where(s => s.TablicaRejstracyjna.Contains(szukajRej));
}
// Zliczanie opłat
try
{
ViewBag.SumowanieOplat = oplaty.Sum(x => x.KwotaOplaty);
}
catch
{
}
return View(oplaty.ToList());
}
public ActionResult ExportData()
{
GridView gv = new GridView();
gv.DataSource = db.Oplaty.ToList();
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=Raport.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
return RedirectToAction("Index");
}
public ActionResult索引(字符串oplataTyp、字符串szukajRej、DateTime?fromDate、DateTime?toDate)
{
var oplaty=从数据库中的p开始。oplaty选择p;
//达蒂
var MinDate=(从db.Oplaty中的d选择d.DataOplaty.Min();
var MaxDate=(从db.Oplaty中的d选择d.DataOplaty.Max();
如果(!fromDate.HasValue)fromDate=MinDate;
如果(!toDate.HasValue)toDate=DateTime.Today.AddDays(1);
如果(toDatec.DataOplaty>=fromDate&&c.DataOplatyx.RodzajOplaty==RodzajOplaty);
}
如果(!String.IsNullOrEmpty(szukajRej))
{
oplaty=oplaty.Where(s=>s.TablicaRejstracyjna.Contains(szukajRej));
}
//Zliczanie opłat
尝试
{
ViewBag.SumowanieOplat=oplaty.Sum(x=>x.kwotoplaty);
}
抓住
{
}
返回视图(oplaty.ToList());
}
公共操作结果导出数据()
{
GridView gv=新的GridView();
gv.DataSource=db.Oplaty.ToList();
gv.DataBind();
Response.ClearContent();
Response.Buffer=true;
AddHeader(“内容处置”、“附件;文件名=Raport.xls”);
Response.ContentType=“应用程序/ms excel”;
响应。Charset=“”;
StringWriter sw=新的StringWriter();
HtmlTextWriter htw=新的HtmlTextWriter(sw);
gv.渲染控制(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
返回操作(“索引”);
}
视图:
@model IEnumerable
泽斯塔维尼酒店
@Scripts.Render(“~/bundles/jqueryui”)
$(文档).ready(函数(){
$(“.DatePicker”).DatePicker({
日期格式:“yyyy-MM-dd”,
变化月:对,
变化年:是的,
是的,
});
});
@ActionLink(“Utwórz”,“Create”)
@{
变量fromDate=(DateTime)ViewBag.fromDate;
var toDate=(DateTime)ViewBag.toDate;
}
@使用(Html.BeginForm(“Index”,“Oplatas”,FormMethod.Get))
{
Rodzaj oplaty:@Html.DropDownList(“oplataTyp”、“Wszystkie”)
Numer rejestracyjny:@Html.TextBox(“szukajRej”)
Od:@Html.TextBox(“fromDate”,string.Format(“{0:yyyy-MM-dd}”,fromDate),new{@class=“DatePicker”})
Do:@Html.TextBox(“toDate”,string.Format(“{0:yyyy-MM-dd}”,toDate),new{@class=“DatePicker”})
}
@DisplayNameFor(model=>model.TablicaRejstracyjna)
@DisplayNameFor(model=>model.Pojazd.Marka)
@DisplayNameFor(model=>model.DataOplaty)
@DisplayNameFor(model=>model.PrzebiegOplaty)
@DisplayNameFor(model=>model.RodzajOplaty)
@DisplayNameFor(model=>model.kwotoplaty)
@DisplayNameFor(model=>model.DodatkoweInformacjeOplaty)
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.TablicaRejstracyjna)
@DisplayFor(modeleItem=>item.Pojazd.Marka)
@DisplayFor(modelItem=>item.DataOplaty)
@DisplayFor(modelItem=>item.PrzebiegOplaty)
@DisplayFor(modeleItem=>item.RodzajOplaty)
@DisplayFor(modelItem=>item.KwotaOplaty)
@DisplayFor(modelItem=>item.DodatKoweiInformacGeoplaty)
@ActionLink(“Edytuj”,“Edit”,new{id=item.id})|
@ActionLink(“Szczegóły”,“Details”,new{id=item.id})|
@ActionLink(“Usuń”,“Delete”,new{id=item.id})
}
@if(ViewBag.SumowanieOplat!=null)
{
Suma kosztów:
@ViewBag.SumowanieOplat zł
}
Kliknij poniżej,aby Wyrogenewaćkompletny raport:
@使用(Html.BeginForm(“ExportData”、“Oplatas”、FormMethod.Post、new{enctype=“multipart/formdata”}))
{
}
我对使用GridView非常生疏,但我相信您的ExportData函数,您可以设置列可见性,例如gv.Columns(4)。Visible=False
希望这有帮助 你建议用什么来代替网格视图?
@model IEnumerable<AutoMonit.Models.Oplata>
<h2>Zestawienie opłat pojazdów</h2>
@Scripts.Render("~/bundles/jqueryui")
<script type="text/javascript">
$(document).ready(function () {
$(".DatePicker").datepicker({
dateFormat: 'yyyy MM dd',
changeMonth: true,
changeYear: true,
changeDay: true,
});
});
</script>
<p>
@Html.ActionLink("Utwórz", "Create")
</p>
@{
var fromDate = (DateTime)ViewBag.fromDate;
var toDate = (DateTime)ViewBag.toDate;
}
@using (Html.BeginForm("Index", "Oplatas", FormMethod.Get))
{
<p>
Rodzaj oplaty: @Html.DropDownList("oplataTyp", "Wszystkie")<br />
Numer rejestracyjny: @Html.TextBox("szukajRej") <br />
Od: @Html.TextBox("fromDate", string.Format("{0:yyyy-MM-dd}", fromDate), new { @class = "DatePicker" })
Do: @Html.TextBox("toDate", string.Format("{0:yyyy-MM-dd}", toDate), new { @class = "DatePicker" })
<br />
<input type="submit" value="Szukaj" />
</p>
}
<br />
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.TablicaRejstracyjna)
</th>
<th>
@Html.DisplayNameFor(model => model.Pojazd.Marka)
</th>
<th>
@Html.DisplayNameFor(model => model.DataOplaty)
</th>
<th>
@Html.DisplayNameFor(model => model.PrzebiegOplaty)
</th>
<th>
@Html.DisplayNameFor(model => model.RodzajOplaty)
</th>
<th>
@Html.DisplayNameFor(model => model.KwotaOplaty)
</th>
<th>
@Html.DisplayNameFor(model => model.DodatkoweInformacjeOplaty)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.TablicaRejstracyjna)
</td>
<td>
@Html.DisplayFor(modelItem => item.Pojazd.Marka)
</td>
<td>
@Html.DisplayFor(modelItem => item.DataOplaty)
</td>
<td>
@Html.DisplayFor(modelItem => item.PrzebiegOplaty)
</td>
<td>
@Html.DisplayFor(modelItem => item.RodzajOplaty)
</td>
<td>
@Html.DisplayFor(modelItem => item.KwotaOplaty)
</td>
<td>
@Html.DisplayFor(modelItem => item.DodatkoweInformacjeOplaty)
</td>
<td>
@Html.ActionLink("Edytuj", "Edit", new { id = item.ID }) |
@Html.ActionLink("Szczegóły", "Details", new { id = item.ID }) |
@Html.ActionLink("Usuń", "Delete", new { id = item.ID })
</td>
</tr>
}
</table>
@if (ViewBag.SumowanieOplat != null)
{
<hr />
<h2>
Suma kosztów:
@ViewBag.SumowanieOplat zł
</h2>
<hr />
}
Kliknij poniżej, aby wygenerować kompletny raport:
@using (Html.BeginForm("ExportData", "Oplatas", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table>
<tr><td></td><td><input type="submit" name="Export" id="Export" value="Generuj" /></td></tr>
</table>
}