Export Excel Based on NPOI and NPOI
In the previous article [query and export of big data], I mentioned how to use the NPOI component to export an Excel file. I wanted to share it with you last time, but I was very busy recently. Today I took the time to sort it out, share it.
Based on the above analysis, we will briefly draw a UML diagram of this function to facilitate understanding.
/// <Summary> /// export interface /// </summary> public interface IExport {/// <summary> /// export data, after the template file is replaced, save as a new file /// </summary> /// <param name = "templateFile"> template file </param> /// <param name = "targetFile"> target file </param> /// <param name = "fromater"> template formatting rules </param> void Export (string templateFile, string targetFile, ExportFormater fromater); // <summary> // export data, save as a new file // </summary> /// <typeparam name = "T"> Data Source Type </typeparam> /// <param name = "templateFile"> template File </param> /// <param name = "targetFile"> target file </param> /// <param name = "formater"> template formatting rules </ param> // <param name = "source"> data source </param> void Export <T> (string templateFile, string targetFile, ExportFormater <T> formater, IList <T> source) where T: class; // <summary> /// append, add data to an existing file /// </summary> /// <typeparam name = "T"> Data Source Type </typeparam> /// <param name = "targetFile"> target file </param> /// <param name = "formater"> template formatting rules </param> /// <param name = "source"> data source </param> void ExportByAppend <T> (string targetFile, exportFormater <T> formater, IList <T> source) where T: class ;}
Hash cell format Processor/// <Summary> /// hashed cell data formatter /// </summary> public class DispersedCellFormater {/// <summary> // cell coordinate /// </summary> public Point CellPoint {get; set ;}//< summary> /// format the string /// </summary> public string FormaterString {get; set ;} /// <summary> /// format the data /// </summary> public object CellValue {get; set ;} /// <summary> /// instantiate /// </summary> public DispersedCellFormater () {}/// <summary> /// instantiate /// </summary> /// <param name = "x"> cell abscissa </param> // <param name = "y"> cell ordinate </param> /// <param name = "formatStr"> Format String </param> /// <param name = "val"> replace value </param> public DispersedCellFormater (int x, int y, string formatStr, object val) {this. cellPoint = new Point (x, y); this. formaterString = formatStr; this. cellValue = val ;}}
Image cell format Processor/// <Summary> /// figure Cell formatter /// </summary> public class ImageCellFormater {// <summary> /// Cell location /// </ summary> public Point CellPoint {get; set ;}//< summary> /// display the Image /// </summary> public Image Img {get; set ;} /// <summary> /// instantiate /// </summary> public ImageCellFormater () {}/// <summary> /// instantiate /// </summary> /// <param name = "x"> cell abscissa </param> // <param name = "y"> cell ordinate </param> // <param name = "img"> image </param> public ImageCellFormater (int x, int y, Image img) {this. cellPoint = new Point (x, y); this. img = img ;}}
Detailed area cell format Processor/// <Summary> // detailed cell value rules // <typeparam name = "T"> data type </typeparam> /// </summary> public class DetailCellValueFormater <T> where T: class {// <summary> /// column Index /// </summary> public int Index {get; set ;} /// <summary> /// Value function // </summary> public Func <T, object> Value {get; set ;} /// <summary> /// instantiate /// </summary> public DetailCellValueFormater () {}/// <summary> /// instantiate /// </summary> /// <param name = "index"> column index </param> /// <param name = "val"> data </param> public DetailCellValueFormater (int index, func <T, object> val) {this. index = index; this. value = val ;}}
Next, let's take a look at the definition of the export format processing set./// <Summary> /// used to describe the exported formatted data /// </summary> public class ExportFormater {/// <summary> /// Replace the type of hash cells/ // </summary> public List <DispersedCellFormater> DispersedCellFormaters {get; private set ;}/// <summary> /// image cell formatting rules //</summary> public List <ImageCellFormater> ImageCellFormaters {get; private set ;} /// <summary> /// index of the starting row of detailed data /// </summary> public int DetailRowBeginIndex {get; set ;} /// <summary> /// instantiate /// </summary> public ExportFormater () {DispersedCellFormaters = new List <DispersedCellFormater> (); imageCellFormaters = new List <ImageCellFormater> () ;}/// <summary> // describe how to export formatted data, rule with data value in the detail area /// </summary> public class ExportFormater <T>: ExportFormater where T: class {// <summary> /// detailed area value function /// </summary> public List <DetailCellValueFormater <T> DetailCellValueFormaters {get; private set ;} /// <summary >/// instantiate /// </summary> public ExportFormater () {DetailCellValueFormaters = new List <DetailCellValueFormater <T> ();}}
Next, let's take a look at the specific export implementation.
Conclusion: