5、VS2010+ASP.NET MVC4+EF4+JqueryEasyUI+Oracle该项目的开发——使用datagrid做报表
来点需要:我使用的数据库访问EF框架,但是,因为使用一个动态表来的统计报告中。单独是每天产生基于数据表,它是很难使用EF加盟前。所以我包装在两组数据库存取层的框内,一个是EF,一种是传统的ADO.NET。当设计使用动态表,我打电话跟ADO.NET进行数据库訪问操作了。
先看下View。这里我使用的是jquery easyUI里面的datagrid来做的报表。
例如以下:
单击选中日报:
年报例如以下:
@model YKT.Model.SubCompanyReport
@{
ViewBag.Title = "分公司营收统计";
Layout = "~/Views/Shared/Base.cshtml";
}
<style type="text/css">
.datagrid-header-row .datagrid-cell-group
{
font-size:12px;
font-weight:bold;
}
.datagrid-header td
{
font-weight:bold;
}
</style>
<script src="../../Res/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
<script type="text/javascript">
var cTime = "";
var cUser = '<div style="text-align: left; float:left;">制表人:</div>';
var dataDate = "";
var yearMonthTitle = "";
var dayMonthTitle = "";
var tbTitle=""; //获取组织类型
function getComponentType() {
$("#sltCompany").empty();
var url = "SubCompanyReport/GetComList"
$.getJSON(url, function (data) {
if (data != null) {
$.each(data, function (i, item) {
$("<option></option>")
.val(item["COMPONENT_ID"])
.text(item["COMPONENT_NAME"])
.appendTo($("#sltCompany"));
});
}
});
} //“查询”button。弹出查询框
function flexiQuery() {
if ($("#Start_Time").val() == "") {
$.messager.alert('提示', '结算日期不能为空');
}
else {
//将查询条件依照分隔符拼接成字符串
var search = "Start_Time=" + $("#Start_Time").val() + "&&End_Time=" + $("#End_Time").val() + "&&sltCompany="
+ $("#sltCompany").val() + "&&sltReportType=" + $("#sltReportType").val(); var url = 'SubCompanyReport/GetData?' + search;
cTime = '<div style="text-align: left; float:left;">制表时间:' + CurentDate() + '</div>'; var myDate = new Date();
var startDate = StringToDate($("#Start_Time").val());
dataDate = '<div style="text-align: left; float:left;">数据日期:';
if ($("#sltReportType").val() == "0") {
var endDate = StringToDate($("#End_Time").val());
if (endDate == "") {
dataDate += getDateByStr(startDate) + getWeek(startDate);
}
else {
dataDate += getDateByStr(startDate) + getWeek(startDate) + " 到 " + getDateByStr(endDate) + getWeek(endDate);
}
dataDate += '</div>';
$('#div1').panel('close');
$('#div').panel('open');
yearMonthTitle = "月累计";
dayMonthTitle = "当日";
//运行查询
getData(url);
}
else if ($("#sltReportType").val() == "1") {
$('#div1').panel('close');
$('#div').panel('open');
var v = getDateByStr(startDate);
dataDate += v.substring(0, 8);
dayMonthTitle = "当月";
yearMonthTitle = "年累计";
dataDate += '</div>';
//运行查询
getData(url);
}
else if ($("#sltReportType").val() == "2") {
var v1 = getDateByStr(startDate);
dataDate = v1.substring(0, 5);
tbTitle = '公交总公司' + dataDate + '运营收入统计表(按分公司)';
$('#div1').panel('open');
$('#div').panel('close');
dataDate += '</div>';
//运行查询
getData1(url);
}
}
}
$(function () {
initData();
$("#sltReportType").change(function () {
if ($("#sltReportType").val() == "0") {
$("#spnEndTime").css("display", "inline");
} else {
$("#spnEndTime").css("display", "none");
}
});
});
//初始化数据
function initData() {
var d = getDate(0);
$("#Start_Time").val(d);
//$("#End_Time").val(d);
getComponentType();
}
//导出
function outputExcel() {
$.messager.confirm('操作提示', "确认导出数据吗? ", function (r) {
if (r) {
//将查询条件依照分隔符拼接成字符串
var search = "Start_Time=" + $("#Start_Time").val() + "&&End_Time=" + $("#End_Time").val() + "&&sltCompany=" + $("#sltCompany").val()
+ "&&sltReportType=" + $("#sltReportType").val();
var url = 'SubCompanyReport/Export?' + search;
window.location.href = url;
}
});
}
//日报表
function getData(url) {
var searchH = $("#divQuery").height();
var h = document.documentElement.clientHeight - searchH - 26;
//var w = document.documentElement.clientWidth;
$('#flexigridData').datagrid({
title: '分公司营收统计', //列表的标题
iconCls: 'icon-site',
// fit: true, //列表自己主动适应宽度
width: 'auto',
singleSelect: true,
height: h,
nowrap: false, //True 就会把数据显示在一行里。
striped: true, //True 就把行条纹化。(即奇偶行使用不同背景色)
collapsible: true, //可调节列宽
//remoteSort: true, //定义是否从server给数据排序。
url: url, //获取数据的url toolbar: [],
columns: [
[{ title: '公交公司全日运营收入统计表(按分公司)', colspan: 17,align:'center'}], //第一行
[{ title: '<div style="text-align: left; float:left;">制表单位:吴江公交总公司IC卡结算管理中心</div>', colspan: 8, headalign: 'left' },
{ title: cTime, colspan: 7 }, { title: cUser, colspan: 2}], //第二行
[{ title: dataDate, colspan: 17}], //第三行
[{ title: '单位', rowspan: 3, field: 'ComponentName' }, { title: '投币收入', colspan: 4 }, { title: 'IC卡收入', colspan: 4 },
{ title: 'IC卡收入占%', colspan: 4 }, { title: '合计', colspan: 4}], //第四行
[{ title: dayMonthTitle, colspan: 2 }, { title: yearMonthTitle, colspan: 2 }, { title: dayMonthTitle, colspan: 2 },
{ title: yearMonthTitle, colspan: 2 }, { title: dayMonthTitle, colspan: 2 },
{ title: yearMonthTitle, colspan: 2 }, { title: dayMonthTitle, colspan: 2 }, { title: yearMonthTitle, colspan: 2}], //第五行
[{ title: '人次(次)', field: 'tbrec' }, { title: '金额(元)', field: 'tbamt', width: 80 }, { title: '人次(次)', field: 'tbljrec' },
{ title: '金额(元)', field: 'tbljamt', width: 80 },
{ title: '人次(次)', field: 'icrec' }, { title: '金额(元)', field: 'icamt', width: 80 }, { title: '人次(次)', field: 'icljrec' },
{ title: '金额(元)', field: 'icljamt', width: 80 },
{ title: '人次(次)', field: 'dayrecPer' }, { title: '金额(元)', field: 'dayAmtPer' }, { title: '人次(次)', field: 'monthrecPer' },
{ title: '金额(元)', field: 'monthAmtPer' },
{ title: '人次(次)', field: 'dayrecTotal' }, { title: '金额(元)', field: 'dayAmtTotal',width:80 },
{ title: '人次(次)', field: 'monthrecTotal' }, { title: '金额(元)', field: 'monthAmtTotal',width:80}], //第六行
] //,
//pagination: true, //在 datagrid 的底部显示分页栏。 //rownumbers: true //显示行号的列
});
//异步获取button
//首先获取iframe标签的id值
var iframeid = window.parent.$('#tabs').tabs('getSelected').find('iframe').attr("id"); //然后关闭AJAX对应的缓存
$.ajaxSetup({
cache: false
}); //获取button值
$.getJSON("../Home/GetToolbar", { id: iframeid }, function (data) {
if (data == null) {
return;
}
$('#flexigridData').datagrid("addToolbarItem", data); });
}
//年报表
function getData1(url) {
var searchH = $("#divQuery").height();
var h = document.documentElement.clientHeight - searchH - 26;
//var w = document.documentElement.clientWidth;
$('#flexigridData1').datagrid({
title: '分公司营收统计', //列表的标题
iconCls: 'icon-site',
// fit: true, //列表自己主动适应宽度
width: 'auto',
singleSelect: true,
height: h,
nowrap: false, //True 就会把数据显示在一行里。 striped: true, //True 就把行条纹化。 (即奇偶行使用不同背景色)
collapsible: true, //可调节列宽
//remoteSort: true, //定义是否从server给数据排序。 url: url, //获取数据的url toolbar: [],
columns: [
[{ title: tbTitle, colspan: 9, align: 'center'}], //第一行
[{ title: '<span style="text">制表单位:吴江公交总公司IC卡结算管理中心</span>', colspan: 4, headalign: 'left' }, { title: cTime, colspan: 3 },
{ title: cUser, colspan: 2}], //第二行
[{ title: '单位', rowspan: 2, field: 'ComponentName' }, { title: '投币收入', colspan: 2 }, { title: 'IC卡收入', colspan: 2 },
{ title: 'IC卡收入占%', colspan: 2 }, { title: '合计', colspan: 2}], //第三行
[{ title: '人次(次)', field: 'tbrec' }, { title: '金额(元)', field: 'tbamt', width: 120 }, { title: '人次(次)', field: 'icrec' },
{ title: '金额(元)', field: 'icamt', width: 120 },
{ title: '人次(次)', field: 'yearrecPer' }, { title: '金额(元)', field: 'yearAmtPer', width: 120 }, { title: '人次(次)', field: 'yearrecTotal' },
{ title: '金额(元)', field: 'yearAmtTotal', width: 120}]
] //,
//pagination: true, //在 datagrid 的底部显示分页栏。 //rownumbers: true //显示行号的列
});
//异步获取按钮
//首先获取iframe标签的id值
var iframeid = window.parent.$('#tabs').tabs('getSelected').find('iframe').attr("id"); //然后关闭AJAX对应的缓存
$.ajaxSetup({
cache: false
}); //获取按钮值
$.getJSON("../Home/GetToolbar", { id: iframeid }, function (data) {
if (data == null) {
return;
}
$('#flexigridData1').datagrid("addToolbarItem", data); });
}
</script>
<div id="content" region="center" fit="true">
<form id="form1">
<div id="divQuery" style="padding: 5px; font-size: 12px;" title="查询列表" class="easyui-toolbar">
<span>结算日期:</span><span>
<input type="text" readonly="readonly" id="Start_Time" onclick="WdatePicker({maxDate:'#F{$dp.$D(\'End_Time\');}'})" />
</span>
<span id="spnEndTime"> 到
<input type="text" readonly="readonly" id="End_Time" onclick="WdatePicker({minDate:'#F{$dp.$D(\'Start_Time\');}'})" /></span>
<span>公司类型:</span> <span><select id="sltCompany"></select></span>
<span style=" margin:4px;">报表类型:</span><span><select id="sltReportType">
<option value="0">日报</option>
<option value="1">月报</option>
<option value="2">年报</option>
</select></span>
<a href="javascript:flexiQuery()" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</a>
<a href="javascript:resetData()" class="easyui-linkbutton" data-options="iconCls:'icon-redo'"> 重置</a>
</div>
</form>
<div id="div" class="easyui-panel" closed="true">
<table id="flexigridData" style=" text-align:center">
</table> </div>
<div id="div1" class="easyui-panel" closed="true">
<table id="flexigridData1" style=" text-align:center">
</table>
</div>
</div>
控制器SubCompanyReportController例如以下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YKT.BLL;
using YKT.Common;
using YKT.Model;
using System.IO;
using Microsoft.Office.Interop.Excel;
using System.Text; namespace YKT.Controllers
{
public class CompanyClass
{
public string COMPONENT_ID { get; set; }
public string COMPONENT_NAME { get; set; }
} public class SubCompanyReportController : Controller
{
#region 初始化 string ClassName = "SubCompanyReport"; //类名
SubCompanyReportService _service;
public SubCompanyReportController()
{
if (_service == null)
{
_service = new SubCompanyReportService();
}
} ValidationErrors validationErrors = new ValidationErrors();
OMOPERATIONLOGTB OperLog = new OMOPERATIONLOGTB(); #endregion public ActionResult Index()
{
return View();
}
public JsonResult GetComList()
{
System.Data.DataTable data = _service.GetComList(1, -1, 0, "COMMON,BUS");
List<CompanyClass> list = new List<CompanyClass>();
if (data != null && data.Rows.Count > 0)
{
for (int i = 0; i < data.Rows.Count; i++)
{
list.Add(new CompanyClass { COMPONENT_ID = data.Rows[i][0].ToString(), COMPONENT_NAME = data.Rows[i][1].ToString() });
}
} return Json(list, JsonRequestBehavior.AllowGet);
} [HttpPost]
public JsonResult GetData(string sltReportType, string Start_Time, string End_Time, string sltCompany, string sltSubCompany)
{
List<SubCompanyReport> list = _service.GetTotalStatBySubCompany(sltReportType, Start_Time, End_Time); return Json(list, JsonRequestBehavior.AllowGet);
} #region 导出 /// <summary>
/// 数据导出
/// </summary>
/// <param name="search"></param>
/// <returns></returns>
public ActionResult Export(string sltReportType, string Start_Time, string End_Time, string sltCompany, string sltSubCompany)
{
List<SubCompanyReport> queryData = _service.GetTotalStatBySubCompany(sltReportType, Start_Time, End_Time);
if (queryData == null || queryData.Count < 1)
{
Response.Write("<script> alert('没有要导出的数据! '); </script>");
return View("Index");
} int sheet;
//结算起始时间 #region 导出Excel string newpath = AppDomain.CurrentDomain.BaseDirectory + "Excel\\";
string newFileName = newpath + "temp.xls";//+ Guid.NewGuid()
if (!Directory.Exists(newpath))
{
Directory.CreateDirectory(newpath);
}
//string newpath = Server.MapPath(".") + @"\Excel\" + Guid.NewGuid() + ".xls";
//调用的模板文件
FileInfo mode = new FileInfo(Server.MapPath("~/xlsMode/营收报表.xls"));
Application app = new Application();
if (app == null)
{
return Json("当前用户电脑未安装EXCEL!", JsonRequestBehavior.AllowGet);
}
app.Application.DisplayAlerts = false;
app.Visible = false; if (mode.Exists)
{
Workbook tworkbook;
Object missing = System.Reflection.Missing.Value; app.Workbooks.Add(missing); string beginTime = string.IsNullOrEmpty(Start_Time) ? string.Empty : DateTime.Parse(Start_Time).ToString("yyyyMMdd");
string endTime = string.IsNullOrEmpty(End_Time) ? string.Empty : DateTime.Parse(End_Time).ToString("yyyyMMdd"); //调用模板 写表头
tworkbook = app.Workbooks.Open(mode.FullName, missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing);
Worksheet tworksheet=new Worksheet();
string ctitle = "制表时间:";
string dataDateText = "数据日期:";
switch (sltReportType)
{
case "0": //日报
sheet = 8;
tworksheet = (Worksheet)tworkbook.Sheets[sheet];
if (!string.IsNullOrEmpty(beginTime) && string.IsNullOrEmpty(endTime))
{
dataDateText += ValueConvert.GetDatetimeString(beginTime);
}
else if (string.IsNullOrEmpty(beginTime) && !string.IsNullOrEmpty(endTime))
{
dataDateText += ValueConvert.GetDatetimeString(endTime);
}
else if (!string.IsNullOrEmpty(beginTime) && !string.IsNullOrEmpty(endTime))
{
dataDateText += ValueConvert.GetDatetimeString(beginTime) + " 到 " + ValueConvert.GetDatetimeString(endTime); ;
}
tworksheet.Cells[3, 9] = ctitle+ValueConvert.GetDatetimeString(DateTime.Now.ToString("yyyyMMdd"));
tworksheet.Cells[4, 1] = dataDateText;
break;
case "1": //月报
sheet = 9;
tworksheet = (Worksheet)tworkbook.Sheets[sheet];
tworksheet.Cells[1, 1] = "吴江市公交公司" + beginTime.Substring(4, 2) + "月份运营收入统计表(按分公司)";
tworksheet.Cells[3, 9] = ctitle + ValueConvert.GetDatetimeString(DateTime.Now.ToString("yyyyMMdd"));
break;
case "2": //年报
sheet = 10;
tworksheet = (Worksheet)tworkbook.Sheets[sheet];
tworksheet.Cells[1, 1] = "吴江市公交公司" + beginTime.Substring(0, 4) + "年运营收入统计表(按分公司)";
tworksheet.Cells[3, 5] = ctitle + ValueConvert.GetDatetimeString(DateTime.Now.ToString("yyyyMMdd"));
break;
} #region 写Excel的内容 if (queryData != null && queryData.Count > 0)
{
if (sltReportType == "0" || sltReportType == "1")
{
int m = 7;
for (int i = 0; i < queryData.Count; i++)
{
tworksheet.Cells[m + i, 1] = queryData[i].ComponentName; tworksheet.Cells[m + i, 2] = queryData[i].tbrec;
tworksheet.Cells[m + i, 3] = queryData[i].tbamt;
tworksheet.Cells[m + i, 4] = queryData[i].tbljrec;
tworksheet.Cells[m + i, 5] = queryData[i].tbljamt; tworksheet.Cells[m + i, 6] = queryData[i].icrec;
tworksheet.Cells[m + i, 7] = queryData[i].icamt;
tworksheet.Cells[m + i, 8] = queryData[i].icljrec;
tworksheet.Cells[m + i, 9] = queryData[i].icljamt; tworksheet.Cells[m + i, 10] = queryData[i].dayrecPer;
tworksheet.Cells[m + i, 11] = queryData[i].dayAmtPer;
tworksheet.Cells[m + i, 12] = queryData[i].monthrecPer;
tworksheet.Cells[m + i, 13] = queryData[i].monthAmtPer; tworksheet.Cells[m + i, 14] = queryData[i].dayrecTotal;
tworksheet.Cells[m + i, 15] = queryData[i].dayAmtTotal;
tworksheet.Cells[m + i, 16] = queryData[i].monthrecTotal;
tworksheet.Cells[m + i, 17] = queryData[i].monthAmtTotal;
}
}
else if (sltReportType == "2")
{
int m = 4;
for (int i = 0; i < queryData.Count; i++)
{
tworksheet.Cells[m + i, 1] = queryData[i].ComponentName; tworksheet.Cells[m + i, 2] = queryData[i].tbrec;
tworksheet.Cells[m + i, 3] = queryData[i].tbamt;
tworksheet.Cells[m + i, 4] = queryData[i].icrec;
tworksheet.Cells[m + i, 5] = queryData[i].icamt; tworksheet.Cells[m + i, 6] = queryData[i].yearrecPer;
tworksheet.Cells[m + i, 7] = queryData[i].yearAmtPer;
tworksheet.Cells[m + i, 8] = queryData[i].yearrecTotal;
tworksheet.Cells[m + i, 9] = queryData[i].yearAmtTotal;
}
} } #endregion tworksheet.SaveAs(newFileName, missing, missing, missing, missing, missing, missing, missing, missing, missing);
tworkbook.Close(false, mode.FullName, missing);
app.Workbooks.Close();
app.Quit();
tworkbook = null;
app = null;
//强制对全部代进行垃圾回收
GC.Collect();
} System.IO.FileInfo file = new System.IO.FileInfo(newFileName);
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.Default;
// 加入头信息,为"文件下载/另存为"对话框指定默认文件名称
string nm = "分公司_" + mode.Name;
Response.AddHeader("Content-Disposition", "attachment; filename=" +HttpUtility.UrlEncode(nm, Encoding.UTF8).ToString() );
//Server.UrlEncode(mode.Name));
// 加入头信息,指定文件大小。让浏览器可以显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一个不能被client读取的流。必须被下载
Response.ContentType = "application/ms-excel";
// 把文件流发送到client
Response.WriteFile(file.FullName);
// 停止页面的运行
Response.End();
return Json("导出成功!", JsonRequestBehavior.AllowGet); #endregion
} #endregion }
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
5、VS2010+ASP.NET MVC4+EF4+JqueryEasyUI+Oracle该项目的开发——使用datagrid做报表的更多相关文章
- 3、VS2010+ASP.NET MVC4+EF4+JqueryEasyUI+Oracle项目开发之——用户登录
近期因为项目赶着上线,一直没时间接着写博客,今天最终空出了时间.声名:我不是专业美工,所以界面问题,希望大家不要拍砖.登录界面例如以下: 在ASP.NET MVC中,要新增一个功能,我们首先要加入一个 ...
- ASP.NET MVC4中用 BundleCollection使用问题手记
ASP.NET MVC4中对JS和CSS的引用又做了一次变化,在MVC3中我们这样引用资源文件: <link href="@Url.Content("~/Content/Si ...
- ASP.NET MVC4中用 BundleCollection
来源:http://www.cnblogs.com/madyina/p/3702314.html ASP.NET MVC4中对JS和CSS的引用又做了一次变化,在MVC3中我们这样引用资源文件: &l ...
- ASP.NET MVC4中对JS和CSS的引用
https://www.cnblogs.com/madyina/p/3702314.html ASP.NET MVC4中对JS和CSS的引用又做了一次变化,在MVC3中我们这样引用资源文件: < ...
- Asp.Net MVC4 + Oracle + EasyUI 学习 第二章
Asp.Net MVC4 + Oracle + EasyUI 第二章 --使用Ajax提升网站性能 本文链接:http://www.cnblogs.com/likeli/p/4236723.html ...
- Asp.Net MVC4 + Oracle + EasyUI 学习 第一章
Asp.Net MVC4 + Oracle + EasyUI 第一章 --操作数据和验证 本文链接:http://www.cnblogs.com/likeli/p/4234238.html 文章集合 ...
- Asp.Net MVC4 + Oracle + EasyUI 学习 序章
Asp.Net MVC4 + Oracle + EasyUI 序章 -- 新建微软实例 本文链接:http://www.cnblogs.com/likeli/p/4233387.html 1. 简 ...
- Asp.Net MVC4 + Oracle + EasyUI + Bootstrap
Asp.Net MVC4 + Oracle + EasyUI + Bootstrap --操作数据和验证 本文链接:http://www.cnblogs.com/likeli/p/4234238.ht ...
- Asp.Net MVC4 + Oracle + EasyUI + Bootstrap 1
Asp.Net MVC4 + Oracle + EasyUI + Bootstrap 序章 Asp.Net MVC4 + Oracle + EasyUI + Bootstrap 序章 -- 新建微软实 ...
随机推荐
- Android进程间通信(IPC)机制Binder简介和学习计划
在Android系统,每个应用程序是由多个Activity和Service部件,这些Activity和Service有可能在相同的处理被执行,此外,还可以在不同的过程中进行. 然后.不是在同一个过程A ...
- cocos2d-x3.0数据结构
1.cocos2d::Vector 1.头报价"CCVector.h"头文件. 2.保存的数据类型必须是cocos2d::Ref的子类. 3.实现是动态加入数据集合即链表.主要的使 ...
- 关闭 sqlserver提示信息
- linux 下上传 datapoint数据到yeelink 修改版本
/*client.c*/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include ...
- BAE3.0搭建wordpress注意
仅仅是mark一个注意的点,数据库连接时,主机是: /** MySQL主机 */ define('DB_HOST', 'sqld.duapp.com:4050');
- 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context}
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to'org.eclipse ...
- [BEROR]CodeSign error: code signing is required for product type 'Application' in SDK 'iOS 8.1'
解决方法: 选择project->Build Settings -> Code Signing -> Code Signing Identity -> Debug -> ...
- ListView的cacheColorHint与listSelector物业和fragment里面onActivityResult问题没有响应
fragment里面onActivityResult 如何才华被称之为.在使用时发现没反应,没注意这个问题谁. 多方法都不是非常方便.最终实验除了一个最简单的方法. startActivityForR ...
- CopyOnWriteArrayList源代码阅读器
java.util.concurrent在相应的并发集合的包中定义的通用集合类,为了有效地处理并发场景.间CopyOnWriteArrayList它是合适ArrayList.顾名思义CopyOnWri ...
- 左右Map
Person p=new Person("黄雄"); Map map=new HashMap(); map.put("p", p); p.setName(&qu ...