【问题标题】:Why does my WorkbookView sequence keep returning "null" value with OpenXml?为什么我的 WorkbookView 序列使用 OpenXml 不断返回“null”值?
【发布时间】:2019-09-30 16:35:27
【问题描述】:

我在 C#/asp.net 中创建了一个下载按钮,它采用 GridView,将其转换为数据表,然后将其存储到 .xlsx 文件中。之后我想做的是能够更改我创建的电子表格的活动选项卡。

下面是代码:

 protected void downloadBtn_Click(object sender, EventArgs e)
    {

        using (var spreadSheet = SpreadsheetDocument.Create(Server.MapPath("~/Downloads/TestSheet.xlsx"), DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
        {
            WorkbookPart workbookPart = spreadSheet.AddWorkbookPart();
            workbookPart.Workbook = new Workbook();

            WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
            worksheetPart.Worksheet = new Worksheet(new SheetData());

            //Fine up to this point

            spreadSheet.WorkbookPart.Workbook.Sheets = new Sheets();

            DataTable table = new DataTable();
            //Converts GridView into Data Table **
            for (int i = 0; i < gvEmployee.HeaderRow.Cells.Count - 1; i++)
            {

                table.Columns.Add(gvEmployee.HeaderRow.Cells[i + 1].Text);
            }
            // fill rows     
            for (int i = 0; i < gvEmployee.Rows.Count; i++)
            {
                DataRow dr = table.NewRow();
                for (int j = 0; j < gvEmployee.Columns.Count - 1; j++)
                {
                    dr[j] = gvEmployee.Rows[i].Cells[j + 1].Text;
                }
                table.Rows.Add(dr);

            }

            var sheetPart = spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();
            var sheetData = new SheetData();
            sheetPart.Worksheet = new Worksheet(sheetData);

            Sheets sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild<Sheets>();
            string relationshipId = spreadSheet.WorkbookPart.GetIdOfPart(sheetPart);

            uint sheetId = 1;
            if (sheets.Elements<Sheet>().Count() > 0)
            {
                sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
            }

            Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = "TestSheet" };
            sheets.Append(sheet);

            Row headerRow = new Row();
            List<String> columns = new List<string>();
            foreach (DataColumn column in table.Columns)
            {

                columns.Add(column.ColumnName);

                Cell cell = new Cell();
                cell.DataType = CellValues.String;
                cell.CellValue = new CellValue(column.ColumnName);
                headerRow.AppendChild(cell);
            }


            sheetData.AppendChild(headerRow);
            foreach (DataRow dsrow in table.Rows)
            {
                Row newRow = new Row();
                foreach (String col in columns)
                {
                    Cell cell = new Cell();
                    cell.DataType = CellValues.String;
                    cell.CellValue = new CellValue(dsrow[col].ToString()); //
                    newRow.AppendChild(cell);
                }

                sheetData.AppendChild(newRow);
            }


            Sheet sheet2 = new Sheet() { Id = spreadSheet.WorkbookPart.GetIdOfPart(sheetPart), SheetId = 2, Name = "AdditionalSheet" };
            sheets.Append(sheet2);

            var sheetIndex = workbookPart.Workbook.Descendants<Sheet>().ToList().IndexOf(sheet2);
            WorkbookView workbookView = workbookPart.Workbook.Descendants<WorkbookView>().FirstOrDefault(); //new WorkbookView();
            workbookView.ActiveTab = Convert.ToUInt32(sheetIndex);

            workbookPart.Workbook.Save();
            spreadSheet.Close();

        }
    }

当我尝试运行此代码时,它会在“workbookView.ActiveTab”的最后几行抛出一个错误,说明

System.NullReferenceException: '对象引用未设置为对象的实例。'

workbookView 为空。

关于它为什么这样做的任何想法?

【问题讨论】:

  • 我认为您正在创建一个新工作簿,而不是将工作表放入现有工作簿中。
  • 好的,我刚刚尝试关闭电子表格并使用另一个“使用语句”重新打开它,并尝试在该语句中选择活动选项卡,但它仍然返回空值。任何其他提示将不胜感激!
  • 您是打开一两个工作簿吗?
  • 我只得到一个名为 TestSheet.xlsx 的工作簿,因为它在路径中命名。当我注释掉尝试选择活动选项卡的部分时,它会使用“TestSheet”和“AdditionalSheet”成功地将工作簿保存到服务器,但是当我取消注释该部分时,它会显示“workbookView is null”。
  • 在发生错误的地方设置一个断点,然后检查打开的 excel 应用程序并确认只打开了一个工作簿。通过单击任务栏上的 excel 图标。

标签: c# html asp.net excel openxml


【解决方案1】:

好的,我刚刚弄清楚出了什么问题,没有初始化 WorkbookView,因为我认为它认为它不需要。但是您必须先创建工作簿的 WorkbookView,然后才能对其进行更改。我只需要这一行:

            workbookPart.Workbook.Append(new BookViews(new WorkbookView()));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    相关资源
    最近更新 更多