阅读Excel文件问题 [英] Reading an Excel File issue
问题描述
我使用的是Microsoft.Interop dll版本14.
OLEDB连接字符串12.0
我的代码,
Hi,
I am using Microsoft.Interop dll Version 14.
OLEDB Connection string 12.0
My code,
try
{
string connStr = CommonConstants.ExcelConnectionString.Replace("FILENAME", InputFileWithPath);
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb = null;
excel.Visible = false;
string SheetName = string.Empty;
try
{
object missing = System.Reflection.Missing.Value;
wb = excel.Workbooks.Open(InputFileWithPath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in wb.Sheets)
{
SheetName = sheet.Name + "$";
break;
}
excel.Quit();
}
finally
{
if (excel != null)
{
excel.Quit();
ClsCommon.ExcelClose(excel);
}
}
using (conn = new OleDbConnection(connStr))
{
conn.Open();
System.Data.DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
String[] excelSheets = new String[dt.Rows.Count];
adapter = new OleDbDataAdapter("SELECT F1,F3,F5,F7,F8,F9,F10,F14,F16,F17,F18 FROM [" + SheetName + "]", conn);
adapter.Fill(ClsParameter.dtInputFile);
conn.Close();
for (int j = 0; j < 10; j++)
{
ClsParameter.dtInputFile.Rows.RemoveAt(0);
}
ClsParameter.dtInputFile.AcceptChanges();
}
}
catch (Exception ex)
{
ClsParameter.TotalErrorCount++;
ClsLog.WriteLogFile("Error while Reading Input File. Error : " + ex.Message.ToString(), CommonConstants.LogType_Error);
return false;
}
finally
{
if (conn != null && conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
conn = null;
System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("Excel");
foreach (System.Diagnostics.Process p in process)
{
if (!string.IsNullOrEmpty(p.ProcessName))
{
try
{
p.Kill();
}
catch { }
}
}
}
}
当我尝试在具有Microsoft Office 2013的系统上运行相同时,我收到如下所述的错误:
HRESULT异常:0x800AC472。
我已经安装了2007办公系统数据驱动程序,但是工作正常。
请帮忙。
When i am trying to run the same on system having Microsoft Office 2013 i am getting error as described below :
Exception from HRESULT: 0x800AC472.
I have installed 2007 office System Data Drivers but it dint work.
Kindly help.
推荐答案
;
break ;
}
excel.Quit();
}
最后
{
if (excel!= null )
{
excel.Quit();
ClsCommon.ExcelClose(excel);
}
}
使用(conn = new OleDbConnection(connStr ))
{
conn.Open();
System.Data.DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null );
字符串 [] excelSheets = new String [dt.Rows.Count];
adapter = new OleDbDataAdapter( SELECT F1 ,F3,F5,F7,F8,F9,F10,F14,F16,F17,F18 FROM [ + SheetName + ],conn);
adapter.Fill(ClsParameter.dtInputFile);
conn.Close();
for ( int j = 0 ; j < 10 ; j ++)
{
ClsParameter.dtInputFile.Rows.RemoveAt( 0 );
}
ClsParameter.dtInputFile.AcceptChanges();
}
}
catch (例外情况)
{
ClsParameter.TotalErrorCount ++;
ClsLog.WriteLogFile( 读取输入文件时出错。错误: + ex.Message .ToString(),CommonConstants.LogType_Error);
return false ;
}
最后
{
如果(conn != null && conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
conn = null ;
System.Diagnostics.Process [] process = System.Diagnostics.Process.GetProcessesByName( Excel跨度>);
foreach (System.Diagnostics.Process p in process)
{
if (!string.IsNullOrEmpty(p.ProcessName))
{
try
{
p.Kill();
}
catch {}
}
}
}
}
"; break; } excel.Quit(); } finally { if (excel != null) { excel.Quit(); ClsCommon.ExcelClose(excel); } } using (conn = new OleDbConnection(connStr)) { conn.Open(); System.Data.DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); String[] excelSheets = new String[dt.Rows.Count]; adapter = new OleDbDataAdapter("SELECT F1,F3,F5,F7,F8,F9,F10,F14,F16,F17,F18 FROM [" + SheetName + "]", conn); adapter.Fill(ClsParameter.dtInputFile); conn.Close(); for (int j = 0; j < 10; j++) { ClsParameter.dtInputFile.Rows.RemoveAt(0); } ClsParameter.dtInputFile.AcceptChanges(); } } catch (Exception ex) { ClsParameter.TotalErrorCount++; ClsLog.WriteLogFile("Error while Reading Input File. Error : " + ex.Message.ToString(), CommonConstants.LogType_Error); return false; } finally { if (conn != null && conn.State == ConnectionState.Open) { conn.Close(); conn.Dispose(); conn = null; System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("Excel"); foreach (System.Diagnostics.Process p in process) { if (!string.IsNullOrEmpty(p.ProcessName)) { try { p.Kill(); } catch { } } } } }
当我尝试在具有Microsoft Office 2013的系统上运行相同时,我收到如下所述的错误:
HRESULT异常:0x800AC472。
我已经安装了2007办公系统数据驱动程序,但是工作正常。
请帮助。
When i am trying to run the same on system having Microsoft Office 2013 i am getting error as described below :
Exception from HRESULT: 0x800AC472.
I have installed 2007 office System Data Drivers but it dint work.
Kindly help.
您需要在机器上安装正确的互操作程序 - https://msdn.microsoft.com/en-us/library/15s06t57.aspx [ ^ ]。如果您使用的是互操作版本14,那么我猜您需要为office 2010而不是office 2007安装interop。
You need to install the right interop assmeblies on the machine - https://msdn.microsoft.com/en-us/library/15s06t57.aspx[^]. If you are use interop version 14 then I guess you need to install interop for office 2010 and not office 2007.
这篇关于阅读Excel文件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!