/// <summary> /// 首页初始化 /// </summary> private void IndexStepInit() { IWorkbook workbook = null; var execlPath = @"C:\Users\Administrator\Desktop\出入境咨询服务.xlsx"; using (FileStream fs = File.OpenRead(execlPath)) { //判断文件格式:HSSF只能读取xls,XSSF只能读取xlsx格式的 if (Path.GetExtension(fs.Name) == ".xls") { workbook = new HSSFWorkbook(fs); } else if (Path.GetExtension(fs.Name) == ".xlsx") { workbook = new XSSFWorkbook(fs); } } sheet = workbook.GetSheetAt(0); var indexQP = new Qpoint { Y = 0, X = 0, Xspan = MAX_INT }; //首页问题坐标 var qpList = SearchSells(indexQP); //首页问题列表 listQnode = new List <Qnode> { new Qnode { TSY = "请选择出入境类型?", Qpoints = qpList } }; }
/// <summary> /// 根据坐标查询符合条件的单元格 /// </summary> /// <param name="qpoint">问题坐标</param> private List <Qpoint> SearchSells(Qpoint qpoint) { List <Qpoint> tempList = new List <Qpoint>(); //符合坐标要求的,所有单元格 var cells = sheet.GetRow(qpoint.Y + 1).Cells.FindAll( p => (p.ColumnIndex >= qpoint.X && p.ColumnIndex < qpoint.Xspan) && !string.IsNullOrEmpty(p.StringCellValue)); if (cells != null && cells.Count > 0) { for (int i = 0; i < cells.Count; i++) { var tempSpan = qpoint.Xspan; //X轴跨度 if (i + 1 < cells.Count) { tempSpan = cells[i + 1].ColumnIndex; } var strArray = cells[i].StringCellValue.Split('>'); //分割单元格中的文本 if (strArray != null && strArray.Length > 0) { if (strArray.Length < 2) { tempList.Add(new Qpoint { Y = cells[i].RowIndex, X = cells[i].ColumnIndex, Xspan = tempSpan, Text = strArray[0], TSY = string.Empty, IsEnd = true }); } else { tempList.Add(new Qpoint { Y = cells[i].RowIndex, X = cells[i].ColumnIndex, Xspan = tempSpan, Text = strArray[0], TSY = strArray[1], IsEnd = false }); } } } } return(tempList); }