Commit b2fcc28e by wyc

科室绩效详情下载

parent 12a13c86
......@@ -213,6 +213,32 @@ public ApiResponse UniteDeptDetail([FromBody] UniteDeptDetailRequest request)
}
[Route("download/unite/deptdetail")]
[HttpPost]
public IActionResult DownloadUniteDeptdetail([FromBody] UniteDeptDetailRequest request)
{
request.UnitType = request.UnitType.Replace("行政后勤", "行政工勤");
if (!Enum.TryParse(request.UnitType, ignoreCase: true, out UnitType unitType))
return BadRequest("核算组别错误");
var userid = _claim.GetUserId();
var uniteDeptDetail = _computeService.UniteDeptDetail(request.AllotId, unitType, request.AccountingUnit, userid);
//var ser = JsonConvert.SerializeObject(response);
//var rows = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(ser);
//Dictionary<string, object> rows = _computeService.ConvertToDictionary(response);
var filepath = _computeService.ExcelDownload(uniteDeptDetail, "科室绩效详情", request.AllotId);
var memoryStream = new MemoryStream();
using (var stream = new FileStream(filepath, FileMode.Open))
{
stream.CopyToAsync(memoryStream).Wait();
}
memoryStream.Seek(0, SeekOrigin.Begin);
var provider = new FileExtensionContentTypeProvider();
FileInfo fileInfo = new FileInfo(filepath);
var memi = provider.Mappings[".xlsx"];
return File(memoryStream, memi, Path.GetFileName(fileInfo.Name));
}
#region 20230531 即将弃用,由新接口替代
......
......@@ -605,106 +605,91 @@ public void SaveUniteDeptDetailSetting(int allotId, List<UniteDeptDetailItem> re
}
#region 科室详情下载
//public string ExcelDownload(List<Dictionary<string, object>> rows, string name, int allotId)
//{
// var perAllot = perforPerallotRepository.GetEntity(t => t.ID == allotId);
// string title = $"{perAllot.Year}年{perAllot.Month}月{name}";
// var data = new List<Dictionary<string, object>>();
// foreach (var obj in rows)
// {
// Dictionary<string, object> nobj = new Dictionary<string, object>();
// foreach (var item in obj)
// {
// var lower = item.Key.ToLower();
// if (lower.Contains("date"))
// nobj[lower] = Convert.ToDateTime(item.Value).ToString("d");
// else if (lower.Contains("detial"))
// {
// var detRows = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(item.Value.ToString());
// foreach (var detlist in detRows)
// {
// object value = null;
// foreach (var detitem in detlist)
// {
// if (detitem.Key.Equals("Value", StringComparison.OrdinalIgnoreCase))
// value = detitem.Value;
// if (detitem.Key.Equals("Title", StringComparison.OrdinalIgnoreCase))
// nobj[detitem.Value.ToString()] = value;
// }
// }
// }
// else
// nobj[lower] = item.Value;
// }
// data.Add(nobj);
// }
// var dpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Files");
// if (!Directory.Exists(dpath)) Directory.CreateDirectory(dpath);
// string filepath = Path.Combine(dpath, $"{name}{DateTime.Now:yyyy年MM月dd日}");
// if (File.Exists(filepath)) File.Delete(filepath);
// using (FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate))
// using (ExcelPackage package = new ExcelPackage(fs))
// {
// var worksheet = package.Workbook.Worksheets.Add(name);
// if (rows != null && rows.Count() > 0)
// {
// worksheet.SetValue(1, 1, title);
// for (int col = 0; col < headList.Count; col++)
// {
// worksheet.SetValue(2, col + 1, headList[col].Alias);
// }
// for (int col = 0; col < headList.Count; col++)
// {
// for (int row = 0; row < data.Count(); row++)
// {
// var temp = data.ElementAt(row);
// var key = headList[col].Name;
// if (!temp.ContainsKey(key))
// key = key.ToLower();
// temp.TryGetValue(key, out object value);
// worksheet.Cells[row + 3, col + 1].Value = value ?? "";
// }
// }
// #region 样式设置
// for (int row = worksheet.Dimension.Start.Row; row <= worksheet.Dimension.End.Row; row++)
// {
// worksheet.Row(row).Height = 20;
// for (int col = worksheet.Dimension.Start.Column; col <= worksheet.Dimension.End.Column; col++)
// {
// worksheet.Cells[row, col].Style.Border.BorderAround(ExcelBorderStyle.Thin);
// worksheet.Cells[row, col].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
// worksheet.Cells[row, col].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
// }
// }
// //worksheet.Cells[1, 1, 1, headList.Count].Merge = true;
// //worksheet.Cells[1, 1, 1, headList.Count].Style.Font.Bold = true;
// //worksheet.Cells[1, 1, 1, headList.Count].Style.Font.Size = 16;
// //worksheet.Row(1).Height = 24;
// //worksheet.Cells[2, 1, 2, headList.Count].Style.Font.Bold = true;
// worksheet.View.FreezePanes(3, 1);
// worksheet.Cells.AutoFitColumns();
// for (int col = worksheet.Dimension.Start.Column; col <= worksheet.Dimension.End.Column; col++)
// {
// worksheet.Column(col).Width = worksheet.Column(col).Width > 20 ? 20 : worksheet.Column(col).Width;
// }
// #endregion
// }
// package.Save();
// }
// return filepath;
//}
public string ExcelDownload(UniteDeptDetailResponse uniteDeptDetail, string name, int allotId)
{
var dpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Files");
if (!Directory.Exists(dpath)) Directory.CreateDirectory(dpath);
string filepath = Path.Combine(dpath, $"{name}{DateTime.Now:yyyyMMdd}");
if (File.Exists(filepath)) File.Delete(filepath);
using (FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate))
using (ExcelPackage package = new ExcelPackage(fs))
{
var maxColIndex = 10;
var worksheet = package.Workbook.Worksheets.Add(name);
worksheet.SetValue(1, 1, uniteDeptDetail.Title);
worksheet.Cells[1, 1, 1, maxColIndex].Merge = true;
var rowIndex = 1;
xx(worksheet, maxColIndex, rowIndex, uniteDeptDetail.DetailItems);
//#region 样式设置
//for (int row = worksheet.Dimension.Start.Row; row <= worksheet.Dimension.End.Row; row++)
//{
// worksheet.Row(row).Height = 20;
// for (int col = worksheet.Dimension.Start.Column; col <= worksheet.Dimension.End.Column; col++)
// {
// worksheet.Cells[row, col].Style.Border.BorderAround(ExcelBorderStyle.Thin);
// worksheet.Cells[row, col].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
// worksheet.Cells[row, col].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
// }
//}
////worksheet.Cells[1, 1, 1, headList.Count].Merge = true;
////worksheet.Cells[1, 1, 1, headList.Count].Style.Font.Bold = true;
////worksheet.Cells[1, 1, 1, headList.Count].Style.Font.Size = 16;
////worksheet.Row(1).Height = 24;
////worksheet.Cells[2, 1, 2, headList.Count].Style.Font.Bold = true;
//worksheet.Cells.AutoFitColumns();
//for (int col = worksheet.Dimension.Start.Column; col <= worksheet.Dimension.End.Column; col++)
//{
// worksheet.Column(col).Width = worksheet.Column(col).Width > 20 ? 20 : worksheet.Column(col).Width;
//}
//#endregion
package.Save();
}
return filepath;
}
void xx(ExcelWorksheet worksheet, int maxColIndex, int rowIndex, List<DeptDetailItem> detailItems)
{
foreach (var item in detailItems)
{
rowIndex = rowIndex + 1;
worksheet.SetValue(rowIndex, 1, $"{item.Title}:{item.TotalFormat}");
worksheet.Cells[rowIndex, 1, rowIndex, maxColIndex].Merge = true;
if (item.Children?.Any() == true)
{
xx(worksheet, maxColIndex, rowIndex, item.Children);
}
else if (item.Items?.Any() == true)
{
rowIndex = rowIndex + 1;
var headers = item.Items.FirstOrDefault().Keys.Select(w => w);
for (int colIndex = 0; colIndex < headers.Count(); colIndex++)
{
worksheet.SetValue(rowIndex, colIndex + 1, headers.ElementAt(colIndex));
if (colIndex + 1 == headers.Count())
worksheet.Cells[rowIndex, colIndex, rowIndex, maxColIndex].Merge = true;
}
rowIndex = rowIndex + 1;
foreach (var row in item.Items)
{
rowIndex = rowIndex + 1;
for (int colIndex = 0; colIndex < headers.Count(); colIndex++)
{
worksheet.SetValue(rowIndex, colIndex + 1, row[headers.ElementAt(colIndex)]);
if (colIndex + 1 == headers.Count())
worksheet.Cells[rowIndex, colIndex, rowIndex, maxColIndex].Merge = true;
}
}
}
}
}
#endregion
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment