excel - "File Format Is Not Valid" error while trying to export xlsx file from data table -
excel gives me error: excel cannot open file .xlsx because file format or file extension not valid
.
here code:
response.appendheader("content-type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") response.appendheader("content-disposition", "attachment; filename=excel.xlsx") response.contentencoding = encoding.unicode response.binarywrite(encoding.unicode.getpreamble) response.write(x.tostring) response.end()
i believe should this.
private sub datatabletoexcel(byval dttemp datatable) dim _excel new microsoft.office.interop.excel.application dim wbook microsoft.office.interop.excel.workbook dim wsheet microsoft.office.interop.excel.worksheet wbook = _excel.workbooks.add() wsheet = wbook.activesheet() dim dt system.data.datatable = dttemp dim dc system.data.datacolumn dim dr system.data.datarow dim colindex integer = 0 dim rowindex integer = 0 each dc in dt.columns colindex = colindex + 1 _excel.cells(1, colindex) = dc.columnname next each dr in dt.rows rowindex = rowindex + 1 colindex = 0 each dc in dt.columns colindex = colindex + 1 _excel.cells(rowindex + 1, colindex) = dr(dc.columnname) next next wsheet.columns.autofit() dim strfilename string = "c:\datatable.xlsx" if system.io.file.exists(strfilename) system.io.file.delete(strfilename) end if wbook.saveas(strfilename) wbook.close() _excel.quit() end sub
http://www.authorcode.com/export-datatable-to-excel-in-vb-net/
Comments
Post a Comment