import zipfile, openpyxl, win32com.client

content_types = '''<?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"/>
<Default Extension="xml" ContentType="application/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/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>
</Types>'''

rels = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
</Relationships>'''

wb_rels = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
</Relationships>'''

workbook = '''<?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">
<sheets>
<sheet name="Hoja1" sheetId="1" r:id="rId1"/>
</sheets>
</workbook>'''

styles = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<numFmts count="1">
<numFmt numFmtId="164" formatCode="#,##0.00"/>
</numFmts>
<fonts count="2">
<font><name val="Segoe UI"/><sz val="10"/></font>
<font><b/><name val="Segoe UI"/><sz val="10"/></font>
</fonts>
<fills count="2">
<fill><patternFill patternType="none"/></fill>
<fill><patternFill patternType="gray125"/></fill>
</fills>
<borders count="1">
<border><left/><right/><top/><bottom/><diagonal/></border>
</borders>
<cellStyleXfs count="1">
<xf numFmtId="0" fontId="0" fillId="0" borderId="0"/>
</cellStyleXfs>
<cellXfs count="3">
<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>
<xf numFmtId="0" fontId="1" fillId="0" borderId="0" xfId="0" applyFont="1"/>
<xf numFmtId="164" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/>
</cellXfs>
</styleSheet>'''

sheet1 = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<sheetData>
<row r="1">
<c r="A1" t="inlineStr" s="1"><is><t>Número</t></is></c>
<c r="B1" t="inlineStr" s="1"><is><t>Fecha</t></is></c>
<c r="C1" t="inlineStr" s="1"><is><t>Importe</t></is></c>
</row>
<row r="2">
<c r="A2"><v>600</v></c>
<c r="B2" t="inlineStr"><is><t>28/10/2026</t></is></c>
<c r="C2" s="2"><v>471.30</v></c>
</row>
</sheetData>
</worksheet>'''

out_path = r'c:\fuentes\proyectos delphi\RAUL_ASENCIO\GESTION\scratch\test_native.xlsx'
with zipfile.ZipFile(out_path, 'w', zipfile.ZIP_DEFLATED) as z:
    z.writestr('[Content_Types].xml', content_types.strip())
    z.writestr('_rels/.rels', rels.strip())
    z.writestr('xl/_rels/workbook.xml.rels', wb_rels.strip())
    z.writestr('xl/workbook.xml', workbook.strip())
    z.writestr('xl/styles.xml', styles.strip())
    z.writestr('xl/worksheets/sheet1.xml', sheet1.strip())

print('ZIP created!')

wb = openpyxl.load_workbook(out_path)
print('Openpyxl sheets:', wb.sheetnames)
ws = wb.active
print('Row 1:', [c.value for c in ws[1]])
print('Row 2:', [c.value for c in ws[2]])

excel = win32com.client.Dispatch('Excel.Application')
excel.DisplayAlerts = False
ewb = excel.Workbooks.Open(out_path)
ews = ewb.Sheets('Hoja1')
print('Excel COM cell C2:', ews.Range('C2').Value)
ewb.Close(False)
excel.Quit()
print('Validation SUCCESS!')
