16

While I'm trying to open excel file a message box is prompting like "We found a problem with some content in file name. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes.". What actually done is i have a excel template designed and copying the file to another file and created temp file I'm inserting data to temp file using OPEN XML and data is getting from the database.

i have tried the solutions provided in the net but those fixes are not resolving my issue.My excel is 2010

enter image description here

enter image description here

Anyone solution provided is much appreciated.

Dinesh Haraveer
  • 1,784
  • 3
  • 31
  • 54

7 Answers7

9

I had this problem. It was caused by the way I was storing numbers and strings in cells.

Numbers can be stored simply using cell.CellValue = new CellValue("5"), but for non-numeric text, you need to insert the string in the SharedStringTable element and get the index of that string. Then change the data type of the cell to SharedString, and set the value of the cell to the index of the string in the 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());

This is explained in the documentation here: http://msdn.microsoft.com/en-us/library/office/cc861607.aspx

Boric
  • 822
  • 7
  • 26
5

Another few cases that can cause this type of error:

  • Your sheet name is longer than 31 characters
  • You have invalid characters in sheet name
  • You have cells with values longer than 32k
Rory
  • 40,559
  • 52
  • 175
  • 261
2

The issue is due to using

package.Save();

and

package.GetAsByteArray();

at the same time

when we call

package.GetAsByteArray();

it will do following operations

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

Hence, removing

package.Save();

will solve this problem "We found a problem with some content in file name. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes."

Hari
  • 117
  • 4
0

Another possible cause could be exceeded maximum number of cell styles.

You can define:

  • up to 4000 styles in a .xls workbook
  • up to 64000 styles in a .xlsx workbook

In this case you should re-use the same cell style for multiple cells, instead of creating a new cell style for every cell.

lu_ko
  • 4,055
  • 2
  • 26
  • 31
0

I added the right cellReference and fixed this issue for me:

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);
}
Bridge
  • 29,818
  • 9
  • 60
  • 82
Yuri
  • 1
0

Same warning but the problem with me was that I was using a client input (name of wave) as sheet name for the file and when date was presented within the name, the character '/' used as date part separator was causing the issue.

I think Microsoft need to provide a better error log to save people time investigate such minor issues. Hope my answer will save someone else's time.

0

The issue was due to storing a string in the cell directly using cell.CellValue = new CellValue("Text"). It is possible to store numbers like this but not string. For string, define data type as string before assigning the text using Cell.DataType = CellValues.String;