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.

189 lines
6.5 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;
1 month ago
using OfficeOpenXml;
using NPOI.Util;
using NPOI.SS.UserModel;
using NPOI.HPSF;
using DS.Module.Core.Extensions;
namespace DS.Module.ExcelModule
{
public class ExcelService : IExcelService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
static readonly ApiFox api;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="serviceProvider"></param>
static ExcelService()
{
api = new ApiFox();
}
/// <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);
8 months ago
}
public async Task<MemoryStream> ExportExcelStreamByColumnAsync(ExportByColumnReq req)
{
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;
4 weeks ago
info.Add(columnSet.Title, temp[key]);
//if (temp[key].ObjToString().ToLower()== "false")
//{
// info.Add(columnSet.Title, "否");
//}
//if (temp[key].ObjToString().ToLower() == "true")
//{
// info.Add(columnSet.Title, "是");
//}
//else
//{
// 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;
}
2 months ago
8 months ago
public MemoryStream ExportExcelStreamByColumn(ExportByColumnReq req)
{
1 month ago
//var filename = Guid.NewGuid() + ".xlsx";
8 months ago
//var path = Path.Combine(_environment.WebRootPath, "export/" + filename);
1 month ago
//var path = Path.Combine("", "wwwroot/export/" + filename);
8 months ago
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);
}
1 month ago
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;
}
1 month ago
}
}