@@ -38,7 +38,7 @@ export class CoreFile implements IExcelFile {
3838 */
3939export class WorkbookRelsFile implements IExcelFile {
4040 public writeElement ( folder : JSZip , worksheetData : WorksheetData ) {
41- const hasSharedStrings = worksheetData . isEmpty === false ;
41+ const hasSharedStrings = ! worksheetData . isEmpty || worksheetData . options . alwaysExportHeaders ;
4242 folder . file ( 'workbook.xml.rels' , ExcelStrings . getWorkbookRels ( hasSharedStrings ) ) ;
4343 }
4444}
@@ -71,11 +71,11 @@ export class WorksheetFile implements IExcelFile {
7171 public async writeElementAsync ( folder : JSZip , worksheetData : WorksheetData ) {
7272 return new Promise < void > ( resolve => {
7373 this . prepareDataAsync ( worksheetData , ( cols , rows ) => {
74- const hasTable = ! worksheetData . isEmpty && worksheetData . options . exportAsTable ;
75- const isHierarchicalGrid = worksheetData . data [ 0 ] ?. type === ExportRecordType . HierarchicalGridRecord ;
74+ const hasTable = ( ! worksheetData . isEmpty || worksheetData . options . alwaysExportHeaders )
75+ && worksheetData . options . exportAsTable ;
7676
7777 folder . file ( 'sheet1.xml' , ExcelStrings . getSheetXML (
78- this . dimension , this . freezePane , cols , rows , hasTable , this . maxOutlineLevel , isHierarchicalGrid ) ) ;
78+ this . dimension , this . freezePane , cols , rows , hasTable , this . maxOutlineLevel , worksheetData . isHierarchical ) ) ;
7979 resolve ( ) ;
8080 } ) ;
8181 } ) ;
@@ -87,14 +87,15 @@ export class WorksheetFile implements IExcelFile {
8787 const dictionary = worksheetData . dataDictionary ;
8888 this . rowIndex = 0 ;
8989
90- if ( worksheetData . isEmpty ) {
90+ if ( worksheetData . isEmpty && ( ! worksheetData . options . alwaysExportHeaders || worksheetData . owner . columns . length === 0 ) ) {
9191 sheetData += '<sheetData/>' ;
9292 this . dimension = 'A1' ;
9393 done ( '' , sheetData ) ;
9494 } else {
9595 const owner = worksheetData . owner ;
96- const isHierarchicalGrid = worksheetData . data [ 0 ] . type === ExportRecordType . HierarchicalGridRecord ;
96+ const isHierarchicalGrid = worksheetData . isHierarchical ;
9797 const hasMultiColumnHeader = worksheetData . hasMultiColumnHeader ;
98+
9899 const hasUserSetIndex = owner . columns . some ( col => col . exportIndex !== undefined ) ;
99100
100101 const height = worksheetData . options . rowHeight ;
@@ -182,7 +183,7 @@ export class WorksheetFile implements IExcelFile {
182183 const indexOfLastPinnedColumn = worksheetData . indexOfLastPinnedColumn ;
183184 const frozenColumnCount = indexOfLastPinnedColumn + 1 ;
184185 let firstCell = ExcelStrings . getExcelColumn ( frozenColumnCount ) + freezeHeaders ;
185- if ( indexOfLastPinnedColumn !== - 1 &&
186+ if ( indexOfLastPinnedColumn !== undefined && indexOfLastPinnedColumn !== - 1 &&
186187 ! worksheetData . options . ignorePinning &&
187188 ! worksheetData . options . ignoreColumnsOrder ) {
188189 this . freezePane =
@@ -209,7 +210,7 @@ export class WorksheetFile implements IExcelFile {
209210 sheetData += rows ;
210211 sheetData += '</sheetData>' ;
211212
212- if ( hasMultiColumnHeader ) {
213+ if ( hasMultiColumnHeader && this . mergeCellsCounter > 0 ) {
213214 sheetData += `<mergeCells count="${ this . mergeCellsCounter } ">${ this . mergeCellStr } </mergeCells>` ;
214215 }
215216
@@ -223,40 +224,41 @@ export class WorksheetFile implements IExcelFile {
223224 const height = worksheetData . options . rowHeight ;
224225 this . rowHeight = height ? ' ht="' + height + '" customHeight="1"' : '' ;
225226
226- const isHierarchicalGrid =
227- worksheetData . data . some ( r => r . type === ExportRecordType . HeaderRecord || r . type === ExportRecordType . HierarchicalGridRecord ) ;
227+ const isHierarchicalGrid = worksheetData . isHierarchical ;
228228 const hasUserSetIndex = worksheetData . owner . columns . some ( c => c . exportIndex !== undefined ) ;
229229
230230 let recordHeaders = [ ] ;
231231
232232 yieldingLoop ( worksheetData . rowCount - 1 , 1000 ,
233233 ( i ) => {
234- if ( ! isHierarchicalGrid ) {
235- if ( hasUserSetIndex ) {
236- recordHeaders = worksheetData . rootKeys ;
234+ if ( ! worksheetData . isEmpty ) {
235+ if ( ! isHierarchicalGrid ) {
236+ if ( hasUserSetIndex ) {
237+ recordHeaders = worksheetData . rootKeys ;
238+ } else {
239+ recordHeaders = worksheetData . owner . columns
240+ . filter ( c => c . headerType !== HeaderType . MultiColumnHeader && ! c . skip )
241+ . sort ( ( a , b ) => a . startIndex - b . startIndex )
242+ . sort ( ( a , b ) => a . pinnedIndex - b . pinnedIndex )
243+ . map ( c => c . field ) ;
244+ }
237245 } else {
238- recordHeaders = worksheetData . owner . columns
239- . filter ( c => c . headerType !== HeaderType . MultiColumnHeader && ! c . skip )
240- . sort ( ( a , b ) => a . startIndex - b . startIndex )
241- . sort ( ( a , b ) => a . pinnedIndex - b . pinnedIndex )
242- . map ( c => c . field ) ;
243- }
244- } else {
245- const record = worksheetData . data [ i ] ;
246+ const record = worksheetData . data [ i ] ;
246247
247- if ( record . type === ExportRecordType . HeaderRecord ) {
248- const recordOwner = worksheetData . owners . get ( record . owner ) ;
249- const hasMultiColumnHeaders = recordOwner . columns . some ( c => ! c . skip && c . headerType === HeaderType . MultiColumnHeader ) ;
248+ if ( record . type === ExportRecordType . HeaderRecord ) {
249+ const recordOwner = worksheetData . owners . get ( record . owner ) ;
250+ const hasMultiColumnHeaders = recordOwner . columns . some ( c => ! c . skip && c . headerType === HeaderType . MultiColumnHeader ) ;
250251
251- if ( hasMultiColumnHeaders ) {
252- this . hGridPrintMultiColHeaders ( worksheetData , rowDataArr , record , recordOwner ) ;
252+ if ( hasMultiColumnHeaders ) {
253+ this . hGridPrintMultiColHeaders ( worksheetData , rowDataArr , record , recordOwner ) ;
254+ }
253255 }
256+
257+ recordHeaders = Object . keys ( worksheetData . data [ i ] . data ) ;
254258 }
255259
256- recordHeaders = Object . keys ( worksheetData . data [ i ] . data ) ;
260+ rowDataArr . push ( this . processRow ( worksheetData , i , recordHeaders , isHierarchicalGrid ) ) ;
257261 }
258-
259- rowDataArr . push ( this . processRow ( worksheetData , i , recordHeaders , isHierarchicalGrid ) ) ;
260262 } ,
261263 ( ) => {
262264 done ( rowDataArr . join ( '' ) ) ;
@@ -404,7 +406,8 @@ export class WorkbookFile implements IExcelFile {
404406 */
405407export class ContentTypesFile implements IExcelFile {
406408 public writeElement ( folder : JSZip , worksheetData : WorksheetData ) {
407- folder . file ( '[Content_Types].xml' , ExcelStrings . getContentTypesXML ( ! worksheetData . isEmpty , worksheetData . options . exportAsTable ) ) ;
409+ const hasSharedStrings = ! worksheetData . isEmpty || worksheetData . options . alwaysExportHeaders ;
410+ folder . file ( '[Content_Types].xml' , ExcelStrings . getContentTypesXML ( hasSharedStrings , worksheetData . options . exportAsTable ) ) ;
408411 }
409412}
410413
@@ -436,7 +439,10 @@ export class TablesFile implements IExcelFile {
436439 public writeElement ( folder : JSZip , worksheetData : WorksheetData ) {
437440 const columnCount = worksheetData . columnCount ;
438441 const lastColumn = ExcelStrings . getExcelColumn ( columnCount - 1 ) + worksheetData . rowCount ;
439- const dimension = 'A1:' + lastColumn ;
442+ const autoFilterDimension = 'A1:' + lastColumn ;
443+ const tableDimension = worksheetData . isEmpty
444+ ? 'A1:' + ExcelStrings . getExcelColumn ( columnCount - 1 ) + ( worksheetData . rowCount + 1 )
445+ : autoFilterDimension ;
440446 const hasUserSetIndex = worksheetData . owner . columns . some ( c => c . exportIndex !== undefined ) ;
441447 const values = hasUserSetIndex
442448 ? worksheetData . rootKeys
@@ -463,7 +469,7 @@ export class TablesFile implements IExcelFile {
463469 sortString = `<sortState ref="A2:${ lastColumn } "><sortCondition descending="${ dir } " ref="${ sc } 1:${ sc } 15"/></sortState>` ;
464470 }
465471
466- folder . file ( 'table1.xml' , ExcelStrings . getTablesXML ( dimension , tableColumns , sortString ) ) ;
472+ folder . file ( 'table1.xml' , ExcelStrings . getTablesXML ( autoFilterDimension , tableDimension , tableColumns , sortString ) ) ;
467473 }
468474}
469475
0 commit comments