1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-01-18 13:07:58 +00:00

Fixed export to Excel feature, it works now

This commit is contained in:
Romein van Buren 2023-06-25 22:00:53 +02:00
parent 3b4be3ebf6
commit cae29d9a71
Signed by: romein
GPG Key ID: 0EFF8478ADDF6C49
6 changed files with 96 additions and 58 deletions

View File

@ -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(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet
xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac xr xr2 xr3"
xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac"
xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision"
xmlns:xr2="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2"
xmlns:xr3="http://schemas.microsoft.com/office/spreadsheetml/2016/revision3" xr:uid="{17671867-E8D5-A14C-B382-03A6AA54A004}">
<dimension ref="A1:C5"/>
<sheetViews>
<sheetView tabSelected="1" workbookViewId="0">
<selection activeCell="A1" sqref="A1"/>
</sheetView>
</sheetViews>
<sheetFormatPr baseColWidth="10" defaultRowHeight="16" x14ac:dyDescent="0.2"/>
<sheetData>`))
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(`<row r="1" spans="1:%d" s="1" customFormat="1" x14ac:dyDescent="0.2">`, len(columnKeys))))
for idx, key := range columnKeys {
// excelStringsWriter.Write([]byte(fmt.Sprintf("<si><t>%s</t></si>", key)))
excelStrings = append(excelStrings, key)
excelSheetWriter.Write([]byte(fmt.Sprintf(`<c r="%s1" s="1" t="s"><v>%d</v></c>`, excelColIndex(idx+1), len(excelStrings))))
excelSheetWriter.Write([]byte(fmt.Sprintf(`<c r="%s1" s="1" t="str"><v>%s</v></c>`, excelColIndex(idx+1), key)))
}
excelSheetWriter.Write([]byte("</row>"))
}
@ -485,9 +461,7 @@ func (a *App) PerformFindExport(hostKey, dbKey, collKey, settingsJson string) bo
excelSheetWriter.Write([]byte(fmt.Sprintf(`<row r="%d" spans="1:%d" s="1" x14ac:dyDescent="0.2">`, index+2, len(columnKeys))))
for idx, val := range excelRow {
// excelStringsWriter.Write([]byte(fmt.Sprintf("<si><t>%s</t></si>", val)))
excelStrings = append(excelStrings, val)
excelSheetWriter.Write([]byte(fmt.Sprintf(`<c r="%s%d" t="s"><v>%d</v></c>`, excelColIndex(idx+1), index+2, len(excelStrings))))
excelSheetWriter.Write([]byte(fmt.Sprintf(`<c r="%s%d" t="str"><v>%s</v></c>`, excelColIndex(idx+1), index+2, val)))
}
excelSheetWriter.Write([]byte("</row>"))
}
@ -500,11 +474,9 @@ func (a *App) PerformFindExport(hostKey, dbKey, collKey, settingsJson string) bo
file.WriteString("]\n")
case ExportFormatExcel:
excelSheetWriter.Write([]byte(`</sheetData><pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/></worksheet>`))
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(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="%d" uniqueCount="%d">%s`, len(excelStrings), len(excelStrings), "\r\n")))
for _, str := range excelStrings {
excelStringsWriter.Write([]byte(fmt.Sprintf("<si><t>%s</t></si>\r\n", str)))
}
excelStringsWriter.Write([]byte("</sst>"))
sw.Write([]byte(strings.ReplaceAll(fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet
xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac xr xr2 xr3"
xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac"
xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision"
xmlns:xr2="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2"
xmlns:xr3="http://schemas.microsoft.com/office/spreadsheetml/2016/revision3" xr:uid="{17671867-E8D5-A14C-B382-03A6AA54A004}">
<dimension ref="A1:%s%d"/>
<sheetViews>
<sheetView workbookViewId="0"/>
</sheetViews>
<sheetFormatPr baseColWidth="25" defaultRowHeight="16" x14ac:dyDescent="0.2"/>
<sheetData>`, excelColIndex(len(columnKeys)), index+1), "\n", "\r\n")))
sw.Write([]byte(excelSheetWriter.String()))
sw.Write([]byte(`</sheetData><pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/></worksheet>`))
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!",

View File

@ -1,12 +1,29 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
<Types
xmlns="http://schemas.openxmlformats.org/package/2006/content-types"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
<Default Extension="xml" ContentType="application/xml"/>
<Default Extension="bin" ContentType="application/vnd.ms-excel.sheet.binary.macroEnabled.main"/>
<Default Extension="vml" ContentType="application/vnd.openxmlformats-officedocument.vmlDrawing"/>
<Default Extension="data" ContentType="application/vnd.openxmlformats-officedocument.model+data"/>
<Default Extension="bmp" ContentType="image/bmp"/>
<Default Extension="png" ContentType="image/png"/>
<Default Extension="gif" ContentType="image/gif"/>
<Default Extension="emf" ContentType="image/x-emf"/>
<Default Extension="wmf" ContentType="image/x-wmf"/>
<Default Extension="jpg" ContentType="image/jpeg"/>
<Default Extension="jpeg" ContentType="image/jpeg"/>
<Default Extension="tif" ContentType="image/tiff"/>
<Default Extension="tiff" ContentType="image/tiff"/>
<Default Extension="pdf" ContentType="application/pdf"/>
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>
<Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>
<Override PartName="/xl/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/>
<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>
<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>
<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>
<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>
<Override PartName="/xl/metadata.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheetMetadata+xml"/>
</Types>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
</Relationships>

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<metadata xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:xlrd="http://schemas.microsoft.com/office/spreadsheetml/2017/richdata" xmlns:xda="http://schemas.microsoft.com/office/spreadsheetml/2017/dynamicarray">
<metadataTypes count="1">
<metadataType name="XLDAPR" minSupportedVersion="120000" copy="1" pasteAll="1" pasteValues="1" merge="1" splitFirst="1" rowColShift="1" clearFormats="1" clearComments="1" assign="1" coerce="1" cellMeta="1"/>
</metadataTypes>
<futureMetadata name="XLDAPR" count="1">
<bk>
<extLst>
<ext uri="{bdbb8cdc-fa1e-496e-a857-3c3f30c029c3}">
<xda:dynamicArrayProperties fDynamic="1" fCollapsed="0"/>
</ext>
</extLst>
</bk>
</futureMetadata>
<cellMetadata count="1">
<bk>
<rc t="1" v="0"/>
</bk>
</cellMetadata>
</metadata>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook
xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
>
<workbookPr codeName="ThisWorkbook"/>
<sheets>
<sheet name="RolensExport" sheetId="1" r:id="rId1"/>
</sheets>
</workbook>