1.第一步判断( (Excel.Range)excelRange.Cells[5,6]).MergeCells确定是否是合并单元格
2. 第二步判断( (Excel.Range)excelRange.Cells[5,6]).MergeArea.Row和( (Excel.Range)excelRange.Cells[5,6]).MergeArea.Column判断合并单元格的起始行号和列号
例如:合并单元格从B2值H8,那么
( (Excel.Range)excelRange.Cells[5,6]).MergeArea.Row=2
( (Excel.Range)excelRange.Cells[5,6]).MergeArea.Column=2
// 下面是正常代码流程
Excel.Application xlApp = new Excel.ApplicationClass();
if (xlApp == null)
{
System.Windows.Forms.MessageBox.Show("EXCEL could not be started. Check that your office installation and project references are correct.");
return null;
}
Excel.Workbook xlWorkBook = xlApp.Workbooks.Open("c:\\test.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Excel.Range excelRange = xlWorkSheet.UsedRange;
int RowCount = excelRange.Rows.Count;
int ColCount = excelRange.Columns.Count;
//Range mergeArea = curentCell.MergeArea;
//int count = mergeArea.Cells.Count; //合并列的个数(不分上下还是左右)
object[,] valueArray = (object[,])excelRange.get_Value(Excel.XlRangeValueDataType.xlRangeValueDefault);
Excel.Range subRange = (Excel.Range)excelRange.Cells[1, 1];
////设置禁止弹出保存和覆盖的询问提示框
//app.DisplayAlerts = false;
//app.AlertBeforeOverwriting = false;
xlWorkBook.Close(true, null, null);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);