从Excel读取时,外部表格不是预期的格式 [英] External table is not in the expected format while reading from Excel

查看:231
本文介绍了从Excel读取时,外部表格不是预期的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

我将数据导出到Excel(2003格式)。

但是当我尝试阅读相同的Excel时,我收到此错误

外部表格不符合预期格式



我的PC上安装了MS Office 2007。 />


当我打开Excel然后尝试读取它时,它工作正常,而预计它应该给我一个文件已被使用的错误信息。

(我想是的。如果我错了请告诉我)



这是我的代码所说的:

Hi Friends,
I am exporting data into Excel (2003 format).
But when I try to read the same Excel, I get this error
"External table is not in the expected format"

I have MS Office 2007 installed on my PC.

When I open that Excel and then try to read it, it works fine, whereas it is expected that it should give me error msg that the File is already being used.
(I think so. Please clear me if I am wrong)

Here is what my code says:

public enum ExcelVersion
{
            Excel,
            Excel2007
};
private string ProviderExcel = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
private string ExtendedPropertiesExcel = "Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
private string ProviderExcel2007 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=";
private string ExtendedPropertiesExcel2007 = "Extended Properties=\"Excel 12.0;HDR=YES\"";
private string mConnString = "";







On Button_Click甚至,我有..






On Button_Click Even, I have..

foreach (UploadedFile file in rdpUpload.UploadedFiles)
{
   FileName = file.GetName();
   sExtension = file.GetName();
   sExtension = sExtension.Substring(sExtension.LastIndexOf(".") + 1);
   if (sExtension != "xls")
     throw new System.Exception("Select .xls/.xlsx file ");
                    ExcelImport(ExcelVersion.Excel, FilePath + "//" + FileName);
                    GetPaymentVouchers();
}





这些是我的方法



And these are my methods

public void ExcelImport(ExcelVersion ver, string FilePath)
{
	switch (ver)
        {
                case ExcelVersion.Excel:
                    mConnString = ProviderExcel + FilePath + ";" + ExtendedPropertiesExcel;
                    break;
                case ExcelVersion.Excel2007:
                    mConnString = ProviderExcel2007 + FilePath + ";" + ExtendedPropertiesExcel2007;
                    break;
         }
}
public void GetPaymentVouchers()
{
	OleDbConnection cn = new OleDbConnection(mConnString);
        try
        {
                cn.Open();
                DataTable schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                string SheetName = schemaTable.Rows[0][2].ToString();
                StringBuilder SelectStatement = new StringBuilder();
                SelectStatement.Append("SELECT * FROM [").Append(SheetName).Append("]");
                OleDbDataAdapter da = new OleDbDataAdapter(SelectStatement.ToString(), cn);
                DataSet ds = new DataSet();
                da.Fill(ds);
         }
         catch (Exception ex)
         {
                XITingExceptionProcessor.ProcessException(this, ex);
         }
         finally
         {
                cn.Close();
         }
}





请帮帮我朋友。

谢谢,

Lok ..



Please help me out friends.
Thanks,
Lok..

推荐答案

看看这个:





http://forums.asp.net/t/1088515.aspx/1 [ ^ ]






我面临同样类型的问题解决方案。



只需去打开Excel文件



1)点击文件菜单。



2)点击另存为。 />


3)显示一个方框。



4)更改文件名并保存类型为Excel 97-2003工作簿(* .xls)并单击保存然后它工作。



此类问题也出现在.csv应用相同的程序并解决它。
Hi,

I am facing same type of problem solution.

Just go to Open Excel file

1) Click on File Menu.

2) Click on Save As .

3) Show a Box.

4) Change File Name & Save As Type to Excel 97-2003 Workbook (*.xls) & click save then it work.

Also this type of problem occurs in .csv apply same procedure & solve it.


这篇关于从Excel读取时,外部表格不是预期的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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