使用C#创建excel文件时无法打开它 [英] When create excel file using C# cannot open it

查看:378
本文介绍了使用C#创建excel文件时无法打开它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用c#windows窗口创建excel文件visual studio 2015

i需要使用扩展名xlsx创建excel 2007 excel文件

点击按钮后得到到路径我需要在路径中创建我找到的文件

但是无法打开它



我的代码如下:

  public   void  CreateSheetIfNotExists()
{
使用(System.Data.OleDb.OleDbConnection databaseConnection = new System.Data.OleDb.OleDbConnection( ))
{

DataTable schemaTable = 默认(DataTable);

databaseConnection.ConnectionString = Provider = Microsoft.ACE.OLEDB.12.0; + 数据源= D:\\Book310.xlsx;扩展属性= Excel 12.0;;

databaseConnection.Open();

schemaTable = databaseConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object [] {
null
null
Sheet1 $
});

if (schemaTable.Rows.Count == 0
{
string SQLDDLCommand = 创建表[Sheet1](UserID INTEGER,UserName CHAR(255));
System.Data.OleDb.OleDbCommand excelCommand = new System.Data.OleDb.OleDbCommand(SQLDDLCommand,databaseConnection);
excelCommand.ExecuteNonQuery();

}

databaseConnection.Close();
}
}



创建文件310但未打开

打开文件时出现此错误



excel无法打开文件,因为文件格式或文件扩展名无效。验证文件是否已损坏,文件扩展名是否与文件格式匹配。

如果可能,如何解决此问题



我尝试了什么:



使用c#创建Excel文件时无法打开它

解决方案


});

if (schemaTable.Rows.Count == 0
{
string SQLDDLCommand = 创建表[Sheet1](UserID INTEGER,UserName CHAR(255));
System.Data.OleDb.OleDbCommand excelCommand = new System.Data.OleDb.OleDbCommand(SQLDDLCommand,databaseConnection);
excelCommand.ExecuteNonQuery();

}

databaseConnection.Close();
}
}



创建文件310但未打开

打开文件时出现此错误



excel无法打开文件,因为文件格式或文件扩展名无效。验证文件是否已损坏,文件扩展名是否与文件格式匹配。

如果可能,如何解决此问题



我尝试过:



使用c#创建Excel文件时无法打开它


听起来你需要指定Xml:



扩展属性= \Excel 12.0 Xml ; HDR = YES \

请参阅使用OLEDB读写Excel文档 [ ^ ]。

I try to create excel file using c# windows form visual studio 2015
i need to create excel file with excel 2007 with extension xlsx
after i click the button and got to path i need to create i found file in path
But cannot open it

my code as bellow:

public void CreateSheetIfNotExists()  
       {  
           using (System.Data.OleDb.OleDbConnection databaseConnection = new System.Data.OleDb.OleDbConnection())  
           {  
  
               DataTable schemaTable = default(DataTable);  
  
               databaseConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=D:\\Book310.xlsx;Extended Properties=Excel 12.0;";  
  
               databaseConnection.Open();  
  
               schemaTable = databaseConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] {  
           null,  
           null,  
           "Sheet1$"  
       });  
  
               if (schemaTable.Rows.Count == 0)  
               {  
                   string SQLDDLCommand = "CREATE TABLE [Sheet1] (UserID INTEGER, UserName CHAR(255))";  
                   System.Data.OleDb.OleDbCommand excelCommand = new System.Data.OleDb.OleDbCommand(SQLDDLCommand, databaseConnection);  
                   excelCommand.ExecuteNonQuery();  
                     
               }  
              
               databaseConnection.Close();  
           }  
       }


after that file 310 created but not open
when open file i get this error

excel cannot open the file because the file format or file extension is not valid. verify that the file has not been corrupted and that the file extention matches the format of the file."
How to solve this problem if possible

What I have tried:

When create Excel file using c# cannot open it

解决方案

" }); if (schemaTable.Rows.Count == 0) { string SQLDDLCommand = "CREATE TABLE [Sheet1] (UserID INTEGER, UserName CHAR(255))"; System.Data.OleDb.OleDbCommand excelCommand = new System.Data.OleDb.OleDbCommand(SQLDDLCommand, databaseConnection); excelCommand.ExecuteNonQuery(); } databaseConnection.Close(); } }


after that file 310 created but not open
when open file i get this error

excel cannot open the file because the file format or file extension is not valid. verify that the file has not been corrupted and that the file extention matches the format of the file."
How to solve this problem if possible

What I have tried:

When create Excel file using c# cannot open it


It sounds like you need specify Xml:

Extended Properties=\"Excel 12.0 Xml;HDR=YES\"


See Read and Write Excel Documents Using OLEDB[^].


这篇关于使用C#创建excel文件时无法打开它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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