C#读取Excel表格中的内容

随手记录,我自己也搞不明白。直接拿去用了

public static string ReadXLSX(string filePath)
        {
            string sb="";
            using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                var hssfworkbook = new XSSFWorkbook(file);
                var sheet = hssfworkbook.GetSheetAt(0);
                var rows = sheet.GetRowEnumerator();
                while (rows.MoveNext())
                {
                    var row = (XSSFRow)rows.Current;
                    for (var i = 0; i < row.LastCellNum; i++)
                    {
                        string cell = row.GetCell(i).ToString();
                        sb += "\r\n" + cell;
                    }
                }
            }
            return sb;
        }

 

posted @ 2022-10-28 16:54  给自己个晚安  阅读(110)  评论(0编辑  收藏  举报