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.

87 lines
2.9 KiB
C#

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];
8 months ago
var key = columnSet.DataIndex;
info.Add(columnSet.Title, temp[key]);
}
values.Add(info);
}
MiniExcel.SaveAs(path, values);
return DataResult<string>.Success(path);
8 months ago
}
8 months ago
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;
}
}
}