我创建了一个程序来挖掘和导入来自大约 75 个电子表格的数据到一个 oracle 表中。我能够连接、遍历工作表并抓取看起来不错的单元格和行。问题是,如果 Excel 工作表是在分组行折叠的情况下保存的,它会跳过这些行。如果有扩展属性或 reg 设置允许我在进入时扩展组,我在任何地方都找不到?不知道如何绕过折叠组(未合并单元格,我可以毫无问题地处理这些组)。
代码位:
//Starting where I iterate through a particular sheet
var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0; data source={0}; Extended Properties=\"Excel 12.0;HDR=NO;IMEX=1;ReadOnly=0\"", fileName);
OleDbConnection objConn = new OleDbConnection(connectionString);
try
{
objConn.Open();
System.Data.DataTable dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
if (dt != null)
{
foreach (DataRow row in dt.Rows)
{
var adapter = new OleDbDataAdapter("SELECT F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12 FROM [" + row["TABLE_NAME"].ToString() + "]", connectionString);
var ds = new DataSet();
try
{
adapter.Fill(ds, "anyname");
}
catch
{
break;
}
DataTable data = ds.Tables[0];
int rownum = 0;
// <a bunch of variable declarations>
foreach (DataRow row_b in data.Rows)
{
// start slogging through the rows
rownum = rownum++;
// <reset some variables>
if (rownum == 1) // Catch valid scripts that contain a number
{
foreach (DataColumn column in data.Columns)
{
if (column.ToString() == "F1")
{
// <processing code for this column>
}
if (column.ToString() == "F2")
{
// <processing code for this column>
}
if (column.ToString() == "F3")
{
// <you get the picture>
}
}
}
if (rownum == 3)
{
// <moving along through the rows...different processing>
}
// <..rows 4-11..>
if (rownum > 12 )
{
// <more value assignment>
}
string allvals = APPLICATION + E_USER + STEP_DESC + VARIATIONS + STATUS + STOPS_TESTING + ISSUE_NUM + ISSUE_COMMENTS + ADDITIONAL_INFO;
allvals = allvals.Trim();
//Don't want sheets that come across as Print Area this shouldn't affect the row processing
isPrintArea = 0;
if (BOOKSHEET.Contains("Print_Area"))
{
isPrintArea = 1;
}
Boolean addornot=false;
if (cb_forallscripts.Checked == true)
{
addornot = (STEP_NUM != 0 &&
allvals != "" &&
isPrintArea == 0 &&
SCRIPT_NUM != 0);
}
else
{
addornot = (STEP_NUM != 0 &&
allvals != "" &&
isPrintArea == 0 &&
SCRIPT_NUM != 0 &&
runScripts.Contains(SCRIPT_NUM.ToString()));
}
if (addornot)
{
//<connect to our Oracle db, I set up oCmd outside this>
OracleCommand oCmd = new OracleCommand();
oCmd.Connection = oConn;
oCmd.CommandType = CommandType.Text;
oCmd.Parameters.Add("STEP_NUM", STEP_NUM);
// <... bunch of parameters ...>
oCmd.Parameters.Add("script", SCRIPT);
oCmd.CommandText = "<My insert statement> ";
oCmd.ExecuteNonQuery();
}
}
}
}
}
catch ( <error processing>)
{ }