使用NPOI编辑和保存xlsx会导致文件损坏

FileStream s = new FileStream(@myPath, FileMode.Open, FileAccess.ReadWrite); IWorkbook wb = new XSSFWorkbook(s); s.Close(); ISheet isheet = wb.GetSheetAt(0); IRow irow = isheet.CreateRow(0); irow.CreateCell(0).SetCellValue("foo"); s = new FileStream(@myPath + "blah.xlsx", FileMode.Create); wb.Write(s); s.Close(); 

新文件被创build但损坏。 我见过有人说这是在2.0.6版本中修复的,但仍然不适合我。

看起来您正在使用@myPath作为您的代码的第1行上的共享/文件名创build一个stream,但您将文件名连接到@myPath保存该文件。 这可能是错误的来源吗?