在电子表格中搜索输入字符串 [英] Search of an input string in spreadsheet

查看:93
本文介绍了在电子表格中搜索输入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Open XML SDK打开Excel文件(xlsx),我想查找从外部传递的特定字符串或整数,以检查电子表格中值的重复项.

I am using the Open XML SDK to open an Excel file (xlsx) and I want to find a specific string or integer passed from outside to check for duplicates of the value in the spreadsheet.

如何在电子表格的所有单元格中搜索输入字符串?

How can I search for the input string in all the cells of spreadsheet?

推荐答案

此处:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SpreadsheetDocument document = SpreadsheetDocument.Open(@"C:\Users\user\Desktop\Book1.xlsx", true))
            {
                Sheet sheet = document.WorkbookPart.Workbook.Descendants<Sheet>().First<Sheet>();
                Worksheet worksheet = ((WorksheetPart)document.WorkbookPart.GetPartById(sheet.Id)).Worksheet;
                IEnumerable<Row> allRows = worksheet.GetFirstChild<SheetData>().Descendants<Row>();
                foreach (Row currentRow in allRows)
                {
                    IEnumerable<Cell> allCells = currentRow.Descendants<Cell>();
                    foreach (Cell currentCell in allCells)
                    {
                        CellValue currentCellValue = currentCell.GetFirstChild<CellValue>();
                        string data = null;
                        if (currentCell.DataType != null)
                        {
                            if (currentCell.DataType == CellValues.SharedString) // cell has a cell value that is a string, thus, stored else where
                            {
                                data = document.WorkbookPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault().SharedStringTable.ElementAt(int.Parse(currentCellValue.Text)).InnerText;
                            }
                        }
                        else
                        {
                            data = currentCellValue.Text;
                        }
                        Console.WriteLine(data);

                        /* 
                            your code here

                                if(data.contains("myText"))
                                    doSomething();

                        */

                    }
                }
            }           
        }
    }
}

这篇关于在电子表格中搜索输入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆