You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

855 lines
33 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using DSWeb.Areas.TruckMng.DAL.TMSRPT;
using DSWeb.Areas.TruckMng.Models;
using DSWeb.Areas.TruckMng.Models.TMSRPT;
using DSWeb.Areas.TruckMng.Models.Card;
using DSWeb.TruckMng.Comm.Cookie;
using DSWeb.TruckMng.Helper;
using DSWeb.TruckMng.Helper.Repository;
using HcUtility.Comm;
using HcUtility.Core;
using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DSWeb.Areas.CommMng.Models;
using DSWeb.Areas.CommMng.DAL;
using DSWeb.Areas.TruckMng.DAL.MsWlInsure;
using System.IO;
using System.Data.OleDb;
namespace DSWeb.Areas.TruckMng.Controllers
{
[JsonRequestBehavior]
public class TMSRPTController : Controller
{
//
// GET: /TruckMng/MsWlTruck
public ActionResult MONTH_Index()
{
return View();
}
//
// GET: /TruckMng/MsWlTruck/Edit
public ActionResult MONTH_Edit()
{
return View();
}
public ActionResult MONTH_View()
{
return View();
}
//
// GET/TruckMng/MsWlTruck/GetDataList
public ContentResult MONTH_GetDataList(int start, int limit, string sort, string condition)
{
var dataList = TMSRPTDAL.GetDataList(condition, Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), CookieConfig.GetCookie_OrgCode(Request), sort);
var list = dataList.Skip(start).Take(limit);
var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = list.ToList() });
return new ContentResult() { Content = json };
}
//
// GET/TruckMng/MsWlTruck/GetData/
public ContentResult MONTH_GetData ( string handle, string condition )
{
TMSRPT_MONTH head = null;
if (handle == "edit")
{
var list = TMSRPTDAL.GetDataList(condition, Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), CookieConfig.GetCookie_OrgCode(Request));
if (list.Count > 0)
head = list[0];
}
if (head == null)
{
head = new TMSRPT_MONTH();
head.GID = System.Guid.NewGuid().ToString();
head.OP = Session["SHOWNAME"].ToString();
head.COMPANYID = Session["COMPANYID"].ToString();
head.ACCDATE = TMSRPTDAL.GetACCDATE(Session["COMPANYID"].ToString());
head.OPDATE = System.DateTime.Now.ToString("yyyy-MM-dd");
head.FUELPRICE = "0";
}
var json = JsonConvert.Serialize(
new { Success = true, Message = "查询成功", data = head });
return new ContentResult() { Content = json };
}
public ContentResult Save ( string opstatus, string data, string body, string delbody )
{
TMSRPT_MONTH head = JsonConvert.Deserialize<TMSRPT_MONTH>(data);
var bodyList = JsonConvert.Deserialize<List<TMSRPT_MONTH_TRUCKDETAIL>>(body);
var bodyDelList = JsonConvert.Deserialize<List<TMSRPT_MONTH_TRUCKDETAIL>>(delbody);
if (opstatus == "add")
{
head.DbOperationType = DbOperationType.DbotIns;
head.ModelUIStatus = "I";
//head.OP = Convert.ToString(Session["SHOWNAME"]);
//head.OPDATE = System.DateTime.Now.ToString("G");
//head.OrgCode = CookieConfig.GetCookie_OrgCode(Request);
}
else if (opstatus == "edit")
{
head.DbOperationType = DbOperationType.DbotUpd;
head.ModelUIStatus = "E";
head.OP = Convert.ToString(Session["SHOWNAME"]);
head.OPDATE = System.DateTime.Now.ToString("G");
}
else
{
head.DbOperationType = DbOperationType.DbotDel;
}
var modb = new ModelObjectRepository();
DBResult result = modb.Save(head,
ModelObjectConvert<TMSRPT_MONTH_TRUCKDETAIL>.ToModelObjectList(bodyList),
ModelObjectConvert<TMSRPT_MONTH_TRUCKDETAIL>.ToModelObjectList(bodyDelList)
);
var jsonRespose = new JsonResponse
{
Success = result.Success,
Message = result.Message,
Data = TMSRPTDAL.GetData("GID='" + head.GID + "'", Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), CookieConfig.GetCookie_OrgCode(Request))
};
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
public ContentResult Delete(string data)
{
var head = JsonConvert.Deserialize<TMSRPT_MONTH>(data);
var modb = new ModelObjectDB();
DBResult result = modb.Delete(head, Session["USERID"].ToString(),true
, "delete from TMSRPT_MONTH_TRUCKDETAIL where GID='"+head.GID+"'"
);
var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
public ContentResult GetBodyList(int start, int limit, string condition)
{
var dataList = TMSRPTDAL.GetBodyList(condition);
var list = dataList.Skip(start).Take(limit);
var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = list.ToList() });
return new ContentResult() { Content = json };
}
public ContentResult GetCompanyDetailList ( int start, int limit, string condition )
{
var dataList = TMSRPTDAL.GetCompanyDetailList(condition);
var list = dataList.Skip(start).Take(limit);
var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = list.ToList() });
return new ContentResult() { Content = json };
}
/*
#region 参照部分
public ContentResult GetDrvCodeList()
{
var list = MsWlTruckDAL.GetDrvCodeList(Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), CookieConfig.GetCookie_OrgCode(Request));
var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", data = list.ToList() });
return new ContentResult() { Content = json };
}
#endregion
//
public ContentResult GetTruckMonthList ( string condition )
{
var dataList = MsWlTruckDAL.GetTruckMonthList(condition);
//var list = dataList.Skip(start).Take(limit);
var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = dataList.ToList() });
return new ContentResult() { Content = json };
}
*/
public ContentResult ReMake(string data)
{
TMSRPT_MONTH head = JsonConvert.Deserialize<TMSRPT_MONTH>(data);
var result = new DBResult();
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
var strSql = new StringBuilder();
strSql.Append(" delete from TMSRPT_MONTH_TRUCKDETAIL where LINKGID='" + head.GID + "' ");
strSql.Append(" insert into TMSRPT_MONTH_TRUCKDETAIL(GID,LINKGID,COMPANYID,TRUCKNO,TEU,RATEDMIL,OVERLOADMIL,TONS,REALFUEL,RATEDFUEL,REMAINFUEL,AMOUNT,LQF,OTHERAMOUNT ");
strSql.Append(" ,ZJF,BXF,FUELPRICE,FUELAMOUNT,REPAIRDAYS,REPAIRAMOUNT,TYREAMOUNT,TAX,TRUCKCOST,TRUCKPROFIT_M,CARDAYS,WORKDAYS,USABLEDAYS,USABLERATE,MILPERDAY,PcBillType,FUELAMOUNTRATE) ");
strSql.Append(" exec [proc_TMSRPT_MONTH] '" + head.GID + "','" + head.COMPANYID + "'," + head.FUELPRICE + ",'" + head.ACCDATE + "','','" + head.EXPDATEBGN + "','" + head.EXPDATEEND + "' ");
var cmdupdate = db.GetSqlStringCommand(strSql.ToString());
cmdupdate.Parameters.Clear();
db.ExecuteNonQuery(cmdupdate, tran);
result = new DBResult();
result.Success = true;
result.Message = "审核成功";
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "审核错误,请重试或联系系统管理员";
}
}
if (result.Success == true) {
MakeBX(data);
}
var jsonRespose = new JsonResponse
{
Success = result.Success,
Message = result.Message,
Data = null
};
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
public ContentResult LoadBeforeFee(string data)
{
//从上个月份获取数据
TMSRPT_MONTH head = JsonConvert.Deserialize<TMSRPT_MONTH>(data);
var result = new DBResult();
var beforeACCDATE = TMSRPTDAL.GetBeforeACCDATE(head);
if (beforeACCDATE != "")
{
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
var strSql = new StringBuilder();
strSql.Append(" update TMSRPT_MONTH_TRUCKDETAIL set ");
strSql.Append(" zjf=(select top 1 isnull(ZJF,0) from TMSRPT_MONTH_TRUCKDETAIL T where T.TRUCKNO=TMSRPT_MONTH_TRUCKDETAIL.TRUCKNO and T.LINKGID=(select top 1 gid from TMSRPT_MONTH where ACCDATE='" + beforeACCDATE + "' and COMPANYID='" + head.COMPANYID + "' and isnull(isdelete,0)=0)) ");
// strSql.Append(" ,SBF=(select top 1 isnull(SBF,0) from TMSRPT_MONTH_TRUCKDETAIL T where T.TRUCKNO=TMSRPT_MONTH_TRUCKDETAIL.TRUCKNO and T.LINKGID=(select top 1 gid from TMSRPT_MONTH where ACCDATE='" + beforeACCDATE + "' and COMPANYID='" + head.COMPANYID + "' and isnull(isdelete,0)=0)) ");
// strSql.Append(" ,GJJ=(select top 1 isnull(GJJ,0) from TMSRPT_MONTH_TRUCKDETAIL T where T.TRUCKNO=TMSRPT_MONTH_TRUCKDETAIL.TRUCKNO and T.LINKGID=(select top 1 gid from TMSRPT_MONTH where ACCDATE='" + beforeACCDATE + "' and COMPANYID='" + head.COMPANYID + "' and isnull(isdelete,0)=0)) ");
strSql.Append(" where LINKGID='"+head.GID+"' ");
var cmdupdate = db.GetSqlStringCommand(strSql.ToString());
cmdupdate.Parameters.Clear();
db.ExecuteNonQuery(cmdupdate, tran);
result = new DBResult();
result.Success = true;
result.Message = "审核成功";
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "审核错误,请重试或联系系统管理员";
}
}
}
var jsonRespose = new JsonResponse
{
Success = false,
Message = result.Message,
Data = null
};
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
public ContentResult MakeBX ( string data ) //生成保险费
{
TMSRPT_MONTH head = JsonConvert.Deserialize<TMSRPT_MONTH>(data);
var result = new DBResult();
result.Success = true;
result.Message = "没有保险数据";
var DetailList = TMSRPTDAL.GetBodyList("LINKGID='"+head.GID+"'");
var UPDList = TMSRPTDAL.GetBodyList("1=2");
foreach (var _D in DetailList) {
var _FEE=0M;
var BXList=MsWlInsureDAL.GetAllDataList(" TRUCKNO='" + _D.TRUCKNO
+ "' and (InsureEndDate between '" + head.EXPDATEBGN.Substring(0, 10) + "' and '" + head.EXPDATEEND.Substring(0, 10) + "'"
+ " or InsureBgnDate between '" + head.EXPDATEBGN.Substring(0, 10) + "' and '" + head.EXPDATEEND.Substring(0, 10) + "' "
+ " or '" + head.EXPDATEBGN.Substring(0, 10) + "' between InsureBgnDate and InsureEndDate "
+ " or '" + head.EXPDATEEND.Substring(0, 10) + "' between InsureBgnDate and InsureEndDate ) and isnull((select sum(isnull(insuretotal,0)) from tMsWlInsureBody b where b.BillNo=tMsWlInsurehead.BillNo),0)>0 ");
var truckbgn=Convert.ToDateTime(head.EXPDATEBGN.Substring(0, 10));
var truckend=Convert.ToDateTime(head.EXPDATEEND.Substring(0, 10));
if (BXList.Count == 0) {
continue;
}
foreach (var _B in BXList) {
var insurebgn=Convert.ToDateTime(_B.InsureBgnDate);
var insureend=Convert.ToDateTime(_B.InsureEndDate);
//保险大于月内 以车时间为准
if (insurebgn <= truckbgn && insureend >= truckend)
{
TimeSpan _insuredays = insureend.Subtract(insurebgn);
var insuredays = Convert.ToDecimal(_insuredays.Days.ToString()) + 1;
var _dayFee = Convert.ToDecimal(_B.InsureTotal) / insuredays;
TimeSpan _Truckdays = truckend.Subtract(truckbgn);
var Truckdays = Convert.ToDecimal(_Truckdays.Days.ToString()) + 1;
_FEE = _FEE + Truckdays * _dayFee;
}
else
if (insurebgn <= truckbgn && insureend > truckbgn && insureend < truckend)
//前交叉 车开始日期 在保险起止日期之间
{
TimeSpan _insuredays = insureend.Subtract(insurebgn);
var insuredays = Convert.ToDecimal(_insuredays.Days.ToString()) + 1;
var _dayFee = Convert.ToDecimal(_B.InsureTotal) / insuredays;
TimeSpan _Truckdays = insureend.Subtract(truckbgn);
var Truckdays = Convert.ToDecimal(_Truckdays.Days.ToString()) + 1;
_FEE = _FEE + Truckdays * _dayFee;
}
else
if (insurebgn > truckbgn && insurebgn <= truckend && insureend > truckend)
//后交叉 车结束日期 在保险起止日期之间
{
TimeSpan _insuredays = insureend.Subtract(insurebgn);
var insuredays = Convert.ToDecimal(_insuredays.Days.ToString()) + 1;
var _dayFee = Convert.ToDecimal(_B.InsureTotal) / insuredays;
TimeSpan _Truckdays = truckend.Subtract(insurebgn);
var Truckdays = Convert.ToDecimal(_Truckdays.Days.ToString()) + 1;
_FEE = _FEE + Truckdays * _dayFee;
}
else
if (insurebgn >= truckbgn && insureend <= truckend )
//保险包含于车日之内
{
TimeSpan _insuredays = insureend.Subtract(insurebgn);
var insuredays = Convert.ToDecimal(_insuredays.Days.ToString()) + 1;
var _dayFee = Convert.ToDecimal(_B.InsureTotal) / insuredays;
TimeSpan _Truckdays = insureend.Subtract(insurebgn);
var Truckdays = Convert.ToDecimal(_Truckdays.Days.ToString()) + 1;
_FEE = _FEE + Truckdays * _dayFee;
}
}
if (_FEE > 0)
{
//费用后台计算
_D.BXF = _FEE.ToString();
//保险费加入成本
_D.TRUCKCOST = Convert.ToString(Convert.ToDecimal(_D.TRUCKCOST) + _FEE);
//计算:燃油费用占收入比
if (Convert.ToDecimal(_D.AMOUNT)!=0){
_D.FUELAMOUNTRATE = Convert.ToString(Convert.ToDecimal(_D.FUELAMOUNT) / Convert.ToDecimal(_D.AMOUNT));
}
UPDList.Add(_D);
}
}
if (UPDList.Count > 0)
{
var modb = new ModelObjectRepository();
result = modb.SaveComm(
ModelObjectConvert<TMSRPT_MONTH_TRUCKDETAIL>.ToModelObjectList(DetailList)
);
}
var jsonRespose = new JsonResponse
{
Success = result.Success,
Message = result.Message,
Data = null
};
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
#region 审核 是否允许查看
public ContentResult StartAudit ( String USERID, String bill )
{
var result = new DBResult();
var WorkFlowName = "TMSRPT_MONTHAudit";
var billList = bill.Split(',');
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
foreach (var billno in billList)
{
Resultmb WorkResult = WorkFlowDAL.WorkFlowStart(WorkFlowName, billno, USERID,"",billno,"");
if (WorkResult.Success == true)
{
var cmdupdate = db.GetSqlStringCommand("update TMSRPT_MONTH set BLSTATUS=1 where GID=@GID and BLSTATUS in(0,4) ");
cmdupdate.Parameters.Clear();
//db.AddInParameter(cmdupdate, "@AUDITDATE", DbType.String, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
db.AddInParameter(cmdupdate, "@GID", DbType.String, billno);
db.ExecuteNonQuery(cmdupdate, tran);
}
else
{
result.Success = false;
result.Message = "审核错误!";
}
}
result = new DBResult();
result.Success = true;
result.Message = "审核成功";
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "审核错误,请重试或联系系统管理员";
//return result;
}
}
var jsonRespose = new JsonResponse
{
Success = result.Success,
Message = result.Message,
Data = null
};
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
public ContentResult ResetAudit ( String USERID, String bill )
{
var result = new DBResult();
var WorkFlowName = "TMSRPT_MONTHAudit";
var billList = bill.Split(',');
var _count = 0;
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
foreach (var billno in billList)
{
Resultmb WorkResult = WorkFlowDAL.WorkFlowReset(WorkFlowName, billno, USERID);
if (WorkResult.Success == true)
{
var cmdupdate = db.GetSqlStringCommand("update CRM_QUOTATION set BLSTATUS=1 where GID=@GID and BLSTATUS in()");
cmdupdate.Parameters.Clear();
//db.AddInParameter(cmdupdate, "@AUDITDATE", DbType.String, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
db.AddInParameter(cmdupdate, "@GID", DbType.String, billno);
_count = db.ExecuteNonQuery(cmdupdate, tran);
}
else
{
result.Success = false;
result.Message = "审核错误!";
}
}
result = new DBResult();
if (_count > 0)
{
result.Success = true;
result.Message = "撤回成功";
}
else {
result.Success = false;
result.Message = "撤回失败";
}
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "撤回失败,请重试或联系系统管理员";
//return result;
}
}
var jsonRespose = new JsonResponse
{
Success = result.Success,
Message = result.Message,
Data = null
};
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
public ContentResult Audit ( String USERID, String bill )
{
var result = new DBResult();
var WorkFlowName = "TMSRPT_MONTHAudit";
var billList = bill.Split(',');
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
foreach (var billno in billList)
{
Resultmb WorkResult = WorkFlowDAL.InsertWorkFlowDo(WorkFlowName, billno, USERID,billno);
if (WorkResult.Success == true)
{
var cmdupdate = db.GetSqlStringCommand("update TMSRPT_MONTH set BLSTATUS=2 where GID=@GID and BLSTATUS=1 ");
cmdupdate.Parameters.Clear();
//db.AddInParameter(cmdupdate, "@AUDITDATE", DbType.String, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
db.AddInParameter(cmdupdate, "@GID", DbType.String, billno);
db.ExecuteNonQuery(cmdupdate, tran);
}
else
{
result.Success = false;
result.Message = "审核错误!";
}
}
result = new DBResult();
result.Success = true;
result.Message = "审核成功";
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "审核错误,请重试或联系系统管理员";
}
}
var jsonRespose = new JsonResponse
{
Success = result.Success,
Message = result.Message,
Data = null
};
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
public ContentResult AuditBack ( String USERID, String bill )
{
var result = new DBResult();
var WorkFlowName = "TMSRPT_MONTHAudit";
var billList = bill.Split(',');
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
foreach (var billno in billList)
{
Resultmb WorkResult = WorkFlowDAL.DeleteWorkFlowDo(WorkFlowName, billno, USERID, "");
if (WorkResult.Success == true)
{
var cmdupdate = db.GetSqlStringCommand("update TMSRPT_MONTH set BLSTATUS=4 where GID=@GID and BLSTATUS in(1,2) ");
cmdupdate.Parameters.Clear();
//db.AddInParameter(cmdupdate, "@AUDITDATE", DbType.String, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
db.AddInParameter(cmdupdate, "@GID", DbType.String, billno);
db.ExecuteNonQuery(cmdupdate, tran);
}
else
{
result.Success = false;
result.Message = "审核错误!";
}
}
result = new DBResult();
result.Success = true;
result.Message = "审核成功";
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "审核错误,请重试或联系系统管理员";
}
}
var jsonRespose = new JsonResponse
{
Success = result.Success,
Message = result.Message,
Data = null
};
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
#endregion
[HttpPost]
public ContentResult File ( )
{
var jsonRespose = new JsonResponse { Success = false, Message = "" };
if (Request.Files.Count != 1)
{
jsonRespose.Success = false;
jsonRespose.Message = "请选择上传的文件";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
var file = Request.Files["file"];
var TMSRPT_GID = Request.Form["GID"].ToString().Trim();
//var AIRLINES = Request.Form["AIRLINES"].ToString().Trim();
if (file == null)
{
jsonRespose.Success = false;
jsonRespose.Message = "上传文件发生未知错误,请重新上传";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
var path = Server.MapPath("../../UploadFiles/CtnTkDetail");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
var size = file.ContentLength;
var name = Path.GetFileName(file.FileName);
//var bsno = Request.Form["bsno"];
var usercode = CookieConfig.GetCookie_UserCode(Request);
string filename = path + "\\" + usercode + DateTime.Now.ToString("yyyyMMddHHmmssfff") + name;
if (System.IO.File.Exists(filename))
{
System.IO.File.Delete(filename);
}
file.SaveAs(filename);
if (!System.IO.File.Exists(filename))
{
jsonRespose.Success = false;
jsonRespose.Message = "上传的Excel不包含数据01";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
List<string> sheets = ExcelSheetName(filename);
if (sheets.Count == 0)
{
jsonRespose.Success = false;
jsonRespose.Message = "上传的Excel不包含数据02";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
try
{
var sheetname = sheets[0];
string excelConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename +
";Extended Properties=Excel 8.0;";
if (filename.ToLower().IndexOf(".xlsx") > 0)
{
excelConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename +
";Extended Properties=\"Excel 12.0 Xml;HDR=Yes\"";
}
OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "A1:AA]", excelConn);
DataSet ds = new DataSet();
oada.Fill(ds);
if (ds.Tables.Count == 0)
{
jsonRespose.Success = false;
jsonRespose.Message = "上传的Excel不包含数据03";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
var table = ds.Tables[0];
if (table.Rows.Count == 0)
{
jsonRespose.Success = false;
jsonRespose.Message = "上传的Excel不包含数据04";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
var message = string.Empty;
List<TMSRPT_MONTH_TRUCKDETAIL> headList;
//if (customerFieldName == "委托单位")
//{
var InsertCount = 0;
var UpdateCount = 0;
var UnKnowenTruckNo = "";
var isSucess = TMSRPTDAL.ImportExcelData(Request, table, out message, out InsertCount, out UpdateCount, TMSRPT_GID, out UnKnowenTruckNo, out headList);
if (!isSucess)
{
jsonRespose.Success = false;
jsonRespose.Message = message;
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
var json = JsonConvert.Serialize(new
{
success = true,
Message = "上传成功,共处理" + Convert.ToString(headList.Count) + "条记录,其中更新" + Convert.ToString(UpdateCount) + "个。" + UnKnowenTruckNo,
data = headList.ToList()
});
return new ContentResult() { Content = json };
}
catch (Exception)
{
jsonRespose.Success = false;
jsonRespose.Message = "读取Excel文件出错请确认文件正确性";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
}
public List<string> ExcelSheetName ( string filepath )
{
var al = new List<string>();
try
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
if (filepath.ToLower().IndexOf(".xlsx") > 0)
{
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath +
";Extended Properties=\"Excel 12.0 Xml;HDR=Yes\"";
}
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
System.Data.DataTable sheetNames = conn.GetOleDbSchemaTable
(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
conn.Close();
foreach (DataRow dr in sheetNames.Rows)
{
al.Add(dr[2].ToString());
}
}
catch (Exception)
{
return new List<string>();
}
return al;
}
}
}