c#:有没有办法从数据开始的地方检索 excel 中的单元格地址?
c#: Is there a way to retrieve the cell address in excel from where data begins?
我正在尝试将 excel 数据从一个 sheet 复制到另一个。它工作正常,但 问题 是:在源文件中,如果 数据没有从单元格 A1 开始 (请考虑下图),在在这种情况下,我想从单元格 B5 复制数据。这里 Some header
不是必需的。实际数据从 Emp ID
个单元格开始。
我试过的是,我可以提供一个textbox
来输入单元格地址,然后开始从提供的单元格地址复制数据。但这引入了人工干预。我想要它自动化。对此的任何帮助表示赞赏。感谢您的帮助。
假设有一些基本标准,下面的代码应该可以做到。我假设的标准是:1) 如果一行包含任何合并的单元格(如您的 "Some Header"),那么该行不是起始行。 2) 起始单元格将在右侧单元格和下方单元格中包含文本。
private static bool RowIsEmpty(Range range)
{
foreach (object obj in (object[,])range.Value2)
{
if (obj != null && obj.ToString() != "")
{
return false;
}
}
return true;
}
private static bool CellIsEmpty(Range cell)
{
if (cell.Value2 != null && cell.Value2.ToString() != "")
{
return false;
}
return true;
}
private Tuple<int, int> ExcelFindStartCell()
{
var excelApp = new Microsoft.Office.Interop.Excel.Application();
excelApp.Visible = true;
Workbook workbook = excelApp.Workbooks.Open("test.xlsx");
Worksheet worksheet = excelApp.ActiveSheet;
// Go through each row.
for (int row = 1; row < worksheet.Rows.Count; row++)
{
Range range = worksheet.Rows[row];
// Check if the row is empty.
if (RowIsEmpty(range))
{
continue;
}
// Check if the row contains any merged cells, if so we'll assume it's
// some kind of header and move on.
object mergedCells = range.MergeCells;
if (mergedCells == DBNull.Value || (bool)mergedCells)
{
continue;
}
// Find the first column that contains text in this row.
for (int col = 1; col < range.Columns.Count; col++)
{
Range cell = range.Cells[1, col];
if (CellIsEmpty(cell))
{
continue;
}
// Now check if the cell to the right also contains text.
Range rightCell = worksheet.Cells[row, col + 1];
if (CellIsEmpty(rightCell))
{
// No text in right cell, try the next row.
break;
}
// Now check if cell below also contains text.
Range bottomCell = worksheet.Cells[row + 1, col];
if (CellIsEmpty(bottomCell))
{
// No text in bottom cell, try the next row.
break;
}
// Success!
workbook.Close();
excelApp.Quit();
return new Tuple<int, int>(row, col);
}
}
// Didn't find anything that matched the criteria.
workbook.Close();
excelApp.Quit();
return null;
}
我正在尝试将 excel 数据从一个 sheet 复制到另一个。它工作正常,但 问题 是:在源文件中,如果 数据没有从单元格 A1 开始 (请考虑下图),在在这种情况下,我想从单元格 B5 复制数据。这里 Some header
不是必需的。实际数据从 Emp ID
个单元格开始。
我试过的是,我可以提供一个textbox
来输入单元格地址,然后开始从提供的单元格地址复制数据。但这引入了人工干预。我想要它自动化。对此的任何帮助表示赞赏。感谢您的帮助。
假设有一些基本标准,下面的代码应该可以做到。我假设的标准是:1) 如果一行包含任何合并的单元格(如您的 "Some Header"),那么该行不是起始行。 2) 起始单元格将在右侧单元格和下方单元格中包含文本。
private static bool RowIsEmpty(Range range)
{
foreach (object obj in (object[,])range.Value2)
{
if (obj != null && obj.ToString() != "")
{
return false;
}
}
return true;
}
private static bool CellIsEmpty(Range cell)
{
if (cell.Value2 != null && cell.Value2.ToString() != "")
{
return false;
}
return true;
}
private Tuple<int, int> ExcelFindStartCell()
{
var excelApp = new Microsoft.Office.Interop.Excel.Application();
excelApp.Visible = true;
Workbook workbook = excelApp.Workbooks.Open("test.xlsx");
Worksheet worksheet = excelApp.ActiveSheet;
// Go through each row.
for (int row = 1; row < worksheet.Rows.Count; row++)
{
Range range = worksheet.Rows[row];
// Check if the row is empty.
if (RowIsEmpty(range))
{
continue;
}
// Check if the row contains any merged cells, if so we'll assume it's
// some kind of header and move on.
object mergedCells = range.MergeCells;
if (mergedCells == DBNull.Value || (bool)mergedCells)
{
continue;
}
// Find the first column that contains text in this row.
for (int col = 1; col < range.Columns.Count; col++)
{
Range cell = range.Cells[1, col];
if (CellIsEmpty(cell))
{
continue;
}
// Now check if the cell to the right also contains text.
Range rightCell = worksheet.Cells[row, col + 1];
if (CellIsEmpty(rightCell))
{
// No text in right cell, try the next row.
break;
}
// Now check if cell below also contains text.
Range bottomCell = worksheet.Cells[row + 1, col];
if (CellIsEmpty(bottomCell))
{
// No text in bottom cell, try the next row.
break;
}
// Success!
workbook.Close();
excelApp.Quit();
return new Tuple<int, int>(row, col);
}
}
// Didn't find anything that matched the criteria.
workbook.Close();
excelApp.Quit();
return null;
}