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.

185 lines
5.9 KiB
C#

2 months ago
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 "";
}
}
/// <summary>
///将指定的Excel的文件转换成DataTable Excel的第一个sheet
/// </summary>
/// <param name="fullFielPath">文件的绝对路径</param>
/// <returns></returns>
2 months ago
public DataTable WorksheetToTable(string fullFielPath, int SheetIndex = 0)
2 months ago
{
try
{
2 months ago
string[] PathNames = fullFielPath.Split(new string[] { @"/",@"\\", @"\" }, StringSplitOptions.RemoveEmptyEntries);
2 months ago
var ExcelFileName = PathNames[PathNames.Length - 1];
if (ExcelFileName.EndsWith(".xls"))
{
var newExcelFileName = PathNames[PathNames.Length - 1].Replace(".xls", ".xlsx");
2 months ago
var pathhead = Path.GetDirectoryName(fullFielPath);
var _newFileFullname = pathhead + "/" + newExcelFileName;
2 months ago
Workbook book = new Workbook();
book.LoadFromFile(fullFielPath);
book.SaveToFile(_newFileFullname, ExcelVersion.Version2013);
fullFielPath = _newFileFullname;
}
2 months ago
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
2 months ago
FileInfo existingFile = new FileInfo(fullFielPath);
ExcelPackage package = new ExcelPackage(existingFile);
2 months ago
ExcelWorksheet worksheet = package.Workbook.Worksheets[0];//选定 指定页
2 months ago
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;
}
}
/// <summary>
/// 将worksheet转成datatable
/// </summary>
/// <param name="worksheet">待处理的worksheet</param>
/// <returns>返回处理后的datatable</returns>
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;
}
}
}