NPOI在创建或附加数据时损坏的文件

发布于 2025-02-07 16:20:35 字数 3562 浏览 0 评论 0原文

我正在尝试在C#应用程序中使用 npoi 创建 .xlsx 。我遇到了一些最初没有遇到的奇怪错误。我测试了此代码的前几次,我收到了一个缺失的异常。我试图搜索说明,但建议的所有内容都失败了(包括删除包装文件夹并重新加载所有软件包)。

这是我的WriteExcel方法:

public void WriteExcel()
        {
            System.IO.Directory.CreateDirectory(workingDirectory + @"\Excel");
            var newFile = workingDirectory + @"\Excel\data.xlsx";

            try
            {
                if (Directory.Exists(workingDirectory + @"\Excel"))
                {
                    if (!File.Exists(newFile))
                    {
                        using (var fs = new FileStream(newFile, FileMode.Create, FileAccess.Write))
                        {
                            IWorkbook workbook = new XSSFWorkbook();
                            ISheet sheet = workbook.CreateSheet("Data");
                            IRow row = sheet.CreateRow(0);
                            ICell cell1 = row.CreateCell(0);
                            ICell cell2 = row.CreateCell(1);
                            ICell cell3 = row.CreateCell(2);
                            cell1.SetCellValue("Date");
                            cell2.SetCellValue("Value1");
                            cell3.SetCellValue("Value2");

                            for (int i = 0; i <= 20; i++) sheet.AutoSizeColumn(i);

                            workbook.Write(fs);
                        }

                        Console.WriteLine("File is created");
                    }
                    else
                    {
                        IWorkbook workbook;

                        using (var outs = new FileStream(newFile, FileMode.Open, FileAccess.Read))
                        {
                            workbook = new XSSFWorkbook(outs);
                            outs.Close();
                            Console.WriteLine("Workbook read");
                        }

                        ISheet sheet = workbook.GetSheetAt(0);
                        IRow row = sheet.CreateRow(sheet.LastRowNum+1);
                        ICell cell1 = row.CreateCell(0);
                        ICell cell2 = row.CreateCell(1);
                        ICell cell3 = row.CreateCell(2);
                        cell1.SetCellValue(DateTime.Today.Date.ToString("yyyy-MM-dd"));
                        cell2.SetCellValue("Some value");
                        cell3.SetCellValue("some value 2");
                        Console.WriteLine("Values set");

                        using (var ins = new FileStream(newFile, FileMode.Open, FileAccess.Write))
                        {
                            workbook.Write(ins);
                            ins.Close();
                            Console.WriteLine("Workbook saved");

                        }
                    }
                }
                else
                {
                    // We tried creating the directory and it still doesn't exist. Something is wrong, send error.
                }
            }
            catch (Exception e) { Console.WriteLine(e); }
        }

文件是在正确的文件夹中创建的,但文件为0字节,可能是由于各种错误所致。即使我没有遇到错误(我会进一步将它们附加),无论如何,文件都会损坏。 Excel给出了一个错误,然后让我可以选择尝试恢复受损文件的某些数据。

有我的错误:

Exception thrown: 'System.IO.FileNotFoundException' in NPOI.OpenXml4Net.dll
Exception thrown: 'NPOI.POIXMLException' in NPOI.OOXML.dll
System.IO.FileNotFoundException: Det går inte att läsa in filen eller sammansättningen ICSharpCode.SharpZipLib

我目前被困。我试图最大程度地减少代码量,因此仅创建一个单元格。我没有收到以前没有找到文件的错误。我不确定它找不到什么,我是否缺少某种包裹或其他东西?

先感谢您!

I'm trying to create an .xlsx file using NPOI in a C# application. I'm getting some weird errors that I did not initially get. The first few times I tested this code I received a MissingMethod exception. I tried to search for an explanation but everything that was suggested failed (including removing the package folder and re-loading all of the packages).

This is my WriteExcel method:

public void WriteExcel()
        {
            System.IO.Directory.CreateDirectory(workingDirectory + @"\Excel");
            var newFile = workingDirectory + @"\Excel\data.xlsx";

            try
            {
                if (Directory.Exists(workingDirectory + @"\Excel"))
                {
                    if (!File.Exists(newFile))
                    {
                        using (var fs = new FileStream(newFile, FileMode.Create, FileAccess.Write))
                        {
                            IWorkbook workbook = new XSSFWorkbook();
                            ISheet sheet = workbook.CreateSheet("Data");
                            IRow row = sheet.CreateRow(0);
                            ICell cell1 = row.CreateCell(0);
                            ICell cell2 = row.CreateCell(1);
                            ICell cell3 = row.CreateCell(2);
                            cell1.SetCellValue("Date");
                            cell2.SetCellValue("Value1");
                            cell3.SetCellValue("Value2");

                            for (int i = 0; i <= 20; i++) sheet.AutoSizeColumn(i);

                            workbook.Write(fs);
                        }

                        Console.WriteLine("File is created");
                    }
                    else
                    {
                        IWorkbook workbook;

                        using (var outs = new FileStream(newFile, FileMode.Open, FileAccess.Read))
                        {
                            workbook = new XSSFWorkbook(outs);
                            outs.Close();
                            Console.WriteLine("Workbook read");
                        }

                        ISheet sheet = workbook.GetSheetAt(0);
                        IRow row = sheet.CreateRow(sheet.LastRowNum+1);
                        ICell cell1 = row.CreateCell(0);
                        ICell cell2 = row.CreateCell(1);
                        ICell cell3 = row.CreateCell(2);
                        cell1.SetCellValue(DateTime.Today.Date.ToString("yyyy-MM-dd"));
                        cell2.SetCellValue("Some value");
                        cell3.SetCellValue("some value 2");
                        Console.WriteLine("Values set");

                        using (var ins = new FileStream(newFile, FileMode.Open, FileAccess.Write))
                        {
                            workbook.Write(ins);
                            ins.Close();
                            Console.WriteLine("Workbook saved");

                        }
                    }
                }
                else
                {
                    // We tried creating the directory and it still doesn't exist. Something is wrong, send error.
                }
            }
            catch (Exception e) { Console.WriteLine(e); }
        }

The file is created in the correct folder but the file is 0 bytes, probably due to the various errors. Even when I did not get the errors (I'll append them further down) the files were corrupted anyway. Excel gave an error and then gave me the option to try and restore some of the data of the damaged file.

There are my errors:

Exception thrown: 'System.IO.FileNotFoundException' in NPOI.OpenXml4Net.dll
Exception thrown: 'NPOI.POIXMLException' in NPOI.OOXML.dll
System.IO.FileNotFoundException: Det går inte att läsa in filen eller sammansättningen ICSharpCode.SharpZipLib

I'm currently stuck. I've tried to minimize the amount of code so it only creates one cell. I did not receive errors about not finding the file previously. I'm not sure what it is it cant find, am I missing some kind of package or something?

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那些过往 2025-02-14 16:20:35

遇到相同的问题(从0.86更新到1.42),在将NPOI软件包更新为2.7.1之后,该问题已解决。

Run into the same problem (updating from 0.86 to 1.42), after an update of the NPOI package to 2.7.1 the problem was solved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文