為何模塊化 模塊化是一種分治思想,不僅可以分離複雜的業務邏輯,還可以進行不同任務的分工。模塊與模塊之間相互獨立,從而構建一種松耦合的應用程式,便於開發和維護。 開發技術 .Net 6 + WPF + Prism (v8.0.0.1909) + HandyControl (v3.4.0) 知識準備 什 ...
需要插件EPPlus.dll、Excel.dll
/// <summary>
/// 讀取 Excel 表並返回一個 DataRowCollection 對象
/// </summary>
/// <param name="_path">Excel 表路徑</param>
/// <param name="_sheetIndex">讀取的 Sheet 索引。Excel 表中是有多個 Sheet 的</param>
/// <returns></returns>
private static DataRowCollection ReadExcel(string _path, int _sheetIndex = 0)
{
FileStream stream = File.Open(_path, FileMode.Open, FileAccess.Read, FileShare.Read);
//IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);//讀取 Excel 1997-2003版本
Excel.IExcelDataReader excelReader = Excel.ExcelReaderFactory.CreateOpenXmlReader(stream);//讀取 2007及以後的版本
DataSet result = excelReader.AsDataSet();
return result.Tables[_sheetIndex].Rows;
}
/// <summary>
/// 讀取 Excel 表並返回一個 DataRowCollection 對象
/// </summary>
/// <param name="_path">Excel 表路徑</param>
/// <param name="_sheetIndex">讀取的 Sheet 名稱。Excel 表中是有多個 Sheet 的</param>
/// <returns></returns>
private static DataRowCollection ReadExcel(string _path, string _sheetName)
{
FileStream stream = File.Open(_path, FileMode.Open, FileAccess.Read, FileShare.Read);
//IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);//讀取 Excel 1997-2003版本
Excel.IExcelDataReader excelReader = Excel.ExcelReaderFactory.CreateOpenXmlReader(stream);//讀取 2007及以後的版本
DataSet result = excelReader.AsDataSet();
return result.Tables[_sheetName].Rows;
}
----------------------------------------------------------------------------------------------------------------------------------------
Excel文件導入--單例
private static SelectFileOrPath _Instance; //本類實例
#region 屬性
private string _SelectFilePath; //選擇的文件路徑
private string _SelectFile; //選擇文件
private string _FileName; //文件名稱
//選擇文件路徑
public string SelectFilePath
{
get
{
return _SelectFilePath;
}
set
{
_SelectFilePath = value;
}
}
//選擇文件
public string SelectFile
{
get
{
return _SelectFile;
}
set
{
_SelectFile = value;
}
}
//文件名稱
public string FileName
{
get
{
return _FileName;
}
set
{
_FileName = value;
}
}
#endregion
/// <summary>
/// 本類實例
/// </summary>
/// <returns></returns>
public static SelectFileOrPath GetInstance()
{
if (_Instance == null)
{
_Instance = new SelectFileOrPath();
}
return _Instance;
}
/// <summary>
/// 選擇文件路徑(Unity自帶的方法)
/// </summary>
public void SelectFolderPath_Unity()
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
SelectFilePath = fbd.SelectedPath;
}
}
/// <summary>
/// 選擇文件路徑(調用Windows方法)
/// </summary>
public void SelectExcelFilePath_Windows()
{
OpenSelectFileName openFileName = new OpenSelectFileName();
openFileName.structSize = Marshal.SizeOf(openFileName);
openFileName.filter = "Excel文件(*.xlsx)\0*.xlsx";
openFileName.file = new string(new char[256]);
openFileName.maxFile = openFileName.file.Length;
openFileName.fileTitle = new string(new char[64]);
openFileName.maxFileTitle = openFileName.fileTitle.Length;
openFileName.initialDir = UnityEngine.Application.streamingAssetsPath.Replace('/', '\\');//預設路徑
openFileName.title = "請選擇文件";
openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;
if (LocalDialog.GetSaveFileName(openFileName))
{
SelectFilePath = openFileName.file;
FileName = openFileName.file;
}
}