88
99using ClosedXML . Excel ;
1010using DocumentFormat . OpenXml . Spreadsheet ;
11+ using DocumentFormat . OpenXml . Wordprocessing ;
1112
1213namespace EasyData . Export
1314{
@@ -49,6 +50,12 @@ public IDataExportSettings GetDefaultSettings(CultureInfo culture = null)
4950 /// </summary>
5051 public IDataExportSettings DefaultSettings => ExcelDataExportSettings . Default ;
5152
53+ /// <summary>
54+ /// Gets the current export settings passed in the ExportAsync method.
55+ /// </summary>
56+ protected ExcelDataExportSettings ExportSettings { get ; private set ; }
57+
58+
5259 /// <summary>
5360 /// Exports the specified data to the stream.
5461 /// </summary>
@@ -95,12 +102,12 @@ public Task ExportAsync(IEasyDataResultSet data, Stream stream, CancellationToke
95102 /// <returns>Task.</returns>
96103 public async Task ExportAsync ( IEasyDataResultSet data , Stream stream , IDataExportSettings settings , CancellationToken ct = default )
97104 {
98- var mappedSettings = MapSettings ( settings ) ;
105+ ExportSettings = MapSettings ( settings ) ;
99106
100107 // predefined formatters
101108 var predefinedFormatters = GetPredefinedFormatters ( data . Cols , settings ) ;
102109
103- var sheetName = Utils . ToExcelSheetName ( mappedSettings . Title ) ;
110+ var sheetName = Utils . ToExcelSheetName ( ExportSettings . Title ) ;
104111
105112 var wb = new XLWorkbook ( ) ;
106113 var ws = wb . Worksheets . Add ( sheetName ) ;
@@ -111,13 +118,13 @@ public async Task ExportAsync(IEasyDataResultSet data, Stream stream, IDataExpor
111118 var cellNum = startColNum ;
112119
113120 var xlColID = GetExcelColumnId ( startColNum ) ;
114- if ( mappedSettings . ShowDatasetInfo ) {
115- if ( ! string . IsNullOrWhiteSpace ( mappedSettings . Title ) ) {
116- ws . Cell ( $ "{ xlColID } { cellNum } ") . Value = mappedSettings . Title ;
121+ if ( ExportSettings . ShowDatasetInfo ) {
122+ if ( ! string . IsNullOrWhiteSpace ( ExportSettings . Title ) ) {
123+ ws . Cell ( $ "{ xlColID } { cellNum } ") . Value = ExportSettings . Title ;
117124 cellNum ++ ;
118125 }
119- if ( ! string . IsNullOrWhiteSpace ( mappedSettings . Description ) ) {
120- ws . Cell ( $ "{ xlColID } { cellNum } ") . Value = mappedSettings . Description ;
126+ if ( ! string . IsNullOrWhiteSpace ( ExportSettings . Description ) ) {
127+ ws . Cell ( $ "{ xlColID } { cellNum } ") . Value = ExportSettings . Description ;
121128 cellNum ++ ;
122129 }
123130 }
@@ -157,7 +164,7 @@ Task WriteRowAsync(EasyDataRow row, bool isExtraRow = false,
157164 xlColID = GetExcelColumnId ( i + startColNum ) ;
158165
159166 var cell = ws . Cell ( $ "{ xlColID } { cellNum } ") ;
160-
167+
161168 object value ;
162169 if ( ! string . IsNullOrEmpty ( dfmt ) && predefinedFormatters . TryGetValue ( dfmt , out var provider ) ) {
163170 value = string . Format ( provider , dfmt , row [ i ] ) ;
@@ -182,38 +189,10 @@ Task WriteRowAsync(EasyDataRow row, bool isExtraRow = false,
182189 }
183190 }
184191
185- if ( ! ( value is DBNull ) ) {
186- switch ( excelDataType ) {
187- case XLDataType . DateTime :
188- cell . Value = value is DateTimeOffset
189- ? ( ( DateTimeOffset ) value ) . DateTime
190- : Convert . ToDateTime ( value ) ;
191- break ;
192- case XLDataType . Text :
193- if ( value != null ) {
194- var strValue = settings . PreserveFormatting && ! column . Style . AllowAutoFormatting
195- ? "'" + value
196- : value . ToString ( ) ;
197- cell . Value = ( XLCellValue ) strValue ;
198- }
199- else {
200- cell . Value = Blank . Value ;
201- }
202- break ;
203- case XLDataType . Number :
204- cell . Value = Convert . ToDouble ( value ) ;
205- break ;
206- default :
207- cell . Value = value . ToString ( ) ;
208- break ;
209- }
210- }
211- else {
212- cell . Value = Blank . Value ;
213- }
192+ WriteCellValue ( cell , value , excelDataType , column ) ;
214193
215194 // setting the cell's format
216- var cellFormat = GetCellFormat ( excelDataType , column . DataType , mappedSettings , dfmt ) ;
195+ var cellFormat = GetCellFormat ( excelDataType , column . DataType , ExportSettings , dfmt ) ;
217196 if ( ! string . IsNullOrEmpty ( cellFormat ) ) {
218197 if ( excelDataType == XLDataType . Number ) {
219198 cell . Style . NumberFormat . Format = cellFormat ;
@@ -245,31 +224,31 @@ Task WriteRowAsync(EasyDataRow row, bool isExtraRow = false,
245224 if ( settings . RowLimit > 0 && currentRowNum >= settings . RowLimit )
246225 continue ;
247226
248- if ( mappedSettings . BeforeRowInsert != null )
249- await mappedSettings . BeforeRowInsert ( row , WriteExtraRowAsync , ct ) . ConfigureAwait ( false ) ;
227+ if ( ExportSettings . BeforeRowInsert != null )
228+ await ExportSettings . BeforeRowInsert ( row , WriteExtraRowAsync , ct ) . ConfigureAwait ( false ) ;
250229
251230 await WriteRowAsync ( row , cancellationToken : ct ) . ConfigureAwait ( false ) ;
252231
253232 currentRowNum ++ ;
254233 }
255234
256- if ( mappedSettings . BeforeRowInsert != null )
257- await mappedSettings . BeforeRowInsert ( null , WriteExtraRowAsync , ct ) . ConfigureAwait ( false ) ;
235+ if ( ExportSettings . BeforeRowInsert != null )
236+ await ExportSettings . BeforeRowInsert ( null , WriteExtraRowAsync , ct ) . ConfigureAwait ( false ) ;
258237
259238 cellNum -- ;
260239
261240 // Setup styles
262241 var rngTable = ws . Range ( $ "{ startColId } { startColNum } :{ endColId } { cellNum } ") ;
263242 var rowNum = 1 ;
264- if ( mappedSettings . ShowDatasetInfo ) {
265- if ( ! string . IsNullOrWhiteSpace ( mappedSettings . Title ) ) {
243+ if ( ExportSettings . ShowDatasetInfo ) {
244+ if ( ! string . IsNullOrWhiteSpace ( ExportSettings . Title ) ) {
266245 rngTable . Cell ( rowNum , 1 ) . Style . Font . Bold = true ;
267246 rngTable . Cell ( rowNum , 1 ) . Style . Fill . BackgroundColor = XLColor . CornflowerBlue ;
268247 rngTable . Cell ( rowNum , 1 ) . Style . Alignment . Horizontal = XLAlignmentHorizontalValues . Center ;
269248 rngTable . Row ( rowNum ) . Merge ( ) ;
270249 rowNum ++ ;
271250 }
272- if ( ! string . IsNullOrWhiteSpace ( mappedSettings . Description ) ) {
251+ if ( ! string . IsNullOrWhiteSpace ( ExportSettings . Description ) ) {
273252 rngTable . Cell ( rowNum , 1 ) . Style . Font . Bold = true ;
274253 rngTable . Cell ( rowNum , 1 ) . Style . Fill . BackgroundColor = XLColor . White ;
275254 rngTable . Cell ( rowNum , 1 ) . Style . Alignment . Horizontal = XLAlignmentHorizontalValues . Center ;
@@ -297,6 +276,39 @@ Task WriteRowAsync(EasyDataRow row, bool isExtraRow = false,
297276 }
298277 }
299278
279+ protected virtual void WriteCellValue ( IXLCell cell , object value , XLDataType xlDataType , EasyDataCol column )
280+ {
281+ if ( ! ( value is DBNull ) ) {
282+ switch ( xlDataType ) {
283+ case XLDataType . DateTime :
284+ cell . Value = value is DateTimeOffset
285+ ? ( ( DateTimeOffset ) value ) . DateTime
286+ : Convert . ToDateTime ( value ) ;
287+ break ;
288+ case XLDataType . Text :
289+ if ( value != null ) {
290+ var strValue = ExportSettings . PreserveFormatting && ! column . Style . AllowAutoFormatting
291+ ? "'" + value
292+ : value . ToString ( ) ;
293+ cell . Value = strValue ;
294+ }
295+ else {
296+ cell . Value = Blank . Value ;
297+ }
298+ break ;
299+ case XLDataType . Number :
300+ cell . Value = Convert . ToDouble ( value ) ;
301+ break ;
302+ default :
303+ cell . Value = value . ToString ( ) ;
304+ break ;
305+ }
306+ }
307+ else {
308+ cell . Value = Blank . Value ;
309+ }
310+ }
311+
300312 private void AdjustColumnsSize ( IXLColumns columns )
301313 {
302314 if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ) {
0 commit comments