|
|
|
|
using DS.Module.Core;
|
|
|
|
|
using DS.Module.ExcelModule.Model;
|
|
|
|
|
using LanguageExt.ClassInstances;
|
|
|
|
|
using LanguageExt;
|
|
|
|
|
using Microsoft.AspNet.SignalR.Hubs;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using MiniExcelLibs;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace DS.Module.ExcelModule
|
|
|
|
|
{
|
|
|
|
|
public class ExcelService : IExcelService
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
|
private readonly ISqlSugarClient db;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 构造函数
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="serviceProvider"></param>
|
|
|
|
|
public ExcelService(IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
_serviceProvider = serviceProvider;
|
|
|
|
|
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public DataResult<string> ExportExcelByColumn(ExportByColumnReq req)
|
|
|
|
|
{
|
|
|
|
|
var filename = Guid.NewGuid() + ".xlsx";
|
|
|
|
|
//var path = Path.Combine(_environment.WebRootPath, "export/" + filename);
|
|
|
|
|
var path = Path.Combine("", "wwwroot/export/" + filename);
|
|
|
|
|
var data = JArray.Parse(req.JsonDataStr);
|
|
|
|
|
|
|
|
|
|
var values = new List<Dictionary<string, object>>();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < data.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var temp = data[i];
|
|
|
|
|
var info = new Dictionary<string, object>();
|
|
|
|
|
for (int n = 0; n < req.ColumnSets.Count; n++)
|
|
|
|
|
{
|
|
|
|
|
var columnSet = req.ColumnSets[n];
|
|
|
|
|
var key = columnSet.DataIndex;
|
|
|
|
|
info.Add(columnSet.Title, temp[key]);
|
|
|
|
|
}
|
|
|
|
|
values.Add(info);
|
|
|
|
|
}
|
|
|
|
|
MiniExcel.SaveAs(path, values);
|
|
|
|
|
return DataResult<string>.Success(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MemoryStream ExportExcelStreamByColumn(ExportByColumnReq req)
|
|
|
|
|
{
|
|
|
|
|
var filename = Guid.NewGuid() + ".xlsx";
|
|
|
|
|
//var path = Path.Combine(_environment.WebRootPath, "export/" + filename);
|
|
|
|
|
var path = Path.Combine("", "wwwroot/export/" + filename);
|
|
|
|
|
var data = JArray.Parse(req.JsonDataStr);
|
|
|
|
|
|
|
|
|
|
var values = new List<Dictionary<string, object>>();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < data.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var temp = data[i];
|
|
|
|
|
var info = new Dictionary<string, object>();
|
|
|
|
|
for (int n = 0; n < req.ColumnSets.Count; n++)
|
|
|
|
|
{
|
|
|
|
|
var columnSet = req.ColumnSets[n];
|
|
|
|
|
var key = columnSet.DataIndex;
|
|
|
|
|
info.Add(columnSet.Title, temp[key]);
|
|
|
|
|
}
|
|
|
|
|
values.Add(info);
|
|
|
|
|
}
|
|
|
|
|
var memoryStream = new MemoryStream();
|
|
|
|
|
memoryStream.SaveAs(values);
|
|
|
|
|
memoryStream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
return memoryStream;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|