实现下载模板和导出
This commit is contained in:
parent
efe611101b
commit
5881e7d9d9
|
@ -1,5 +1,18 @@
|
||||||
using Microsoft.AspNetCore.Http;
|
using Competition.Common.Util;
|
||||||
|
using CompetitionAPI.api;
|
||||||
|
using CompetitionAPI.Util;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using NPOI.HSSF.UserModel;
|
||||||
|
using NPOI.SS.Formula.Functions;
|
||||||
|
using NPOI.SS.UserModel;
|
||||||
|
using NPOI.SS.Util;
|
||||||
|
using NPOI.XSSF.UserModel;
|
||||||
|
using System.Data;
|
||||||
|
using System.Drawing;
|
||||||
|
using System.Reflection.PortableExecutable;
|
||||||
|
|
||||||
namespace CompetitionAPI.Controllers.statistics
|
namespace CompetitionAPI.Controllers.statistics
|
||||||
{
|
{
|
||||||
|
@ -7,5 +20,242 @@ namespace CompetitionAPI.Controllers.statistics
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class DownloadBusinessStatisticsController : Controller
|
public class DownloadBusinessStatisticsController : Controller
|
||||||
{
|
{
|
||||||
|
Competition.Mysql.BLL.v_business_statistics bll = new Competition.Mysql.BLL.v_business_statistics();
|
||||||
|
|
||||||
|
public DownloadBusinessStatisticsController()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 下载业务统计接口
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="StatisticsId">统计id</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet]
|
||||||
|
public ActionResult Index(string StatisticsId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = bll.GetModel(StatisticsId);
|
||||||
|
if (result != null)
|
||||||
|
{
|
||||||
|
XSSFWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
|
||||||
|
ICellStyle style = workbook.CreateCellStyle();
|
||||||
|
style.BorderBottom = BorderStyle.Thin;
|
||||||
|
style.BorderLeft = BorderStyle.Thin;
|
||||||
|
style.BorderRight = BorderStyle.Thin;
|
||||||
|
style.BorderTop = BorderStyle.Thin;
|
||||||
|
style.WrapText = true; //若字符串过大换行填入单元格
|
||||||
|
style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
|
||||||
|
style.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
|
||||||
|
IFont font2 = workbook.CreateFont();
|
||||||
|
font2.FontHeightInPoints = 18;
|
||||||
|
font2.FontName = "宋体";
|
||||||
|
font2.Boldweight = (short)FontBoldWeight.Bold; // 或者使用 short.MaxValue
|
||||||
|
style.SetFont(font2);
|
||||||
|
|
||||||
|
ICellStyle style2 = workbook.CreateCellStyle();
|
||||||
|
style2.BorderBottom = BorderStyle.Thin;
|
||||||
|
style2.BorderLeft = BorderStyle.Thin;
|
||||||
|
style2.BorderRight = BorderStyle.Thin;
|
||||||
|
style2.BorderTop = BorderStyle.Thin;
|
||||||
|
style2.WrapText = true; //若字符串过大换行填入单元格
|
||||||
|
style2.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
|
||||||
|
style2.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
|
||||||
|
IFont font3 = workbook.CreateFont();
|
||||||
|
font3.FontHeightInPoints = 12;
|
||||||
|
font3.FontName = "宋体";
|
||||||
|
style2.SetFont(font3);
|
||||||
|
|
||||||
|
ICellStyle style3 = workbook.CreateCellStyle();
|
||||||
|
style3.BorderBottom = BorderStyle.Thin;
|
||||||
|
style3.BorderLeft = BorderStyle.Thin;
|
||||||
|
style3.BorderRight = BorderStyle.Thin;
|
||||||
|
style3.BorderTop = BorderStyle.Thin;
|
||||||
|
style3.WrapText = true; //若字符串过大换行填入单元格
|
||||||
|
style3.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
|
||||||
|
style3.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
|
||||||
|
IFont font4 = workbook.CreateFont();
|
||||||
|
font4.FontHeightInPoints = 12;
|
||||||
|
font4.FontName = "宋体";
|
||||||
|
font4.Boldweight = (short)FontBoldWeight.Bold; // 或者使用 short.MaxValue
|
||||||
|
style3.SetFont(font4);
|
||||||
|
|
||||||
|
ICellStyle style4 = workbook.CreateCellStyle();
|
||||||
|
style4.BorderBottom = BorderStyle.Thin;
|
||||||
|
style4.BorderLeft = BorderStyle.Thin;
|
||||||
|
style4.BorderRight = BorderStyle.Thin;
|
||||||
|
style4.BorderTop = BorderStyle.Thin;
|
||||||
|
style4.WrapText = true; //若字符串过大换行填入单元格
|
||||||
|
style4.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
|
||||||
|
style4.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
|
||||||
|
// 获取工作簿的数据格式对象,用于创建自定义格式
|
||||||
|
IDataFormat dataFormat = workbook.CreateDataFormat();
|
||||||
|
// 定义自定义的日期格式
|
||||||
|
string customDateFormat = "yyyy\"年\"m\"月\"";
|
||||||
|
// 创建自定义格式
|
||||||
|
short formatId = dataFormat.GetFormat(customDateFormat);
|
||||||
|
style4.DataFormat = formatId;
|
||||||
|
IFont font5 = workbook.CreateFont();
|
||||||
|
font5.FontHeightInPoints = 12;
|
||||||
|
font5.FontName = "宋体";
|
||||||
|
font5.Boldweight = (short)FontBoldWeight.Bold; // 或者使用 short.MaxValue
|
||||||
|
style4.SetFont(font5);
|
||||||
|
|
||||||
|
ISheet sheet1 = workbook.CreateSheet("sheet1");
|
||||||
|
sheet1.SetColumnWidth(0, 20 * 256);
|
||||||
|
sheet1.SetColumnWidth(1, 20 * 256);
|
||||||
|
sheet1.SetColumnWidth(2, 20 * 256);
|
||||||
|
sheet1.SetColumnWidth(3, 20 * 256);
|
||||||
|
sheet1.SetColumnWidth(4, 20 * 256);
|
||||||
|
sheet1.SetColumnWidth(5, 20 * 256);
|
||||||
|
sheet1.SetColumnWidth(6, 20 * 256);
|
||||||
|
|
||||||
|
var row1 = sheet1.CreateRow(0);//选择第1行
|
||||||
|
// 设置行高
|
||||||
|
row1.HeightInPoints = 22.5f;
|
||||||
|
var cell1_1 = row1.CreateCell(0);//选择第1列
|
||||||
|
cell1_1.SetCellValue("业务统计表");
|
||||||
|
cell1_1.CellStyle = style;
|
||||||
|
var cell1_2 = row1.CreateCell(1);
|
||||||
|
cell1_2.SetCellValue("");
|
||||||
|
cell1_2.CellStyle = style;
|
||||||
|
var cell1_3 = row1.CreateCell(2);
|
||||||
|
cell1_3.SetCellValue("");
|
||||||
|
cell1_3.CellStyle = style;
|
||||||
|
var cell1_4 = row1.CreateCell(3);
|
||||||
|
cell1_4.SetCellValue("");
|
||||||
|
cell1_4.CellStyle = style;
|
||||||
|
var cell1_5 = row1.CreateCell(4);
|
||||||
|
cell1_5.SetCellValue("");
|
||||||
|
cell1_5.CellStyle = style;
|
||||||
|
var cell1_6 = row1.CreateCell(5);
|
||||||
|
cell1_6.SetCellValue("");
|
||||||
|
cell1_6.CellStyle = style;
|
||||||
|
var cell1_7 = row1.CreateCell(6);
|
||||||
|
cell1_7.SetCellValue("");
|
||||||
|
cell1_7.CellStyle = style;
|
||||||
|
sheet1.AddMergedRegion(new CellRangeAddress(0, 0, 0, 6));//合并单元格,第一行合并9列
|
||||||
|
var start_index = 0;
|
||||||
|
var previousValue = "";
|
||||||
|
var merge_list = new List<string>();
|
||||||
|
var list = JsonConvert.DeserializeObject<List<BusinessStatistics>>(result.StatisticsContent);
|
||||||
|
for (int i = 0; i < list.Count; i++)
|
||||||
|
{
|
||||||
|
var row2 = sheet1.CreateRow(i + 1);//选择第1行
|
||||||
|
if (i == 0)
|
||||||
|
{
|
||||||
|
row2.HeightInPoints = 14.25f;
|
||||||
|
var cell2_1 = row2.CreateCell(0);//选择第1列
|
||||||
|
cell2_1.SetCellValue(list[i].BusinessLine);
|
||||||
|
cell2_1.CellStyle = style3;
|
||||||
|
var cell2_2 = row2.CreateCell(1);
|
||||||
|
cell2_2.SetCellValue(list[i].BusinessName);
|
||||||
|
cell2_2.CellStyle = style3;
|
||||||
|
var cell2_3 = row2.CreateCell(2);
|
||||||
|
cell2_3.SetCellValue(list[i].Unit);
|
||||||
|
cell2_3.CellStyle = style3;
|
||||||
|
var cell2_4 = row2.CreateCell(3);
|
||||||
|
cell2_4.SetCellValue(DateTime.Parse(list[i].Years));
|
||||||
|
cell2_4.CellStyle = style4;
|
||||||
|
var cell2_5 = row2.CreateCell(4);
|
||||||
|
cell2_5.SetCellValue(list[i].YoY);
|
||||||
|
cell2_5.CellStyle = style3;
|
||||||
|
var cell2_6 = row2.CreateCell(5);
|
||||||
|
cell2_6.SetCellValue(list[i].YearInterval);
|
||||||
|
cell2_6.CellStyle = style3;
|
||||||
|
var cell2_7 = row2.CreateCell(6);
|
||||||
|
cell2_7.SetCellValue(list[i].YearsYoY);
|
||||||
|
cell2_7.CellStyle = style3;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
row2.HeightInPoints = 14.25f;
|
||||||
|
var cell2_1 = row2.CreateCell(0);//选择第1列
|
||||||
|
cell2_1.SetCellValue(list[i].BusinessLine);
|
||||||
|
cell2_1.CellStyle = style2;
|
||||||
|
var cell2_2 = row2.CreateCell(1);
|
||||||
|
cell2_2.SetCellValue(list[i].BusinessName);
|
||||||
|
cell2_2.CellStyle = style2;
|
||||||
|
var cell2_3 = row2.CreateCell(2);
|
||||||
|
cell2_3.SetCellValue(list[i].Unit);
|
||||||
|
cell2_3.CellStyle = style2;
|
||||||
|
var cell2_4 = row2.CreateCell(3);
|
||||||
|
cell2_4.SetCellValue(list[i].Years);
|
||||||
|
cell2_4.CellStyle = style2;
|
||||||
|
var cell2_5 = row2.CreateCell(4);
|
||||||
|
cell2_5.SetCellValue(list[i].YoY);
|
||||||
|
cell2_5.CellStyle = style2;
|
||||||
|
var cell2_6 = row2.CreateCell(5);
|
||||||
|
cell2_6.SetCellValue(list[i].YearInterval);
|
||||||
|
cell2_6.CellStyle = style2;
|
||||||
|
var cell2_7 = row2.CreateCell(6);
|
||||||
|
cell2_7.SetCellValue(list[i].YearsYoY);
|
||||||
|
cell2_7.CellStyle = style2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i != 0 && i != list.Count - 1)
|
||||||
|
{
|
||||||
|
if (list[i - 1].BusinessLine != list[i].BusinessLine && list[i + 1].BusinessLine == list[i].BusinessLine)
|
||||||
|
{
|
||||||
|
start_index = i + 1;
|
||||||
|
previousValue = list[i].BusinessLine;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (list[i - 1].BusinessLine == list[i].BusinessLine && list[i + 1].BusinessLine != list[i].BusinessLine)
|
||||||
|
{
|
||||||
|
if (list[i].BusinessLine == previousValue)
|
||||||
|
{
|
||||||
|
sheet1.AddMergedRegion(new CellRangeAddress(start_index, i + 1, 0, 0));
|
||||||
|
previousValue = "";
|
||||||
|
start_index = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (i == list.Count - 1)
|
||||||
|
{
|
||||||
|
if (list[i - 1].BusinessLine != list[i].BusinessLine)
|
||||||
|
{
|
||||||
|
start_index = i + 1;
|
||||||
|
previousValue = list[i].BusinessLine;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (list[i - 1].BusinessLine == list[i].BusinessLine)
|
||||||
|
{
|
||||||
|
if (list[i].BusinessLine == previousValue)
|
||||||
|
{
|
||||||
|
sheet1.AddMergedRegion(new CellRangeAddress(start_index, i + 1, 0, 0));
|
||||||
|
previousValue = "";
|
||||||
|
start_index = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MemoryStreamHelper stream = new MemoryStreamHelper();
|
||||||
|
|
||||||
|
workbook.Write(stream);
|
||||||
|
stream.Flush();
|
||||||
|
stream.Position = 0;
|
||||||
|
|
||||||
|
sheet1 = null;
|
||||||
|
workbook = null;
|
||||||
|
|
||||||
|
var exam_name = result.StatisticsTime.ToString("yyyy年MM月") + "-业务统计表";
|
||||||
|
var ExcelName = exam_name + ".xlsx";
|
||||||
|
return File(stream, "application/vnd.ms-excel", ExcelName);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return Json(Tool.GetJsonWithCode(APICode.Fail, "未找到业务统计信息!"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace);
|
||||||
|
return Json(Tool.GetJsonWithCode(APICode.Fail, "发生错误,请联系管理员。"));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
using Competition.Common.Util;
|
||||||
|
using CompetitionAPI.Util;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace CompetitionAPI.Controllers.statistics
|
||||||
|
{
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
[ApiController]
|
||||||
|
public class DownloadTemplateController : Controller
|
||||||
|
{
|
||||||
|
private readonly IWebHostEnvironment _webHostEnvironment;
|
||||||
|
|
||||||
|
public DownloadTemplateController(IWebHostEnvironment webHostEnvironment)
|
||||||
|
{
|
||||||
|
_webHostEnvironment = webHostEnvironment;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 下载模板接口
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="FileName">模块路径</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet]
|
||||||
|
public ActionResult Index(string FileName)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string web_path = _webHostEnvironment.WebRootPath;
|
||||||
|
var filePath = web_path + "/" + FileName;
|
||||||
|
//以字符流的形式下载文件
|
||||||
|
FileStream fs = new FileStream(filePath, System.IO.FileMode.Open);
|
||||||
|
byte[] bytes = new byte[(int)fs.Length];
|
||||||
|
fs.Read(bytes, 0, bytes.Length);
|
||||||
|
fs.Close();
|
||||||
|
string displayName = Path.GetFileName(filePath);
|
||||||
|
return File(bytes, "application/octet-stream", displayName);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace);
|
||||||
|
return Json(Tool.GetJsonWithCode(APICode.Fail, "发生错误,请联系管理员。"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -27,6 +27,10 @@ namespace CompetitionAPI.Controllers.statistics
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public JsonResult Index(string StatisticsTime = "")
|
public JsonResult Index(string StatisticsTime = "")
|
||||||
{
|
{
|
||||||
|
if (string.IsNullOrEmpty(StatisticsTime))
|
||||||
|
{
|
||||||
|
return Json(Tool.GetJsonWithCode(APICode.Fail, "时间不能为空"));
|
||||||
|
}
|
||||||
var model = bll.GetModelList(" StatisticsTime='" + StatisticsTime + "' ").FirstOrDefault();
|
var model = bll.GetModelList(" StatisticsTime='" + StatisticsTime + "' ").FirstOrDefault();
|
||||||
if (model == null)
|
if (model == null)
|
||||||
{
|
{
|
||||||
|
|
|
@ -39,7 +39,9 @@ namespace CompetitionAPI.Controllers.statistics
|
||||||
query.AppendFormat(" order by CreateTime desc OFFSET {0} ROWS FETCH NEXT {1} ROWS ONLY; ", offset, PageSize);
|
query.AppendFormat(" order by CreateTime desc OFFSET {0} ROWS FETCH NEXT {1} ROWS ONLY; ", offset, PageSize);
|
||||||
|
|
||||||
var total = bll.GetRecordCount(total_query.ToString());
|
var total = bll.GetRecordCount(total_query.ToString());
|
||||||
var list = bll.GetModelList(query.ToString());
|
var data = bll.GetModelList(query.ToString());
|
||||||
|
var list = (from d in data
|
||||||
|
select new { StatisticsId = d.StatisticsId, StatisticsTime = d.StatisticsTime.ToString("yyyy年MM月"), CreateTime = d.CreateTime, Remark1 = d.Remark1, Remark2 = d.Remark2, Remark3 = d.Remark3 }).ToList();
|
||||||
return Json(Tool.GetJsonWithCode(APICode.Success, new { total = total, list = list }));
|
return Json(Tool.GetJsonWithCode(APICode.Success, new { total = total, list = list }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
namespace CompetitionAPI.api
|
||||||
|
{
|
||||||
|
public class MemoryStreamHelper : MemoryStream
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取流是否关闭
|
||||||
|
/// </summary>
|
||||||
|
public bool IsColse
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
private set;
|
||||||
|
}
|
||||||
|
public MemoryStreamHelper(bool colse = false)
|
||||||
|
{
|
||||||
|
IsColse = colse;
|
||||||
|
}
|
||||||
|
public override void Close()
|
||||||
|
{
|
||||||
|
if (IsColse)
|
||||||
|
{
|
||||||
|
base.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue