C# 处理动态excel表格

C# 处理动态excel表格,c#,excel,dynamic,ssis,C#,Excel,Dynamic,Ssis,我有一个案例,我需要导入一个excel文件,在DB中有两个工作表。我正在使用SSIS包进行同样的操作。问题是,我可以通过设置表达式使excel工作表动态化,但excel工作簿中的工作表也在更改名称。如何使图纸名称更具动态性。 我已尝试在开发代码中使用Microsoft.Office.InterOp.excel,但产品上没有安装excel。谁能帮我解决这个问题 提前感谢。尝试添加类似于以下脚本的内容,可在中找到。它不需要在计算机上安装Excel string excelFile = null; s

我有一个案例,我需要导入一个excel文件,在DB中有两个工作表。我正在使用SSIS包进行同样的操作。问题是,我可以通过设置表达式使excel工作表动态化,但excel工作簿中的工作表也在更改名称。如何使图纸名称更具动态性。 我已尝试在开发代码中使用Microsoft.Office.InterOp.excel,但产品上没有安装excel。谁能帮我解决这个问题


提前感谢。

尝试添加类似于以下脚本的内容,可在中找到。它不需要在计算机上安装Excel

string excelFile = null;
string connectionString = null;
OleDbConnection excelConnection = null;
DataTable tablesInFile = null;
int tableCount = 0;
DataRow tableInFile = null;
string currentTable = null;
int tableIndex = 0;
string[] excelTables = null;

excelFile = Dts.Variables["User::ExcelFile"].Value.ToString();

connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFile + ";Extended Properties=Excel 8.0";

excelConnection = new OleDbConnection(connectionString);

excelConnection.Open();
tablesInFile = excelConnection.GetSchema("Tables");

tableCount = tablesInFile.Rows.Count;
excelTables = new string[tableCount];

foreach (DataRow tableInFile_loopVariable in tablesInFile.Rows)
{
tableInFile = tableInFile_loopVariable;
currentTable = tableInFile["TABLE_NAME"].ToString();
excelTables[tableIndex] = currentTable;
tableIndex += 1;
}
}

Dts.Variables["User::SheetName"].Value = excelTables[0];

Dts.TaskResult = (int)ScriptResults.Success;

我正在使用SQL脚本将Excel文件原样加载到一个暂存表中,然后使用SSIS/T-SQL对其进行处理,速度相对较快且可靠。您可以自行下载驱动程序,使用此方法不需要office安装

/*Drop table if exists*/
IF OBJECT_ID(’table_1', 'U') IS NOT NULL
EXEC ('DROP TABLE table_1')

/*Load using access driver, can probably work with excel too.*/
select *
into [table_1]
from openrowset('MSDASQL'
               ,'Driver={Microsoft Access Text Driver (*.txt, *.csv)}'
               ,'select * from 'D:\folder\file.csv' ')