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;
using OfficeOpenXml;
using NPOI.Util;
using NPOI.SS.UserModel;
using NPOI.HPSF;
namespace DS.Module.ExcelModule
{
public class ExcelService : IExcelService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
static readonly ApiFox api;
///
/// 构造函数
///
///
static ExcelService()
{
api = new ApiFox();
}
///
/// 构造函数
///
///
public ExcelService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService();
}
public DataResult 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>();
for (int i = 0; i < data.Count; i++)
{
var temp = data[i];
var info = new Dictionary();
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.Success(path);
}
public async Task ExportExcelStreamByColumnAsync(ExportByColumnReq req)
{
var data = JArray.Parse(req.JsonDataStr);
var values = new List>();
for (int i = 0; i < data.Count; i++)
{
var temp = data[i];
var info = new Dictionary();
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 stream = new MemoryStream();
await MiniExcel.SaveAsAsync(stream, values);
stream.Seek(0, SeekOrigin.Begin);
return stream;
}
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>();
for (int i = 0; i < data.Count; i++)
{
var temp = data[i];
var info = new Dictionary();
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 stream = new MemoryStream();
MiniExcel.SaveAs(stream, values);
stream.Seek(0, SeekOrigin.Begin);
return stream;
//var data = JArray.Parse(req.JsonDataStr);
//ExcelPackage.LicenseContext = LicenseContext.NonCommercial; // 设置许可
//var stream = new MemoryStream();
//using (var package = new ExcelPackage(stream))
//{
// var worksheet = package.Workbook.Worksheets.Add("Sheet1");
// // 填充数据
// //worksheet.Cells[1, 1].Value = "Column1";
// //worksheet.Cells[1, 2].Value = "Column2";
// //worksheet.Cells[2, 1].Value = "Data1";
// //worksheet.Cells[2, 2].Value = "Data2";
// if (data != null && req.ColumnSets != null)
// {
// var _count = 1;
// foreach (var title in req.ColumnSets) {
// worksheet.Cells[1, _count].Value = title.Title;
// _count++;
// }
// for (int i = 0; i < data.Count; i++)
// {
// var temp = data[i];
// for (int n = 0; n < req.ColumnSets.Count; n++)
// {
// var columnSet = req.ColumnSets[n];
// var key = columnSet.DataIndex;
// worksheet.Cells[i+2, n+1].Value = temp[key].ToString();
// }
// }
// }
// // 保存
// package.Save();
//}
////using (var fileStream = File.Create("D:\\temp.xlsx"))
////{
//// stream.Seek(0, SeekOrigin.Begin);//设置复制开始的地方
//// stream.CopyTo(fileStream);
////}
//stream.Seek(0, SeekOrigin.Begin);
//stream.Position = 0;
//return stream;
}
}
}