NPOI遍历excel表格

遍历指定文件夹内所有的xls,首先获取所有文件夹内的xls文件路径,使用VBA脚本生成到一个xls文件中,VBA脚本如下:

Sub filelist()
Dim MyName, Dic, Did, i, t, F, TT, MyFileName
       'On Error Resume Next
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.BrowseForFolder(0, "选择文件夹", 0, 0)
    If Not objFolder Is Nothing Then lj = objFolder.self.Path & "\"
    Set objFolder = Nothing
    Set objShell = Nothing
  
    t = Time
    Set Dic = CreateObject("Scripting.Dictionary")    '创建一个字典对象
    Set Did = CreateObject("Scripting.Dictionary")
    Dic.Add (lj), ""
    i = 0
    Do While i < Dic.Count
        Ke = Dic.keys   '开始遍历字典
        MyName = Dir(Ke(i), vbDirectory)    '查找目录
        Do While MyName <> ""
            If MyName <> "." And MyName <> ".." Then
                If (GetAttr(Ke(i) & MyName) And vbDirectory) = vbDirectory Then    '如果是次级目录
                    Dic.Add (Ke(i) & MyName & "\"), ""  '就往字典中添加这个次级目录名作为一个条目
                End If
            End If
            MyName = Dir    '继续遍历寻找
        Loop
        i = i + 1
    Loop
    Did.Add ("文件清单"), ""    '以查找D盘下所有EXCEL文件为例
    For Each Ke In Dic.keys
        MyFileName = Dir(Ke & "*.xls")
        Do While MyFileName <> ""
            Did.Add (Ke & MyFileName), ""
            MyFileName = Dir
        Loop
    Next
    For Each Sh In ThisWorkbook.Worksheets
        If Sh.Name = "XLS文件清单" Then
            Sheets("XLS文件清单").Cells.Delete
            F = True
            Exit For
        Else
            F = False
        End If
    Next
    If Not F Then
        Sheets.Add.Name = "XLS文件清单"
    End If
    Sheets("XLS文件清单").[A1].Resize(Did.Count, 1) = WorksheetFunction.Transpose(Did.keys)
    TT = Time - t
    MsgBox Minute(TT) & "" & Second(TT) & ""
End Sub

 

读取VBA生成xls文件中存放的路径列,并传递给遍历方法

        [TestMethod]
        public void GetXlsTest()
        {
            WriteLog("error", "info", "query start");
            IWorkbook workbook = WorkbookFactory.Create("C:\\Users\\Administrator\\Desktop\\file-list.xlsx");
            ISheet sheet = workbook.GetSheetAt(0);
            for (int i = 1; i <= sheet.LastRowNum + 1; i++)
            {
                var row = sheet.GetRow(i);
                if (row != null)
                {
                    ICell cell = row.GetCell(0);
                    if (cell != null)
                    {
                        FindXlsSheetData(cell.ToString());
                    }
                }
            }
            WriteLog("error", "info", "query end");
        }

 遍历方法接收到路径,开始遍历每个xls中的sheet

        private void FindXlsSheetData(string path)
        {
            try
            {
                if (File.Exists(path))
                {
                    IWorkbook workbook = WorkbookFactory.Create(path);
                    for (int s = 0; s < workbook.NumberOfSheets; s++)
                    {
                        ISheet sheet = workbook.GetSheetAt(s);
                        for (int i = 1; i <= sheet.LastRowNum + 1; i++)
                        {
                            var row = sheet.GetRow(i);
                            if (row != null)
                            {
                                StringBuilder line = new StringBuilder();
                                line.Append(path + ",");
                                for (int j = 0; j < row.LastCellNum; j++)
                                {
                                    ICell cell = row.GetCell(j);
                                    if (cell != null)
                                    {
                                        line.AppendFormat("{0}{1}", cell.ToString().Trim(), ",");
                                    }
                                }
                                int sheetIndex = s + 1;
                                WriteLog("gk", "sheet" + sheetIndex + "-", line.ToString().TrimEnd(','));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                WriteLog("error", "info", path + " query error " + ex.Message);
            }
        }

 

你可能感兴趣的:(NPOI遍历excel表格)