写入 Excel:无法使用 EPPLUS 访问已关闭的流

Writing to Excel: Cannot access closed stream with EPPLUS

我环顾四周,在大多数情况下,我看到了比我自己的问题更复杂的例子。

因此,由于性能改进,我被建议使用 EPPLUS 而不是 EXCEL INTEROP。这是我第一次使用它,也是我第一次遇到内存流,所以我不太确定这里出了什么问题。 我正在尝试写入 Excel 文件并将该 excel 文件转换为 PDF。为此,我通过 NUGET 安装了以下内容:

  1. EPPLUS
  2. EPPLUSExcel

这是我的代码:

       if (DGVmain.RowCount > 0)
        {
            //Source
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Excel Files|*.xls;*.xlsx";
            openFileDialog.ShowDialog();
            lblSuccess.Text = openFileDialog.FileName;
            lblPathings = Path.ChangeExtension(openFileDialog.FileName, null);
            int count = DGVmain.RowCount;
            int current = 0;
            int ballast = 0;

对于 DataGridView 中的每一行,执行写入 Excel,然后转换为 PDF。

            foreach (DataGridViewRow row in DGVmain.Rows)
            {
                //Drag
                if (lblSuccess.Text == null)
                    return;
                string drags = Convert.ToString(row.Cells[0].Value);
                string dragsy = Convert.ToString(row.Cells[1].Value);
                Persona = drag;
                generateID();
                //Initialize the Excel File
                try
                {

这是我预计会出现问题的地方:

                    using (ExcelPackage p = new ExcelPackage())
                    {
                        using (FileStream stream = new FileStream(lblSuccess.Text, FileMode.Open))
                        {
                            ballast++;
                            lblItem.Text = "Item #" + ballast;
                            p.Load(stream);
                            ExcelWorkbook WB = p.Workbook;
                            if (WB != null)
                            {
                                if (WB.Worksheets.Count > 0)
                                {
                                    ExcelWorksheet WS = WB.Worksheets.First();
                                    WS.Cells[82, 12].Value = drag13;
                                    WS.Cells[84, 12].Value = "";
                                    WS.Cells[86, 12].Value = 0;
                                    //========================== Form
                                    WS.Cells[95, 5].Value = drag26;
                                    WS.Cells[95, 15].Value = drag27;
                                    WS.Cells[95, 24].Value = drag28;
                                    WS.Cells[95, 33].Value = drag29;
                                    //========================== Right-Seid
                                    WS.Cells[14, 31].Value = drag27;
                                    WS.Cells[17, 31].Value = drag27;

                                }
                            }
                            Byte[] bin = p.GetAsByteArray();
                            File.WriteAllBytes(lblPathings, bin);
                        }
                        p.Save();
                    }                       
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Write Excel: " + ex.Message);
                }

使用 EPPLUSEXCEL 和 SpireXLS 转换为 PDF 的单独方法。

                finally
                {
                    ConvertToPdf(lblSuccess.Text, finalformat);
                }
            }
        }

除标题中提到的错误外,编译器未抛出任何错误。

您已经在此处保存了 ExcelPackage

Byte[] bin = p.GetAsByteArray();

因此,当您稍后尝试在此处再次保存它时:

p.Save();

ExcelPackage 已经关闭。 IE。删除代码中的 Save() 调用就可以了。