I. Plug-in function: provides various Excel reading methods, such as npoi, COM, and aspose. The Calling interfaces are consistent, including the Excel file path, sheet Name, read whether it contains the column header (that is, whether the first row of Excel is the column header row)
II. Implementation ideas
2.1 define an interface that provides a public method for reading Excel
Public interface iexcelreader {// <summary> // read data from datesheet in Excel to able /// </Summary> /// <Param name = "filepath"> Excel file name </param> /// <Param name = "sheetname"> sheetname </param> /// <Param name = "readheader"> whether to read the first line </Param >/// <returns> </returns> datatable readfromexcel (string filepath, string sheetname = "sheet1", bool readheader = false );}
2.2 each excel reading method defines an implementation class separately and is integrated with the public interface.
Public class excelreadernpoiimpl: iexcelreader {// <summary> // read Excel Data and return it as a datatable (npoi Mode) /// </Summary> /// <Param name = "filepath"> </param> /// <Param name = "sheetname"> </param> /// <Param name = "readheader"> whether to include the column header </param> // <returns> </returns> Public datatable readfromexcel (string filepath, string sheetname = "sheet1", bool readheader = false ){...}} public class excelreaderasposeimpl: iexcelreader {// <summary> // read Excel Data and return it as a datatable (aspose) /// </Summary> /// <Param name = "filepath"> </param> /// <Param name = "sheetname"> </param> /// <Param name = "readheader"> whether to include the column header </param> // <returns> </returns> Public datatable readfromexcel (string filepath, string sheetname = "sheet1", bool readheader = false ){...}}
2.3 define a strategy class and return the object of a specific implementation method through the static method of this class for the caller to operate. The caller does not need to create a specific class on the client. The new process is implemented by the Strategy class.
/// <Summary> /// select the method for reading Excel content /// </Summary> public class excelreadertypeselect {// <summary> // return an aspose implementation iexcelreader // </Summary> /// <returns> </returns> Public static iexcelreader asposereader () {return New excelreaderasposeimpl ();} /// <summary> /// return an iexcelreader implemented by com /// </Summary> /// <returns> </returns> Public static iexcelreader comreader () {return New excelreadercomimpl ();} /// <summary> /// return an iexcelreader implemented by npoi /// </Summary> /// <returns> </returns> Public static iexcelreader npoireader () {return New excelreadernpoiimpl ();}}