Open XML读取Excel一定范围内的值

1 读取Excel一定范围内的值时,指定起始行和终点行。

        /// 
        /// 读取指定范围内的Excel,指定起始行和终止行
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        private static void ReadExcel(string filePath, string sheetName, uint rowStartIndex, uint rowEndIndex)
        {
            using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filePath, false))
            {
                WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
                Workbook workbook = workbookPart.Workbook;

                IEnumerable sheets = workbook.GetFirstChild().Elements().Where(s => s.Name == sheetName);
                if (sheets.Count() == 0)
                {
                    return;
                }
                string relationshipId = sheets.First().Id.Value;
                WorksheetPart worksheetPart = (WorksheetPart)spreadsheetDocument.WorkbookPart.GetPartById(relationshipId);

                Dictionary dicResult = new Dictionary();
                IEnumerable rows = worksheetPart.Worksheet.GetFirstChild().Elements();
                if (rows.Count() == 0)
                {
                    return;
                }

                foreach (Row row in rows)
                {
                    if (rowStartIndex<=row.RowIndex&& row.RowIndex<=rowEndIndex)
                    {
                        IEnumerable cells = row.Elements();
                        if (cells.Count() == 0)
                        {
                            continue;
                        }
                        else
                        {
                            foreach (Cell cell in cells)
                            {
                                dicResult.Add(cell.CellReference, cell.CellValue.Text);
                            }
                        }
                    }                    
                }
                foreach (var dic in dicResult)
                {
                    Console.WriteLine($"单元格:{dic.Key}的值:{dic.Value}");
                }
            }
        }

2 读取Excel一定范围内的值时,指定起始行和终点行,起始列的字符,终点列的字符。

 /// 
        /// 读取指定范围内的Excel,指定起始行和终止行,指定起止列
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        private static void ReadExcel(string filePath, string sheetName, uint rowStartIndex, uint rowEndIndex,char cellStartIndex, char cellEndIndex)
        {
            using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filePath, false))
            {
                WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
                Workbook workbook = workbookPart.Workbook;

                IEnumerable sheets = workbook.GetFirstChild().Elements().Where(s => s.Name == sheetName);
                if (sheets.Count() == 0)
                {
                    return;
                }
                string relationshipId = sheets.First().Id.Value;
                WorksheetPart worksheetPart = (WorksheetPart)spreadsheetDocument.WorkbookPart.GetPartById(relationshipId);

                Dictionary dicResult = new Dictionary();
                IEnumerable rows = worksheetPart.Worksheet.GetFirstChild().Elements();
                if (rows.Count() == 0)
                {
                    return;
                }

                foreach (Row row in rows)
                {
                    if (rowStartIndex <= row.RowIndex && row.RowIndex <= rowEndIndex)
                    {
                        IEnumerable cells = row.Elements();
                        if (cells.Count() == 0)
                        {
                            continue;
                        }
                        else
                        {
                            foreach (Cell cell in cells)
                            {
                                if(cellStartIndex<= GetColumnName(cell.CellReference.Value).First()&& GetColumnName(cell.CellReference.Value).First()<=cellEndIndex)
                                {
                                    dicResult.Add(cell.CellReference, cell.CellValue.Text);
                                }
                            }
                        }
                    }
                }
                foreach (var dic in dicResult)
                {
                    Console.WriteLine($"单元格:{dic.Key}的值:{dic.Value}");
                }
            }
        }

private static uint GetRowIndex(string cellName)
        {
            // Create a regular expression to match the row index portion the cell name.
            Regex regex = new Regex(@"\d+");
            Match match = regex.Match(cellName);

            return uint.Parse(match.Value);
        }

        private static string GetColumnName(string cellName)
        {
            // Create a regular expression to match the row index portion the cell name.
            Regex regex = new Regex(@"[A-Z]");
            Match match = regex.Match(cellName);

            return match.Value;
        }

 

你可能感兴趣的:(其他)