Repaired Records : Cell information from worksheet created from scratch

微笑、不失礼 提交于 2019-12-03 04:46:27

问题


I'm receiving an error when opening my OpenXML created spreadsheet. The error is as follows.

Repaired Records: Cell information from /xl/worksheets/sheet.xml part
Repaired Records: Cell information from /xl/worksheets/sheet2.xml part
Repaired Records: Cell information from /xl/worksheets/sheet3.xml part

The only thing I could find online that was helpful was this issue was the discussion of an algorithm which alters an individual cell multiple times causing the issue. Having said that, I'm going to link my Constructor for the SpreadsheetDocument as well as the three functions for updating a cell (which I do once).

I can supply any additional functions as needed, but I believe the problem is somewhere in the two listed below.

By the way,

 GetWorksheetPartByName
 InsertCellInWorksheet
 GetCell

should all working as intended.

The Actual Program

static void Main(string[] args)
    {
        //Full path for File
        const string newFile = "@C:\test.xlsx";

        //Constructor creates default worksheet called "mySheet"
        var spreadsheet = new XLSXHelper(newFile);

        //updating some cells.
        spreadsheet.UpdateCell("mySheet", "D2", "R", 2);
    }

Constructor

    public XLSXHelper(string filepath)
    {
        newFile = filepath;
        spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);
        this.workbookPart = spreadsheetDocument.AddWorkbookPart();
        workbookPart.Workbook = new Workbook();
        this.worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
        worksheetPart.Worksheet = new Worksheet(new SheetData());
        Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.
        AppendChild<Sheets>(new Sheets());
        Sheet sheet = new Sheet()
        {
            Id = spreadsheetDocument.WorkbookPart.
                GetIdOfPart(worksheetPart),
            SheetId = 1,
            Name = "mySheet"
        };
        sheets.Append(sheet);
        workbookPart.Workbook.Save();
        spreadsheetDocument.Close();
    }

Update Cell

    public void UpdateCell(string worksheetName, string textToInsert, string columnName, uint rowIndex)
    {
        using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(newFile, true))
        {
            WorksheetPart worksheetPart = GetWorksheetPartByName(spreadSheet, worksheetName);
            if (worksheetPart != null)
            {
                InsertCellInWorksheet(columnName, rowIndex, worksheetPart);
                Cell cell = GetCell(worksheetPart.Worksheet,columnName, rowIndex);
                cell.CellValue = new CellValue(textToInsert);
                worksheetPart.Worksheet.Save();
            }
        }
    }

回答1:


If you are adding a string to a cell rather than a number (or a string that can be converted to a number) then you should use an inline string or a shared string instead of the CellValue. You can only use CellValue if the value is numeric.

The XML generated when using CellValue looks something like:

<x:row>
  <x:c>
    <x:v>12345</x:v>
  </x:c>
</x:row>

when you use an inline string it looks like:

<x:row>
  <x:c t="inlineStr">
    <x:is>
      <x:t>Foo</x:t>
    </x:is>
  </x:c>
</x:row>

note the "is" node for inline string and that the cell type attribute is set to "inlineStr".

Here is C# code to generate correct XML for a cell containing text:

cell.DataType = CellValues.InlineString;
cell.InlineString = new InlineString() { Text = new Text(textToInsert) };

From what I have read using shared strings is preferable but using inline strings avoids the error and looks just fine when you open the file in Excel.




回答2:


I realize this is an old thread, but I just had the same error message pop up for a programmatically generated Excel sheet also. My issue was that I had been setting the name of the worksheet with a name that had a forward slash in it.

Hopefully this helps someone else who stumbles on this thread as it's the most popular thread on the topic.




回答3:


Another late one - but check how you're adding cells to a row. Have you cut and paste add cell code in which there's a 'simple' compare with the existing cell reference (A1 etc) in the row? In which case if you have a cell beyond column Z - AA1 onwards - then you might end up trying to insert cell (eg) AB1) before cell B1. You'll then get this error on opening the written sheet in excel. Instead, if simply adding cell after cell along each row, just go straight to insert before with the reference cell set to null - ie. add new cell to end.

Hope that makes sense.




回答4:


Stumbled on this issue myself and found this thread. Difference in my case could not repair the file. Issue was row index which I passed to InsertCellInWorksheet taken from example on MSDN.

Cell cell = InsertCellInWorksheet("A", lastRow.RowIndex, worksheetPart);

When inserting a row, make sure the index of the row is 1, not 0. However, 0 for the SheetData collection.

sheetData.InsertAt(new Row() { RowIndex = 1 }, 0);
lastRow = sheetData.Elements<Row>().LastOrDefault();

In hope to save someone time who goes through the same trouble.

Interestingly enough, my integration test passed, however, when opening the file via Excel, it was showing the pop-up about corruption. Seems there is a row with index 0, however, bit secretive as you will not be able to open a file with Excel.




回答5:


Adding to the list of potential solutions (Year2017 :) ) :

I was facing this issue because I was not using the correct workbook object to write tothe FileOutputStream.

Hope this helps someone.




回答6:


It's an old thread, however I had the same problem and it might be useful to someone like me who landed on this thread to find answer :)

The problem with me was that my application has an export module, and exported file from one environment was working fine, however from other it was not. It was the same error:

Repaired Records: Cell information from /xl/worksheets/sheet19.xml part

After doing a deep investigation, I found out that for one of the records in this sheet exceeding the cell limit of 32767 chars (excel limitation) and by reducing the size of texts on DB directly, solved the problem. :)

I hope it will help :)




回答7:


In my case, the SSRS place holder properties had custom number.

Fix:

  1. GO to all the fields one by one in SSRS designer.
  2. See if there are any alpha numeric values coming from backend. ( cross compare your data set with all the cells in the SSRS report.
  3. double click on the field -> go to 'Number' tab
  4. Click on the default -> ok

Thanks Umakanth Nelige



来源:https://stackoverflow.com/questions/9401508/repaired-records-cell-information-from-worksheet-created-from-scratch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!