using Ease.Core.Model;
using Ease.Core.Utility;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Xml;
namespace HRM.BO
{
#region Import Helper
public sealed class ImportHelper
{
private DataSet _dataSet;
private static char _delimiter;
#region Constructor & Desctructor
public ImportHelper()
{
_delimiter = '\0';
_dataSet = new DataSet("ImportHelper");
}
~ImportHelper()
{
_dataSet.Dispose();
_dataSet = null;
GC.Collect();
}
#endregion
#region Formats Class
public sealed class Formats
{
public readonly static ImportHelper.IFormatter Text = new TextFormatter();
public readonly static ImportHelper.IFormatter Excel = new ExcelFormatter();
private Formats()
{
}
}
#endregion
#region IFormater Interface
public interface IFormatter
{
///
/// Return the dataset after successful operation.
///
/// Valid name of the file.
/// Used the first line as header.
/// Dataset if successfu.
DataSet Import(string fileSpec, bool firstLineColumnHeader);
DataSet Import(System.IO.Stream fileStream, bool firstLineColumnHeader);
}
#endregion
#region Excel Formatter Implementation
private class ExcelFormatter : IFormatter
{
private DataSet _ds;
public ExcelFormatter()
{
_ds = new DataSet("XlsReader");
}
public DataSet Import(string fileSpec, bool firstLineColumnHeader)
{
try
{
_ds = GetDataSetFromFile(fileSpec, firstLineColumnHeader);
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
return _ds;
}
public DataSet Import(System.IO.Stream fileStream, bool firstLineColumnHeader)
{
try
{
_ds = GetDataSetFromFile(fileStream, firstLineColumnHeader);
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
return _ds;
}
}
public static DataSet GetDataSetFromFile(string fileSpec, bool firstLineColumnHeader)
{
try
{
DataSet ds = new DataSet("XlsReader");
FileStream fs = new FileStream(fileSpec, FileMode.Open, FileAccess.Read, FileShare.Read);
IWorkbook wb = WorkbookFactory.Create(fs);
for (int shtIdx = 0; shtIdx < wb.NumberOfSheets; shtIdx++)
ds.Tables.Add(getTable(wb.GetSheetAt(shtIdx), firstLineColumnHeader));
if (fs != null)
{
fs.Close();
fs.Dispose();
fs = null;
}
wb = null;
GC.Collect();
return ds;
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
}
public static DataSet GetDataSetFromFile(System.IO.Stream fileStream, bool firstLineColumnHeader)
{
try
{
DataSet ds = new DataSet("XlsReader");
IWorkbook wb = WorkbookFactory.Create(fileStream);
for (int shtIdx = 0; shtIdx < wb.NumberOfSheets; shtIdx++)
ds.Tables.Add(getTable(wb.GetSheetAt(shtIdx), firstLineColumnHeader));
wb = null;
GC.Collect();
return ds;
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
}
public static DataTable getTable(ISheet sheet, bool firstColIsHeader)
{
try
{
int startRow = sheet.FirstRowNum, idx = 0;
short minCol = short.MaxValue, maxCol = 0;
for (int rowIdx = startRow; rowIdx <= sheet.LastRowNum; rowIdx++)
{
NPOI.SS.UserModel.IRow row = sheet.GetRow(rowIdx);
if (row == null)
continue;
maxCol = maxCol >= row.LastCellNum ? maxCol : row.LastCellNum;
minCol = (minCol >= 0 && minCol < row.FirstCellNum) ? minCol : row.FirstCellNum;
}
DataTable dt = new DataTable(sheet.SheetName);
if (firstColIsHeader)
{
IRow row = sheet.GetRow(startRow);
if (row != null)
{
for (int colIdx = minCol; colIdx < maxCol; colIdx++)
{
string value = row.GetCell(colIdx).StringCellValue;
dt.Columns.Add(value);
}
}
startRow++;
}
else
{
idx = 0;
for (int colIdx = minCol; colIdx < maxCol; colIdx++)
{
idx++;
string value = string.Format("Column{0}", idx);
dt.Columns.Add(value);
}
}
for (int rowIdx = startRow; rowIdx <= sheet.LastRowNum; rowIdx++)
{
DataRow newRow = dt.NewRow();
IRow row = sheet.GetRow(rowIdx);
if (row == null)
continue;
idx = -1;
for (int colIdx = minCol; colIdx < maxCol; colIdx++)
{
idx++;
ICell cell = row.GetCell(colIdx);
if (cell == null)
continue;
if (cell.CellType == CellType.Blank)
newRow[idx] = string.Empty;
else if (cell.CellType == CellType.Boolean)
newRow[idx] = cell.BooleanCellValue;
else if (cell.CellType == CellType.Error)
newRow[idx] = cell.ErrorCellValue;
else if (cell.CellType == CellType.Formula)
newRow[idx] = cell.CellFormula;
else if (cell.CellType == CellType.Numeric)
{
bool isDate = DateUtil.IsCellDateFormatted(cell);
if (isDate)
newRow[idx] = cell.DateCellValue;
else
newRow[idx] = cell.NumericCellValue;
}
else
newRow[idx] = cell.StringCellValue;
}
dt.Rows.Add(newRow);
}
return dt;
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
}
#endregion
#region Text Formatter Implementation
private class TextFormatter : IFormatter
{
private DataSet _ds;
private List