52 lines
1.2 KiB
C#
52 lines
1.2 KiB
C#
|
|
using ExcelDataReader;
|
|||
|
|
using System;
|
|||
|
|
using System.Data;
|
|||
|
|
using System.IO;
|
|||
|
|
|
|||
|
|
namespace OnlineSalesAutoCrop.CoreAPI.Models.Global
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
///
|
|||
|
|
/// </summary>
|
|||
|
|
public static class ExcelReader
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
///
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="fileSpec"></param>
|
|||
|
|
/// <param name="firstRowColumnHeader"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
/// <exception cref="Exception"></exception>
|
|||
|
|
public static DataSet GetDataSetFromFile(string fileSpec, bool firstRowColumnHeader)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
DataSet ds;
|
|||
|
|
using FileStream stream = File.Open(fileSpec, FileMode.Open, FileAccess.Read);
|
|||
|
|
using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream))
|
|||
|
|
{
|
|||
|
|
ds = reader.AsDataSet();
|
|||
|
|
foreach (DataTable table in ds.Tables)
|
|||
|
|
{
|
|||
|
|
if (firstRowColumnHeader && table.Rows.Count > 0)
|
|||
|
|
{
|
|||
|
|
object[] columns = table.Rows[0].ItemArray;
|
|||
|
|
table.Rows.RemoveAt(0);
|
|||
|
|
for (int colIdx = 0; colIdx < columns.Length; colIdx++)
|
|||
|
|
{
|
|||
|
|
table.Columns[colIdx].ColumnName = $"{columns[colIdx]}";
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
stream.Close();
|
|||
|
|
|
|||
|
|
return ds;
|
|||
|
|
}
|
|||
|
|
catch (Exception e)
|
|||
|
|
{
|
|||
|
|
throw new InvalidOperationException(e.Message, e);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|