示例#1
0
        /// <summary>
        /// Creates a worksheet on excel file.
        /// </summary>
        /// <param name="worksheetName">The name of the worksheet.</param>
        /// <returns>Returns the created worksheet.</returns>
        public WorksheetAdapter CreateWorkSheet(string worksheetName)
        {
            var excelWorksheet         = this._excelPackage.Workbook.Worksheets.Add(worksheetName);
            WorksheetAdapter workSheet = new WorksheetAdapter(excelWorksheet);

            return(workSheet);
        }
示例#2
0
        /// <summary>
        /// Create a sheet with a list of values. The list contains a T types(for example int, string fields) to be added to the sheet.
        /// </summary>
        /// <typeparam name="T">A Type which includes fields.</typeparam>
        /// <param name="sheetName">The name of the sheet.</param>
        /// <param name="list">The values in the list are inserted into the given sheet.</param>
        /// <exception cref="System.Exception">Throws when the path string for creating or loading the excel file is not specified in Init Method. To avoid this exception, before using any methods, calls the Init method with the suitable path parameter.</exception>
        public void CreateExcel <T>(string sheetName, IList <T> list)
        {
            if (this._excelAdapter == null)
            {
                throw new Exception("The path is not specified. Please call Init method.");
            }
            Dictionary <string, string> properties = this.GetProperties <T>();
            var propertyList           = properties.ToList();
            WorksheetAdapter worksheet = this._excelAdapter.GetWorksheet(sheetName);

            if (worksheet == null)
            {
                worksheet = this._excelAdapter.CreateWorkSheet(sheetName);
            }
            for (int i = 0; i < propertyList.Count; i++)
            {
                worksheet.SetCellValue(1, i + 1, propertyList[i].Key);
            }
            for (int i = 0; i < list.Count; i++)
            {
                T row = list[i];
                for (int j = 0; j < propertyList.Count; j++)
                {
                    string fieldName = propertyList[j].Value;
                    worksheet.SetCellValue(i + 2, j + 1, GetValue <T>(row, fieldName));
                }
            }
        }
示例#3
0
        /// <summary>
        /// Gets the specified worksheet.
        /// </summary>
        /// <param name="worksheetName">The name of the worksheet.</param>
        /// <returns>Returns the requested worksheet.</returns>
        public WorksheetAdapter GetWorksheet(string worksheetName)
        {
            var excelWorksheet = this._excelPackage.Workbook.Worksheets.FirstOrDefault(o => o.Name == worksheetName);

            if (excelWorksheet == null)
            {
                return(null);
            }
            WorksheetAdapter workSheet = new WorksheetAdapter(excelWorksheet);

            return(workSheet);
        }