1

I have this code to generate an Excel file and download it:

SpreadsheetInfo.SetLicense("mycode"); 
ExcelFile myExcelFile = new ExcelFile();
ExcelWorksheet ws = myExcelFile.Worksheets.Add("Page 1");
myExcelFile.Save(Response, "asd.xlsx"); //this is for download

When I download the Excel file, it says:

Either file type or file extension is not valid so Excel cannot open this file. Confirm that file is not broken or file extension matches file type.

I'm trying to solve this for hours but I couldn't find a solution. I use same code other places and it works, but it doesn't work at this part of my project. Can you tell me what I should do? Thanks.

jason
  • 6,962
  • 36
  • 117
  • 198
  • You should submit a support ticket to GemBox Software by using [this link](http://www.gemboxsoftware.com/support-center/new-ticket) and attach the corrupted file so that they can investigate the file. – Mario Z Aug 01 '16 at 10:36

4 Answers4

2

Try to save the fire as an xls-file instead of an xlsx-file. I'm guessing that the default is xls. Excel can get confused if the file extension is wrong.

Rune Grimstad
  • 35,612
  • 10
  • 61
  • 76
0

I had the same issue and it was caused by the worksheet title. The worksheet title should not be too long. I think the limit is 32 characters.

Mahdi
  • 3,199
  • 2
  • 25
  • 35
0

HYE. TRY THIS. I'VE TRY SOME EXAMPLES FROM GEMBOX SPREADSHEET WEBSITE.

Imports GemBox.Spreadsheet
Imports GemBox.Spreadsheet.WinFormsUtilities
Imports System.Data.OleDb

Public Class Form1

Public Sub New()
    SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

    InitializeComponent()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim openFileDialog = New OpenFileDialog()
    openFileDialog.Filter = "XLS files (*.xls, *.xlt)|*.xls;*.xlt|XLSX files (*.xlsx, *.xlsm, *.xltx, *.xltm)|*.xlsx;*.xlsm;*.xltx;*.xltm|ODS files (*.ods, *.ots)|*.ods;*.ots|CSV files (*.csv, *.tsv)|*.csv;*.tsv|HTML files (*.html, *.htm)|*.html;*.htm"
    openFileDialog.FilterIndex = 2

    If (openFileDialog.ShowDialog() = DialogResult.OK) Then
        Dim ef = ExcelFile.Load(openFileDialog.FileName)

        ' Export Excel worksheet to DataGridView control.
        DataGridViewConverter.ExportToDataGridView(ef.Worksheets.ActiveWorksheet, Me.DataGridView1, New ExportToDataGridViewOptions() With {.ColumnHeaders = True})
    End If



End Sub

THAT OPEN FILE DIALOG, YOU CAN MINIMIZE TO READ EXCEL FILE ONLY

KEROL
  • 1
  • 3
  • YOU HAVE TO CLICK ADD REFERENCE AT YOUR PROJECT AND CHOOSE GEMBOX SPREADSHEET & GEMBOX WIN UTILITIES. MAKESURE IT HAS THE SAME VERSION OR ELSE IT WILL CORRUPT – KEROL May 12 '17 at 03:23
0

If you save the file to the folder and it works fine then check if your http response is modified before returned to the user. https://learn.microsoft.com/en-us/dotnet/api/system.web.httpresponse.filter?view=netframework-4.8.1 If yes, add logic to exclude response modification by content type or other properties that suits your logic.

IHAFURR
  • 111
  • 4