diff --git a/internal/app/collection_find_export.go b/internal/app/collection_find_export.go index bb7064b..6fca235 100644 --- a/internal/app/collection_find_export.go +++ b/internal/app/collection_find_export.go @@ -6,7 +6,6 @@ import ( "encoding/csv" "encoding/json" "fmt" - "io" "os" "strings" @@ -43,6 +42,12 @@ var ( excelThemeXml string //go:embed collection_find_export_excel/contenttypes.xml excelContentTypesXml string + //go:embed collection_find_export_excel/metadata.xml + excelMetadataXml string + //go:embed collection_find_export_excel/workbook.xml + excelWorkbookXml string + //go:embed collection_find_export_excel/dotrels.xml + excelDotRelsXml string alphabet = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ") ) @@ -260,8 +265,7 @@ func (a *App) PerformFindExport(hostKey, dbKey, collKey, settingsJson string) bo var csvWriter *csv.Writer var excelZipWriter *zip.Writer - var excelSheetWriter io.Writer - var excelStrings = make([]string, 0) + var excelSheetWriter strings.Builder switch settings.Format { case ExportFormatJsonArray: @@ -274,12 +278,15 @@ func (a *App) PerformFindExport(hostKey, dbKey, collKey, settingsJson string) bo excelZipWriter = zip.NewWriter(file) files := map[string]string{ - "docProps/app.xml": excelAppXml, - "docProps/core.xml": strings.Replace(excelCoreXml, "{TITLE}", fmt.Sprintf("%s.%s", dbKey, collKey), 1), - "xl/theme/theme1.xml": excelThemeXml, - "xl/rels/workbook.xml.rels": excelRelsXml, - "xl/styles.xml": excelStylesXml, - "[Content-Types].xml": excelContentTypesXml, + "_rels/.rels": excelDotRelsXml, + "docProps/app.xml": excelAppXml, + "docProps/core.xml": strings.Replace(excelCoreXml, "{TITLE}", fmt.Sprintf("%s.%s", dbKey, collKey), 1), + "xl/_rels/workbook.xml.rels": excelRelsXml, + "xl/theme/theme1.xml": excelThemeXml, + "xl/metadata.xml": excelMetadataXml, + "xl/styles.xml": excelStylesXml, + "xl/workbook.xml": excelWorkbookXml, + "[Content_Types].xml": excelContentTypesXml, } for fname, body := range files { @@ -306,36 +313,7 @@ func (a *App) PerformFindExport(hostKey, dbKey, collKey, settingsJson string) bo } } - excelZipWriter.Create("_rels/") - - excelSheetWriter, err = excelZipWriter.Create("xl/worksheets/sheet1.xml") - if err != nil { - runtime.LogErrorf(a.ctx, "Export: Excel ZIP error creating worksheet: %s", err.Error()) - runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ - Title: "ZIP error!", - Message: err.Error(), - Type: runtime.ErrorDialog, - }) - return false - } - - excelSheetWriter.Write([]byte(` - - - - - - - - - `)) + excelSheetWriter = strings.Builder{} } for cur.Next(ctx) { @@ -388,9 +366,7 @@ func (a *App) PerformFindExport(hostKey, dbKey, collKey, settingsJson string) bo case ExportFormatExcel: excelSheetWriter.Write([]byte(fmt.Sprintf(``, len(columnKeys)))) for idx, key := range columnKeys { - // excelStringsWriter.Write([]byte(fmt.Sprintf("%s", key))) - excelStrings = append(excelStrings, key) - excelSheetWriter.Write([]byte(fmt.Sprintf(`%d`, excelColIndex(idx+1), len(excelStrings)))) + excelSheetWriter.Write([]byte(fmt.Sprintf(`%s`, excelColIndex(idx+1), key))) } excelSheetWriter.Write([]byte("")) } @@ -485,9 +461,7 @@ func (a *App) PerformFindExport(hostKey, dbKey, collKey, settingsJson string) bo excelSheetWriter.Write([]byte(fmt.Sprintf(``, index+2, len(columnKeys)))) for idx, val := range excelRow { - // excelStringsWriter.Write([]byte(fmt.Sprintf("%s", val))) - excelStrings = append(excelStrings, val) - excelSheetWriter.Write([]byte(fmt.Sprintf(`%d`, excelColIndex(idx+1), index+2, len(excelStrings)))) + excelSheetWriter.Write([]byte(fmt.Sprintf(`%s`, excelColIndex(idx+1), index+2, val))) } excelSheetWriter.Write([]byte("")) } @@ -500,11 +474,9 @@ func (a *App) PerformFindExport(hostKey, dbKey, collKey, settingsJson string) bo file.WriteString("]\n") case ExportFormatExcel: - excelSheetWriter.Write([]byte(``)) - - excelStringsWriter, err := excelZipWriter.Create("xl/sharedStrings.xml") + sw, err := excelZipWriter.Create("xl/worksheets/sheet1.xml") if err != nil { - runtime.LogErrorf(a.ctx, "Export: Excel ZIP error creating shared strings: %s", err.Error()) + runtime.LogErrorf(a.ctx, "Export: Excel ZIP error creating worksheet: %s", err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Title: "ZIP error!", Message: err.Error(), @@ -513,13 +485,26 @@ func (a *App) PerformFindExport(hostKey, dbKey, collKey, settingsJson string) bo return false } - excelStringsWriter.Write([]byte(fmt.Sprintf(`%s`, len(excelStrings), len(excelStrings), "\r\n"))) - for _, str := range excelStrings { - excelStringsWriter.Write([]byte(fmt.Sprintf("%s\r\n", str))) - } - excelStringsWriter.Write([]byte("")) + sw.Write([]byte(strings.ReplaceAll(fmt.Sprintf(` + + + + + + + `, excelColIndex(len(columnKeys)), index+1), "\n", "\r\n"))) + sw.Write([]byte(excelSheetWriter.String())) + sw.Write([]byte(``)) - if err := excelZipWriter.Close(); err != nil { + err = excelZipWriter.Close() + if err != nil { runtime.LogErrorf(a.ctx, "Export: Excel ZIP error while closing: %s", err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Title: "ZIP error!", diff --git a/internal/app/collection_find_export_excel/contenttypes.xml b/internal/app/collection_find_export_excel/contenttypes.xml index 2312f21..8a41e3f 100644 --- a/internal/app/collection_find_export_excel/contenttypes.xml +++ b/internal/app/collection_find_export_excel/contenttypes.xml @@ -1,12 +1,29 @@ - - + + + + + + + + + + + + + + + - + diff --git a/internal/app/collection_find_export_excel/dotrels.xml b/internal/app/collection_find_export_excel/dotrels.xml new file mode 100644 index 0000000..a719722 --- /dev/null +++ b/internal/app/collection_find_export_excel/dotrels.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/internal/app/collection_find_export_excel/metadata.xml b/internal/app/collection_find_export_excel/metadata.xml new file mode 100644 index 0000000..99f5b57 --- /dev/null +++ b/internal/app/collection_find_export_excel/metadata.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/internal/app/collection_find_export_excel/template.xlsx b/internal/app/collection_find_export_excel/template.xlsx new file mode 100644 index 0000000..0a4518f Binary files /dev/null and b/internal/app/collection_find_export_excel/template.xlsx differ diff --git a/internal/app/collection_find_export_excel/workbook.xml b/internal/app/collection_find_export_excel/workbook.xml new file mode 100644 index 0000000..6b4c93a --- /dev/null +++ b/internal/app/collection_find_export_excel/workbook.xml @@ -0,0 +1,10 @@ + + + + + + +