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 NPOI.SS.UserModel;
using OfficeOpenXml;
using System.Data;
using Spire.Xls;
namespace DS.Module.ExcelModule
{
public class EPPlusService : IEPPlusService
{
private readonly IServiceProvider _serviceProvider;
private static string GetString(object obj)
{
try
{
if (obj == null) return "";
return obj.ToString();
}
catch (Exception ex)
{
return "";
}
}
///
///将指定的Excel的文件转换成DataTable (Excel的第一个sheet)
///
/// 文件的绝对路径
///
public DataTable WorksheetToTable(string fullFielPath, int SheetIndex = 0)
{
try
{
string[] PathNames = fullFielPath.Split(new string[] { @"/",@"\\", @"\" }, StringSplitOptions.RemoveEmptyEntries);
var ExcelFileName = PathNames[PathNames.Length - 1];
if (ExcelFileName.EndsWith(".xls"))
{
var newExcelFileName = PathNames[PathNames.Length - 1].Replace(".xls", ".xlsx");
var pathhead = Path.GetDirectoryName(fullFielPath);
var _newFileFullname = pathhead + "/" + newExcelFileName;
Workbook book = new Workbook();
book.LoadFromFile(fullFielPath);
book.SaveToFile(_newFileFullname, ExcelVersion.Version2013);
fullFielPath = _newFileFullname;
}
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
FileInfo existingFile = new FileInfo(fullFielPath);
ExcelPackage package = new ExcelPackage(existingFile);
ExcelWorksheet worksheet = package.Workbook.Worksheets[0];//选定 指定页
return WorksheetToTable(worksheet);
}
catch (Exception e)
{
throw;
}
}
public DataTable WorksheetToTable_NoTitle(string fullFielPath, int SheetIndex = 1)
{
try
{
string[] PathNames = fullFielPath.Split(new string[] { @"/" }, StringSplitOptions.RemoveEmptyEntries);
var ExcelFileName = PathNames[PathNames.Length - 1];
if (ExcelFileName.EndsWith(".xls"))
{
var newExcelFileName = PathNames[PathNames.Length - 1].Replace(".xls", ".xlsx");
var _newFileFullname = Path.GetDirectoryName(fullFielPath) + "/" + newExcelFileName;
Workbook book = new Workbook();
book.LoadFromFile(fullFielPath);
book.SaveToFile(_newFileFullname, ExcelVersion.Version2013);
fullFielPath = _newFileFullname;
}
FileInfo existingFile = new FileInfo(fullFielPath);
ExcelPackage package = new ExcelPackage(existingFile);
ExcelWorksheet worksheet = package.Workbook.Worksheets[SheetIndex];//选定 指定页
return WorksheetToTable_NoTitle(worksheet);
}
catch (Exception e)
{
throw;
}
}
///
/// 将worksheet转成datatable
///
/// 待处理的worksheet
/// 返回处理后的datatable
public DataTable WorksheetToTable(ExcelWorksheet worksheet)
{
//获取worksheet的行数
int rows = worksheet.Dimension.End.Row;
//获取worksheet的列数
int cols = worksheet.Dimension.End.Column;
DataTable dt = new DataTable(worksheet.Name);
DataRow dr = null;
for (int i = 1; i <= rows; i++)
{
if (i > 1)
dr = dt.Rows.Add();
for (int j = 1; j <= cols; j++)
{
//默认将第一行设置为datatable的标题
if (i == 1)
dt.Columns.Add(GetString(worksheet.Cells[i, j].Value));
//剩下的写入datatable
else
dr[j - 1] = GetString(worksheet.Cells[i, j].Value);
}
}
return dt;
}
public DataTable WorksheetToTable_NoTitle(ExcelWorksheet worksheet)
{
//获取worksheet的行数
int rows = worksheet.Dimension.End.Row;
//获取worksheet的列数
int cols = worksheet.Dimension.End.Column;
DataTable dt = new DataTable(worksheet.Name);
DataRow dr = null;
for (int i = 1; i <= rows; i++)
{
//if (i > 1)
dr = dt.Rows.Add();
for (int j = 1; j <= cols; j++)
{
//默认将第一行设置为datatable的标题
if (i == 1)
{
dt.Columns.Add(GetString(worksheet.Cells[i, j].Value));
dr[j - 1] = GetString(worksheet.Cells[i, j].Value);
}
//剩下的写入datatable
else
{
dr[j - 1] = GetString(worksheet.Cells[i, j].Value);
}
}
}
return dt;
}
}
}