Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private List<string> readRow(IRow row)
- {
- var rs = new List<string>();
- // Since there would be no emtpy cell,
- // we start from the left-most cell and read until the end.
- for (int colIndex = 0; colIndex < row.LastCellNum; colIndex++)
- {
- var cell = row.GetCell(colIndex);
- // Get the value of this cell, whether it is part of a merged cell or not.
- var cellValue = getCellValue(cell);
- rs.Add(cellValue);
- }
- return rs;
- }
- private string getCellValue(ICell cell)
- {
- var dataFormatter = new DataFormatter(CultureInfo.CurrentCulture);
- // If this is not part of a merge cell,
- // just get this cell's value like normal.
- if (!cell.IsMergedCell)
- {
- return dataFormatter.FormatCellValue(cell);
- }
- // Otherwise, we need to find the value of this merged cell.
- else
- {
- // Get current sheet.
- var currentSheet = cell.Sheet;
- // Loop through all merge regions in this sheet.
- for (int i = 0; i < currentSheet.NumMergedRegions; i++)
- {
- var mergeRegion = currentSheet.GetMergedRegion(i);
- // If this merged region contains this cell.
- if (mergeRegion.FirstRow <= cell.RowIndex && cell.RowIndex <= mergeRegion.LastRow &&
- mergeRegion.FirstColumn <= cell.ColumnIndex && cell.ColumnIndex <= mergeRegion.LastColumn)
- {
- // Find the top-most and left-most cell in this region.
- var firstRegionCell = currentSheet.GetRow(mergeRegion.FirstRow)
- .GetCell(mergeRegion.FirstColumn);
- // And return its value.
- return dataFormatter.FormatCellValue(firstRegionCell);
- }
- }
- // This should never happen.
- throw new Exception("Cannot find this cell in any merged region");
- }
- }
- // And return its value.
- return dataFormatter.FormatCellValue(firstRegionCell);
- }
- else
- return dataFormatter.FormatCellValue(cell); //return cell if isMerge is true but not in range cells
Add Comment
Please, Sign In to add comment