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

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

        /// <summary>
        /// 读取指定范围内的Excel,指定起始行和终止行
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="sheetName"></param>
        /// <param name="startColName"></param>
        /// <param name="endColName"></param>
        /// <param name="rowStartIndex"></param>
        /// <param name="rowEndIndex"></param>
        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<Sheet> sheets = workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where(s => s.Name == sheetName);
                if (sheets.Count() == 0)
                {
                    return;
                }
                string relationshipId = sheets.First().Id.Value;
                WorksheetPart worksheetPart = (WorksheetPart)spreadsheetDocument.WorkbookPart.GetPartById(relationshipId);

                Dictionary<string, string> dicResult = new Dictionary<string, string>();
                IEnumerable<Row> rows = worksheetPart.Worksheet.GetFirstChild<SheetData>().Elements<Row>();
                if (rows.Count() == 0)
                {
                    return;
                }

                foreach (Row row in rows)
                {
                    if (rowStartIndex<=row.RowIndex&& row.RowIndex<=rowEndIndex)
                    {
                        IEnumerable<Cell> cells = row.Elements<Cell>();
                        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一定范围内的值时,指定起始行和终点行,起始列的字符,终点列的字符。

 /// <summary>
        /// 读取指定范围内的Excel,指定起始行和终止行,指定起止列
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="sheetName"></param>
        /// <param name="startColName"></param>
        /// <param name="endColName"></param>
        /// <param name="rowStartIndex"></param>
        /// <param name="rowEndIndex"></param>
        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<Sheet> sheets = workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where(s => s.Name == sheetName);
                if (sheets.Count() == 0)
                {
                    return;
                }
                string relationshipId = sheets.First().Id.Value;
                WorksheetPart worksheetPart = (WorksheetPart)spreadsheetDocument.WorkbookPart.GetPartById(relationshipId);

                Dictionary<string, string> dicResult = new Dictionary<string, string>();
                IEnumerable<Row> rows = worksheetPart.Worksheet.GetFirstChild<SheetData>().Elements<Row>();
                if (rows.Count() == 0)
                {
                    return;
                }

                foreach (Row row in rows)
                {
                    if (rowStartIndex <= row.RowIndex && row.RowIndex <= rowEndIndex)
                    {
                        IEnumerable<Cell> cells = row.Elements<Cell>();
                        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;
        }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值