简体   繁体   English

Excel无法打开Gembox电子表格文件

[英]Excel cannot open Gembox Spreadsheet file

I have this code to generate an Excel file and download it: 我有以下代码来生成Excel文件并下载:

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: 当我下载Excel文件时,它说:

Either file type or file extension is not valid so Excel cannot open this file. 文件类型或文件扩展名无效,因此Excel无法打开该文件。 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. 谢谢。

Try to save the fire as an xls-file instead of an xlsx-file. 尝试将火另存为xls文件而不是xlsx文件。 I'm guessing that the default is xls. 我猜默认是xls。 Excel can get confused if the file extension is wrong. 如果文件扩展名错误,Excel可能会感到困惑。

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. 我认为上限为32个字符。

HYE. HYE。 TRY THIS. 尝试这个。 I'VE TRY SOME EXAMPLES FROM GEMBOX SPREADSHEET WEBSITE. 我已经从GEMBOX电子表格网站上尝试了一些示例。

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

Public Class Form1 公开课表格1

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 通过打开文件对话框,您可以最小化仅读取EXCEL文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM