function inportEx()
{
$("#btnEx").text("导出中...");
var fugNumber = "";//第几次随访
var fugid = "";//随访组Id
var fudid = "";//医生id
var zTree = $.fn.zTree.getZTreeObj("tree");
var nodes = zTree.getSelectedNodes();
//获取选中节点的id值
if (nodes.length > 0) {
var pnode = nodes[0].getParentNode();
if (pnode != null && pnode != "") {
fugid = pnode.FuGID;
}
// fudid = nodes[0].FuGID;
}
//获取需要导出excel的患者的id
var chek_value_id = [];
$('input[name="checkItem"]:checked').each(function () {
chek_value_id.push($(this).val());
}); var treeObj = $.fn.zTree.getZTreeObj("treeExcel");
var nodes = treeObj.getCheckedNodes(true); //将患者id数组转化为字符串传到后台
var pId = chek_value_id.join(",");
$.ajax({
url: '/Patient/SaveExcel',// 跳转到 action
data: {
pid: pId,
pfid:pfid,
fugid: fugid
},
type: 'get',
cache: false,
dataType: 'json',
success: function (result) {
console.log(result);
if (checkSession(result)) {
if (result != null && result.length > 0) {
console.log(result);
downloadFile(result);
}
} },
complete: function (XMLHttpRequest, textStatus) { pfid = "";
$('#myModal').modal("hide");
}
});
}
//文件下载
function downloadFile(url) {
try {
var elemIF = document.createElement("iframe");
elemIF.src = url;
elemIF.style.display = "none";
document.body.appendChild(elemIF);
} catch (e) { }
}
public async Task<JsonResult> SaveExcel(string pId, string pfid, string fugid)
{ //使用js导出excel
string dir = Server.MapPath("/");
string serverUrl = HttpContext.Request.Url.Authority;
string filePath = "Excel\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + "\\";
dir = dir + filePath;
string fileName = "患者管理" + DateTime.Now.ToString("yyyyMMddhhmmss");
string path = dir + fileName + ".xls";
string rtnValue = "http://" + serverUrl + "/Excel/" + DateTime.Now.ToString("yyyyMMddhhmmss") + "/" + fileName + ".xls";
if (!Directory.Exists(dir))
{
//创建文件所在目录
Directory.CreateDirectory(dir);
}
//获取该随访组的随访计划和问卷信息
List<FllowAndPlanName> list = new List<FllowAndPlanName>();
list = await patientBLL.SelectFllowPlan(fugid);
int listGroup = (list.GroupBy(x => x.FllowPlan_id)).Count();
var _list = await patientBLL.getFllowInfoList(fugid);
#region 绘制表头
string[] arr = { "序号", "姓名", "性别", "年龄", "联系电话", "随访医生", "最近一次随访", "计划次数", "共随访次数", "病历数", "咨询数", "下次随访时间", "状态" };
HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
//添加一个sheet1
ISheet sheet1 = book.CreateSheet("Sheet1");
CellRangeAddress m_region = new CellRangeAddress(, , , arr.Length - ); //合并0列的n--n+2行
sheet1.AddMergedRegion(m_region);
IRow row = sheet1.CreateRow();
ICell cell = row.CreateCell();
cell.SetCellValue("基本信息");
ICellStyle cellstyle = book.CreateCellStyle();//设置垂直居中格式
cellstyle.VerticalAlignment = VerticalAlignment.CENTER;//垂直居中
cellstyle.Alignment = HorizontalAlignment.CENTER;//水平居中
cell.CellStyle = cellstyle; IRow row2 = sheet1.CreateRow();
for (int i = ; i < arr.Length; i++)
{
row2.CreateCell(i).SetCellValue(arr[i]);
} var title = _list.GroupBy(x => x.FllowPlan_Name);
List<string> planName = new List<string>();//随访计划名称
List<string> fllowName = new List<string>();//问卷名称
Dictionary<string, string> timu = new Dictionary<string, string>();//当前问卷下的问卷题目
List<string> timuList = new List<string>();
Dictionary<string, int> timuResult = new Dictionary<string, int>();//当前问卷下的答案的个数
Dictionary<string, string> fllowInfo = new Dictionary<string, string>();//问卷名称和问卷id
var listInfo = list.GroupBy(x => x.FollowInfo_ID).Select(g => g.First());
foreach (var info in listInfo)
{
fllowInfo.Add(info.FollowInfo_Name, info.FollowInfo_ID);
}
//获取随访计划和问卷信息
foreach (var item in title)
{
planName.Add(item.Key.ToString());
foreach (var name in item)
{
if (!fllowName.Contains(name.FollowInfo_Name))
{
fllowName.Add(name.FollowInfo_Name);
}
if (!timu.ContainsKey(name.Question_Name))
{
timu.Add(name.Question_Name, name.FollowInfo_Name);
}
}
}
//获取当前问卷下的题目的个数
var tGroup = timu.GroupBy(x => x.Value);
foreach (var item in tGroup)
{
foreach (var count in item)
{
if (!timuResult.ContainsKey(count.Value))
{
timuResult.Add(count.Value, item.Count());
}
}
}
//获取题目集合
foreach (KeyValuePair<string, string> item in timu)
{
timuList.Add(item.Key);
}
int rowOne = arr.Length;
int rowTwo = arr.Length;
//用来控制题目的列数
List<int> timuNum = new List<int>();
IRow row3 = sheet1.CreateRow();
IRow row4 = sheet1.CreateRow();
int num = ;
//绘制随访计划列
for (int i = ; i < planName.Count; i++)
{
//绘制问卷列
for (int j = arr.Length; j < fllowName.Count + arr.Length; j++)
{
//获取当前问卷下面的题目个数
int r = timuResult[fllowName[j - arr.Length].ToString()];
sheet1.AddMergedRegion(new CellRangeAddress(, , rowTwo, rowTwo + r - ));
ICell cellFllow = row4.CreateCell(rowTwo);
cellFllow.CellStyle = cellstyle;
cellFllow.SetCellValue(fllowName[j - arr.Length].ToString());
rowTwo += r;
}
sheet1.AddMergedRegion(new CellRangeAddress(, , rowOne, rowOne + timu.Count - ));
ICell cellPlan = row.CreateCell(rowOne);
cellPlan.CellStyle = cellstyle;
cellPlan.SetCellValue(planName[i]);
rowOne += timu.Count;
num += ; }
//绘制题目列
for (int u = ; u < planName.Count; u++)
{
for (int k = ; k < timuList.Count; k++)
{ ICell cellTimu = row2.CreateCell(timuNum.Count + arr.Length);
cellTimu.CellStyle = cellstyle;
cellTimu.SetCellValue(timuList[k].ToString());
timuNum.Add(k); }
} #endregion
#region Databtable的建立
DataTable ExData = new DataTable(); var tablleTitle = await patientBLL.getFllowInfoList(fugid);
var infoCode = tablleTitle.GroupBy(x => x.FollowInfo_ID).Select(p => (new { Infoid = p.Key }));
List<string> infoCodeList = new List<string>();
foreach (var code in infoCode)
{
infoCodeList.Add(code.Infoid);
}
ExData.Columns.Add("序号", System.Type.GetType("System.String"));
ExData.Columns.Add("姓名", System.Type.GetType("System.String"));
ExData.Columns.Add("性别", System.Type.GetType("System.String"));
ExData.Columns.Add("年龄", System.Type.GetType("System.String"));
ExData.Columns.Add("联系电话", System.Type.GetType("System.String"));
ExData.Columns.Add("随访医生", System.Type.GetType("System.String"));
ExData.Columns.Add("最近一次随访", System.Type.GetType("System.String"));
ExData.Columns.Add("计划次数", System.Type.GetType("System.String"));
ExData.Columns.Add("共随访次数", System.Type.GetType("System.String"));
ExData.Columns.Add("病历数", System.Type.GetType("System.String"));
ExData.Columns.Add("咨询数", System.Type.GetType("System.String"));
ExData.Columns.Add("下次随访时间", System.Type.GetType("System.String"));
ExData.Columns.Add("状态", System.Type.GetType("System.String"));
//把选择的随访计划和问卷信息做处理
string[] singleInfo = pfid.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
int timeYear = DateTime.Now.Year;
string[] singlePid = pId.Split(new char[] { ',' }, StringSplitOptions.None);
//当前随访组下的随访计划
for (int w = ; w < planName.Count + ; w++)
{
for (int t = ; t < timuList.Count; t++)
{
ExData.Columns.Add(timuList[t] + w, System.Type.GetType("System.String")); } }
//选择患者的个数
for (int p = ; p < singlePid.Length; p++)
{
// var excutePlanid = await patientBLL.getExcutePlan_ID(singlePid[p]);
DataRow ExRow = ExData.NewRow(); for (int m = ; m < planName.Count + ; m++)
{ #region 获取数据 var res = await patientBLL.SavaExcel(singlePid[p], fugid);
ExRow["序号"] = p + ;
ExRow["姓名"] = res[].Patient_ID.ToString();
ExRow["性别"] = res[].Patient_Sex.ToString();
ExRow["年龄"] = (timeYear - res[].age.Value.Year).ToString() + "岁";
ExRow["联系电话"] = res[].Login_Account.ToString();
ExRow["随访医生"] = res[].fudname.ToString();
ExRow["最近一次随访"] = res[].followUp_Last_time.ToString();
ExRow["计划次数"] = res[].planCount.ToString();
ExRow["共随访次数"] = res[].followUpCount.ToString();
ExRow["病历数"] = res[].AdvisoryRecordCount.ToString();
ExRow["咨询数"] = res[].medicalCount.ToString();
ExRow["下次随访时间"] = res[].followUp_Next_time.ToString();
ExRow["状态"] = res[].FuP_Status == "" ? "在访" : (res[].FuP_Status == "" ? "失访" : (res[].FuP_Status == "-2" ? "死亡" : "出组")); for (int i = ; i < singleInfo.Length; i++)
{ string[] info = singleInfo[i].Split(new char[] { ',' });
//选择了随访计划
if (info[] == m.ToString() && info.Length == )
{
var quesResult = await patientBLL.SavaExcel(singlePid[p], fugid);
var querRes = quesResult.Where(x => x.fllowplanid.Equals(info[]));
foreach (var item in querRes)
{
if (item.Question_Name != null) {
string index = item.Question_Name + m;
ExRow[index] = item.Choice_Content;
} }
}
//选择了随访计划的某一个问卷
if (info[] == m.ToString() && info.Length == )
{
var querResult_1 = await patientBLL.SavaExcel(singlePid[], fugid);
var querRes_1 = querResult_1.Where(x => x.QBClass_Code.Equals(info[]) && x.fllowplanid.Equals(info[]));
foreach (var item in querRes_1)
{
if (item.Question_Name != null)
{
string index = item.Question_Name + m;
ExRow[index] = item.Choice_Content; }
}
}
//如果一次随访计划下有多个问卷,选择了一个或者多个问卷
if (info[] == m.ToString() && info.Length < )
{
string[] condition = info[].Split(new char[] { '*' }, StringSplitOptions.RemoveEmptyEntries);
for (int k = ; k < condition.Length; k++)
{
string[] singCondtion = condition[k].ToString().Split(new char[] { '@' });
var querResult_2 = await patientBLL.SavaExcel(singlePid[], fugid);
// List<excelModel> querRs = new List<excelModel>();
for (int n = ; n < singCondtion.Length; n++)
{
var querRes_2 = querResult_2.Where(x => x.QBClass_Code.Equals(singCondtion[]) && x.fllowplanid.Equals(info[])).ToList();
foreach (var item in querRes_2)
{
if (item.Question_Name != null)
{
string index = item.Question_Name + m;
ExRow[index] = item.Choice_Content;
}
}
//querRs.AddRange(querRes_2);
}
}
}
}
#endregion
}
ExData.Rows.Add(ExRow);
}
//将数据写入到excel
for (int i = ; i < ExData.Rows.Count; i++)
{
row = sheet1.CreateRow(i + );
ExData.Rows[i][] = (i + ).ToString();
for (int j = ; j < ExData.Columns.Count; j++)
{
ICell ce = row.CreateCell(j);
ce.SetCellValue(ExData.Rows[i][j].ToString());
} }
#endregion using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
book.Write(fs);
fs.Close();
}
return Json(rtnValue, JsonRequestBehavior.AllowGet); }