【问题标题】:Opening excel file prompts a message box "content recovery of the workbook"打开excel文件提示消息框“工作簿内容恢复”
【发布时间】:2019-10-06 15:14:11
【问题描述】:

当我尝试打开 excel 文件时,一个消息框提示类似于 “我们发现文件名中的某些内容存在问题。您希望我们尽可能多地恢复吗?如果您信任此工作簿的来源,请单击是。”。实际完成的是我设计了一个 excel 模板并将文件复制到另一个文件并创建了临时文件我正在使用 OPEN XML 将数据插入到临时文件中,并且数据是从数据库中获取的。

我已经尝试了网络中提供的解决方案,但这些修复并没有解决我的问题。我的 excel 是 2010 年

非常感谢提供的任何解决方案。

【问题讨论】:

  • 这不是编码问题,因此无法解决。
  • 你能说出为什么无法修复吗?
  • @dineshHaraveer 你能解决这个问题吗?因为我也面临同样的问题
  • @Pratik 我无法解决这个问题。我正在等待任何人提供解决方案。我用谷歌搜索但一无所获。
  • @dineshHaraveer 这个问题我已经解决了使用正确的方法使用 openxml SDK 构建 excel 文件。我点击了这个链接blogs.msdn.com/b/chrisrae/archive/2011/08/18/…

标签: excel openxml


【解决方案1】:

我遇到了这个问题。这是由我在单元格中存储数字和字符串的方式引起的。

可以简单地使用cell.CellValue = new CellValue("5") 存储数字,但对于非数字文本,您需要将字符串插入SharedStringTable 元素并获取该字符串的索引。然后将单元格的数据类型改为SharedString,并将单元格的值设置为SharedStringTable中字符串的索引。

// Here is the text I want to add.
string text = "Non-numeric text.";

// Find the SharedStringTable element and append my text to it.
var sharedStringTable = document.WorkbookPart.GetPartsOfType<SharedStringTablePart>().First().SharedStringTable;
var item = sharedStringTable.AppendChild(new SharedStringItem(new Text(text)));

// Set the data type of the cell to SharedString.
cell.DataType = new EnumValue<CellValues>(CellValues.SharedString);

// Set the value of the cell to the index of the SharedStringItem.
cell.CellValue = new CellValue(item.ElementsBefore().Count().ToString());

这在此处的文档中进行了解释:http://msdn.microsoft.com/en-us/library/office/cc861607.aspx

【讨论】:

    【解决方案2】:

    可能导致此类错误的其他几种情况:

    • 您的工作表名称超过 31 个字符
    • 工作表名称中包含无效字符
    • 您的单元格的值超过 32k

    【讨论】:

      【解决方案3】:

      问题是由于使用

      package.Save();

      package.GetAsByteArray();

      同时

      当我们打电话时

      package.GetAsByteArray();

      它将执行以下操作

      this.Workbook.Save(); this._package.Close(); this._package.Save(this._stream);

      因此,删除

      package.Save();

      将解决此问题“我们发现文件名中的某些内容存在问题。您希望我们尽可能多地恢复吗?如果您信任此工作簿的来源,请单击是。”

      【讨论】:

        【解决方案4】:

        另一个可能的原因可能是超出了最大单元格样式数

        你可以定义:

        • .xls 工作簿中多达 4000 种样式
        • .xlsx 工作簿中多达 64000 种样式

        在这种情况下,您应该为多个单元格重复使用相同的单元格样式,而不是为每个单元格创建新的单元格样式。

        【讨论】:

          【解决方案5】:

          我添加了正确的 cellReference 并为我解决了这个问题:

          string alpha = "ABCDEFGHIJKLMNOPQRSTUVQXYZ";
          for (int colInx = 0; colInx < reader.FieldCount; colInx++)
          {
              AppendTextCell(alpha[colInx] + "1", reader.GetName(colInx), headerRow);
          }
          
          private static void AppendTextCell(string cellReference, string cellStringValue, Row excelRow)
          {
              //  Add a new Excel Cell to our Row 
              Cell cell = new Cell() { CellReference = cellReference, DataType = new EnumValue<CellValues>(CellValues.String) };
              CellValue cellValue = new CellValue();
              cellValue.Text = cellStringValue.ToString();
              cell.Append(cellValue);
              excelRow.Append(cell);
          }
          

          【讨论】:

          • 我刚刚获得了 closedXml 并且没有任何问题。它也容易得多。
          【解决方案6】:

          同样的警告,但我的问题是我使用客户端输入(波形名称)作为文件的工作表名称,并且当名称中出现日期时,用作日期部分分隔符的字符“/”导致问题。

          我认为微软需要提供更好的错误日志来节省人们调查此类小问题的时间。希望我的回答能节省别人的时间。

          【讨论】:

            【解决方案7】:

            问题是由于直接使用 cell.CellValue = new CellValue("Text") 在单元格中存储了一个字符串。可以存储这样的数字,但不能存储字符串。对于字符串,在使用 Cell.DataType = CellValues.String; 分配文本之前将数据类型定义为字符串;

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2014-09-12
              • 2017-09-25
              • 1970-01-01
              • 2010-09-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多