在 SSIS 脚本任务中读取 Excel 单元格值时出现 OleDbException

发布于 2025-01-09 10:41:57 字数 3060 浏览 1 评论 0原文

我有一个 SSIS 包,我可以在其中读取 Excel 文件并将数据加载到 SQL 表中。读取 Excel 文件的一部分是检查每个工作表中特定单元格中的值,并确保该值与工作表名称匹配。我在脚本任务中有以下代码(这不是全部代码,只是直到错误发生的相关部分):

public void Main()

{

//Declare and initialize variables

String FolderPath = Dts.Variables["User::FolderPath"].Value.ToString();

String FilePath = Dts.Variables["User::FolderPath"].Value.ToString() + Dts.Variables["User::strCurrentFileIn"].Value.ToString();

String TableName = Dts.Variables["User::TableName"].Value.ToString();

String SchemaName = Dts.Variables["User::SchemaName"].Value.ToString();

String CellToRead = Dts.Variables["User::CellToRead"].Value.ToString();

String CellName = "";

Int32 CellNumber;



var directory = new DirectoryInfo(FolderPath);

FileInfo files = new FileInfo(FilePath);

String FileLoaddate;



ConnectionManager cm;

SqlConnection myADONetConnection;

SqlCommand cmd;



cm = Dts.Connections["CP_RACO_ADO"];

myADONetConnection = (SqlConnection)cm.AcquireConnection(Dts.Transaction);



int intIndex = 1;



var alphanumericstring = new System.Text.RegularExpressions.Regex("(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)");

var match = alphanumericstring.Match(CellToRead);

CellName = match.Groups["Alpha"].Value;

int.TryParse(match.Groups["Numeric"].Value, out CellNumber);



//Process current inbound file

FileLoaddate = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss");



//Create Excel Connection

string ConStr;

string LoadingFilename = files.Name;

string HDR;

HDR = "NO";

ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"" + FilePath + "\";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=1\"";

OleDbConnection cnn = new OleDbConnection(ConStr);



////Get Sheet Name

cnn.Open();

DataTable dtSheet = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

string sheetname;

sheetname = "";

string ldsheetname = "";



//Get Row Count for each sheet

foreach (DataRow drSheet in dtSheet.Rows)

{

    if (drSheet["TABLE_NAME"].ToString().Contains("$"))

    {

        sheetname = drSheet["TABLE_NAME"].ToString();

        ldsheetname = sheetname.Replace("$", "");

        ldsheetname = ldsheetname.Replace("'", "");

        ldsheetname = ldsheetname.Replace(" ", "");



        //Read the cell value from Excel sheet

        OleDbCommand oCell = new OleDbCommand("select  * from [" + sheetname + CellName + (CellNumber - 1).ToString() + ":" + CellToRead + "]", cnn);

        OleDbDataAdapter daCell = new OleDbDataAdapter(oCell);

        DataTable dtCell = new DataTable();

        daCell.Fill(dtCell);

一切正常,直到 daCell.Fill(dtCell) 语句。此时,脚本会抛出以下错误:

System.Data.OleDb.OleDbException: Microsoft Access 数据库引擎找不到对象“BETTAREL, BRIAN J$'B8:B9”。制作 确保该对象存在并且您拼写了它的名称和路径名 正确。

本节之后不久就是实际将所有 Excel 数据加载到表中的代码,并且该部分可以正常工作。那么为什么一次加载整个工作表可以工作,但我无法读取单个单元格值?造成此错误的原因是什么?如何修复此错误?我尝试添加感叹号(就像您在 Excel 公式中找到的那样),但没有成功。我尝试过删除美元符号,但这不起作用。我尝试使用替换来去掉引号,但这不起作用。

某些原因导致了这种情况,并且无论进行多少谷歌搜索或弄乱代码似乎都不起作用。有人知道为什么会发生这种情况,和/或我需要做什么才能让它发挥作用?

I have an SSIS package where I am reading Excel files and loading the data to SQL tables. Part of reading the Excel file is to check the value in a specific cell in each sheet and make sure that the value matches the sheet name. I have the following code in the script task (this is not all of the code, only the relevant part up to where the error happens):

public void Main()

{

//Declare and initialize variables

String FolderPath = Dts.Variables["User::FolderPath"].Value.ToString();

String FilePath = Dts.Variables["User::FolderPath"].Value.ToString() + Dts.Variables["User::strCurrentFileIn"].Value.ToString();

String TableName = Dts.Variables["User::TableName"].Value.ToString();

String SchemaName = Dts.Variables["User::SchemaName"].Value.ToString();

String CellToRead = Dts.Variables["User::CellToRead"].Value.ToString();

String CellName = "";

Int32 CellNumber;



var directory = new DirectoryInfo(FolderPath);

FileInfo files = new FileInfo(FilePath);

String FileLoaddate;



ConnectionManager cm;

SqlConnection myADONetConnection;

SqlCommand cmd;



cm = Dts.Connections["CP_RACO_ADO"];

myADONetConnection = (SqlConnection)cm.AcquireConnection(Dts.Transaction);



int intIndex = 1;



var alphanumericstring = new System.Text.RegularExpressions.Regex("(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)");

var match = alphanumericstring.Match(CellToRead);

CellName = match.Groups["Alpha"].Value;

int.TryParse(match.Groups["Numeric"].Value, out CellNumber);



//Process current inbound file

FileLoaddate = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss");



//Create Excel Connection

string ConStr;

string LoadingFilename = files.Name;

string HDR;

HDR = "NO";

ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"" + FilePath + "\";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=1\"";

OleDbConnection cnn = new OleDbConnection(ConStr);



////Get Sheet Name

cnn.Open();

DataTable dtSheet = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

string sheetname;

sheetname = "";

string ldsheetname = "";



//Get Row Count for each sheet

foreach (DataRow drSheet in dtSheet.Rows)

{

    if (drSheet["TABLE_NAME"].ToString().Contains("
quot;))

    {

        sheetname = drSheet["TABLE_NAME"].ToString();

        ldsheetname = sheetname.Replace("
quot;, "");

        ldsheetname = ldsheetname.Replace("'", "");

        ldsheetname = ldsheetname.Replace(" ", "");



        //Read the cell value from Excel sheet

        OleDbCommand oCell = new OleDbCommand("select  * from [" + sheetname + CellName + (CellNumber - 1).ToString() + ":" + CellToRead + "]", cnn);

        OleDbDataAdapter daCell = new OleDbDataAdapter(oCell);

        DataTable dtCell = new DataTable();

        daCell.Fill(dtCell);

Everything works just fine, right up until the daCell.Fill(dtCell) statement. At that point, the script throws the following error:

System.Data.OleDb.OleDbException: The Microsoft Access database engine could not find the object ''BETTAREL, BRIAN J$'B8:B9'. Make
sure the object exists and that you spell its name and the path name
correctly.

Shortly after this section is the code that actually loads all of the Excel data into a table, and that part works. So why is it that loading an entire sheet at once works, but I can't read a single cell value? What is causing this error, and how do I fix this? I've tried adding an exclamation mark (like you'd find in Excel formulas), and that didn't work. I've tried removing the dollar sign, and that doesn't work. I've tried using a replace to get rid of the quotation marks, and that doesn't work.

Something is causing this, and no amount of googling or messing with the code seems to work. Anybody have any idea why this is happening, and/or what I need to do to get this to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最好是你 2025-01-16 10:41:57

在这里找到了答案。工作表名称中有单引号,当我删除引号时,我删除了双引号。删除单引号有效,代码现在可以运行。

Figured out the answer here. There were single quotes in the sheet name, and when I went to remove the quotation marks I removed the doubles. Removing the single quotes worked, and the code now works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文