简体   繁体   English

在脚本任务中使用oledb读取excel文件

[英]Read excel file using oledb in Script task

I am trying to read data from Excel using the oledb command in the script task of SSIS.我正在尝试使用 SSIS 脚本任务中的 oledb 命令从 Excel 读取数据。 The excel does not have sheet names. excel 没有工作表名称。 I am getting error as the sheet name is empty.由于工作表名称为空,我收到错误消息。 Below is the code used下面是使用的代码

excelConnection = new OleDbConnection(excelConnectionString);
                excelConnection.Open();  

                DataTable excelDataTable = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                string excelSheetName = string.Empty;

                foreach (DataRow row in excelDataTable.Rows)
                {
                    excelSheetName = row[0].ToString().Trim("'".ToCharArray());
                    //Console.writeLine(excelSheetName);
                }

                OleDbDataAdapter excelAdapter = new OleDbDataAdapter();
                OleDbCommand excelCommand = new OleDbCommand();
                DataSet excelDataSet = new DataSet();

                excelCommand.Connection = excelConnection;
                excelCommand.CommandText = "Select * from [" + excelSheetName + "]";
                excelAdapter.SelectCommand = excelCommand;

                excelAdapter.Fill(excelDataSet);  // error here

How to fix this?如何解决这个问题? Thanks谢谢

我使用 - excelSheetName = row["TABLE_NAME"].ToString(); 修复了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM