vb.net - Can't open Excel spreadsheet after datagridview export from vb .net -


i have following code takes datagridview , exports content excel. working fine. however, have new spreadsheet launch after done. receive following error when gets line open excel:

excel cannot open file 'exported.xlsx' because file format or file extension not valid. verify file has not been corrupted , file extension matches format of file.

here code:

 private sub button4_click_1(sender system.object, e system.eventargs) handles btlxlsexport.click     dim fs new io.streamwriter("c:\exported.xlsx", false)     fs.writeline("<?xml version=""1.0""?>")     fs.writeline("<?mso-application progid=""excel.sheet""?>")     fs.writeline("<workbook xmlns=""urn:schemas-microsoft-com:office:spreadsheet xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet"">")     fs.writeline("    <ss:styles>")     fs.writeline("        <ss:style ss:id=""1"">")     fs.writeline("           <ss:font ss:bold=""1""/>")     fs.writeline("        </ss:style>")     fs.writeline("    </ss:styles>")     fs.writeline("    <worksheet ss:name=""sheet1"">")     fs.writeline("        <ss:table>")     x integer = 0 dgcustomers.columns.count - 1         fs.writeline("            <ss:column ss:width=""{0}""/>",                      dgcustomers.columns.item(x).width)     next     fs.writeline("            <ss:row ss:styleid=""1"">")     integer = 0 dgcustomers.columns.count - 1         fs.writeline("                <ss:cell>")         fs.writeline(string.format(                      "                   <ss:data ss:type=""string"">{0}</ss:data>",                                   dgcustomers.columns.item(i).headertext))         fs.writeline("                </ss:cell>")     next     fs.writeline("            </ss:row>")     introw integer = 0 dgcustomers.rowcount - 2         fs.writeline(string.format("            <ss:row ss:height =""{0}"">",                                   dgcustomers.rows(introw).height))         intcol integer = 0 dgcustomers.columns.count - 1             fs.writeline("                <ss:cell>")             fs.writeline(string.format(                          "                   <ss:data ss:type=""string"">{0}</ss:data>",                                        dgcustomers.item(intcol, introw).value.tostring))             fs.writeline("                </ss:cell>")         next         fs.writeline("            </ss:row>")     next     fs.writeline("        </ss:table>")     fs.writeline("    </ss:worksheet>")     fs.writeline("</ss:workbook>")     fs.close()      dim wapp microsoft.office.interop.excel.application      dim wsheet microsoft.office.interop.excel.worksheet      dim wbook microsoft.office.interop.excel.workbook      ' dim wrange microsoft.office.interop.excel.range      ' dim chartrange microsoft.office.interop.excel.range        wapp = new microsoft.office.interop.excel.application      wbook = wapp.workbooks.open("c:\exported.xlsx")     wsheet = wbook.worksheets(1)   end sub 

thanks help!


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -