什么';在C#中对MS Excel文件运行正则表达式的最佳方法是什么?

什么';在C#中对MS Excel文件运行正则表达式的最佳方法是什么?,c#,.net,office-interop,C#,.net,Office Interop,我正在尝试读取一个MS Office Excel文档,以便可以对照它测试一组正则表达式。我有一些有用的东西,但它看起来非常笨拙。检查每个单元格。。。。我无法想象这是最好的方式。有什么好的建议吗 static void ReadMSOfficeExcelFile(string file) { try { Microsoft.Office.Interop.Excel.Application xlsApp = new Microsoft.Office.I

我正在尝试读取一个MS Office Excel文档,以便可以对照它测试一组正则表达式。我有一些有用的东西,但它看起来非常笨拙。检查每个单元格。。。。我无法想象这是最好的方式。有什么好的建议吗

    static void ReadMSOfficeExcelFile(string file) {
        try {
            Microsoft.Office.Interop.Excel.Application xlsApp = new Microsoft.Office.Interop.Excel.Application();
            object nullobj = System.Reflection.Missing.Value;
            object ofalse = false;
            object ofile = file;

            Microsoft.Office.Interop.Excel.Workbook xlsWorkbook = xlsApp.Workbooks.Open(
                                                             file, nullobj, nullobj,
                                                             nullobj, nullobj, nullobj,
                                                             nullobj, nullobj, nullobj,
                                                             nullobj, nullobj, nullobj,
                                                             nullobj, nullobj, nullobj);

            Microsoft.Office.Interop.Excel.Sheets xlsSheets = xlsWorkbook.Worksheets;

            foreach (Microsoft.Office.Interop.Excel.Worksheet xlsWorkSheet in xlsWorkbook.Sheets) {
                Microsoft.Office.Interop.Excel.Range xlsRange = xlsWorkSheet.UsedRange;
                foreach (Microsoft.Office.Interop.Excel.Range xlsRow in xlsRange.Rows) {
                    foreach (Microsoft.Office.Interop.Excel.Range xlsCell in xlsRow.Columns) {
                        CheckLineMatch(file, xlsCell.Text.ToString());
                    }
                }
            }
            xlsWorkbook.Close(ofalse, nullobj, nullobj);
        }
        catch {
            PrintError("Unable to parse file because of MS Office error.", file);
        }
    }

您应该查看此库:

本项目的目标是提供一个本机.NET解决方案,以便在不使用COM互操作或OLEDB连接的情况下创建、读取和修改Excel文件

目前实现了.xls(BIFF8)格式。将来也可能支持.xlsx(Excel2007)


这很好,但是这里没有太多的文档,所以你可能一开始会遇到一些困难。

我一直在看另一篇文章。我注意到一个帖子,上面写着:

“”“ Excel数据阅读器怎么样

我曾在it anger中,在生产环境中,将大量数据从各种Excel文件拉入SQL Server Compact。它运行良好,相当健壮。 “”“

它最终很好地满足了我的需要

    static void ReadMSOffice2003ExcelFile(string file) {
        FileStream stream = File.Open(file, FileMode.Open, FileAccess.Read);
        ReadMSOfficeExcelFile(file, ExcelReaderFactory.CreateBinaryReader(stream));
    }


    static void ReadMSOffice2007ExcelFile(string file) {
        FileStream stream = File.Open(file, FileMode.Open, FileAccess.Read);
        ReadMSOfficeExcelFile(file, ExcelReaderFactory.CreateOpenXmlReader(stream));
    }


    static void ReadMSOfficeExcelFile(string file, IExcelDataReader xlsReader) {
        string xlsRow;
        while (xlsReader.Read()) {
            xlsRow = "";
            for (int i = 0; i < xlsReader.FieldCount; i++) {
                xlsRow += " " + xlsReader.GetString(i);
            }
            CheckLineMatch(file, xlsRow);
        }
    }
static void ReadMSOffice2003ExcelFile(字符串文件){
FileStream stream=File.Open(File,FileMode.Open,FileAccess.Read);
ReadMSOfficeExcelFile(文件,ExcelReaderFactory.CreateBinaryReader(流));
}
静态无效ReadMSOffice2007ExcelFile(字符串文件){
FileStream stream=File.Open(File,FileMode.Open,FileAccess.Read);
ReadMSOfficeExcelFile(文件,ExcelReaderFactory.CreateOpenXmlReader(stream));
}
静态无效ReadMSOfficeExcelFile(字符串文件,IExcelDataReader xlsReader){
字符串xlsRow;
while(xlsReader.Read()){
xlsRow=“”;
对于(int i=0;i
使用ClosedXML并循环遍历列集

检查行匹配的功能是什么?听起来它应该返回一个
bool
。可能重复-它将文本与正则表达式列表进行比较。没有返回值。尼古拉:我已经读过了,对我来说没有任何意义。