无法打开文件“ file name.xlsx"因为此文件的格式或扩展名无效 [英] Cannot open file " file name.xlsx " because the format or extension of this file is invalid

查看:125
本文介绍了无法打开文件“ file name.xlsx"因为此文件的格式或扩展名无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,我使用的是form1,其中gridview和form2包含文本框。我的代码运行正常并将文件保存到我选择的目录中。问题是当我尝试打开excel文件时,我收到来自Excel的错误,指出Excel无法打开文件Invoice System.xlsx,因为文件格式或文件扩展名无效。验证文件是否已损坏,文件扩展名是否与文件格式相符



我尝试过:



form1:

使用(var fs = new FileStream(Invoice System.xlsx,FileMode.Create,FileAccess.Write)){wb。写(FS); } $ / $
form2:

private void button1_Click(object sender,EventArgs e){string [] rowData = new string [] {textBox1.Text,textBox2.Text}; form1.dataGridView1.Rows.Add(rowData); }

I have a project where I am using form1 having gridview and form2 containing textboxes. My code runs fine and saves the file to the directory that I choose. The problem is when I try to open the excel file I receive an error from Excel stating "Excel cannot open the file Invoice System.xlsx because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file

What I have tried:

form1:
using (var fs = new FileStream("Invoice System.xlsx", FileMode.Create, FileAccess.Write)) { wb.Write(fs); }
form2:
private void button1_Click(object sender, EventArgs e) { string[] rowData = new string[] { textBox1.Text, textBox2.Text}; form1.dataGridView1.Rows.Add(rowData); }

推荐答案

using (var fs = new FileStream("Invoice System.xlsx", FileMode.Create, FileAccess.Write)) { wb.Write(fs); } 





这个 不会 写适当格式的数据到Excel文件中。



您需要使用以下其中一种:

1. OpenXml [ ^ ]: Open XML从头创建Excel | Wriju的博客 [ ^ ]

2. Excel.Interop [ ^ ]: Workbooks.Add方法(Microsoft.Office.Interop.Excel) [ ^ ]

3. EPPlus [ ^ ]:如何创建Excel文件(开发) )使用EPPlus .NET库(C#) - 第一部分



This won't write data in proper format into Excel file.

You need to use one of below:
1. OpenXml[^]: Open XML Create Excel from scratch | Wriju's BLOG[^]
2. Excel.Interop[^]: Workbooks.Add method (Microsoft.Office.Interop.Excel)[^]
3. EPPlus[^]: How To Create An Excel File (Development) Using EPPlus .NET Library (C#) - Part One


Quote:

问题是,当我尝试打开excel文件时,我收到来自Excel的错误,指出Excel无法打开文件Invoice System.xlsx,因为文件格式或文件扩展名为n有效。验证文件是否已损坏且文件扩展名是否与文件格式匹配

The problem is when I try to open the excel file I receive an error from Excel stating "Excel cannot open the file Invoice System.xlsx because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file



Excel会告诉您文件不是正确的Excel数据文件。

简单地命名文件Invoice System.xlsx,不足以使其成为Excel数据文件或Excel可以读取的文件。

Excel可以读取大量数据文件:csv,xml ,HTML等。你必须研究每种方法可以做些什么,并选择适合你需要的女巫。


Excel tells you that your file is not a proper Excel data file.
Simply naming a file "Invoice System.xlsx", is not enough to make it an Excel data file or a file that Excel can read.
Excel can read numerous data files: csv, xml, html and more. you have to study what can be done with each and choose witch one fit your needs.


这篇关于无法打开文件“ file name.xlsx"因为此文件的格式或扩展名无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆