简体   繁体   English

SharePoint 2010“错误”打开通过C#创建的Excel

[英]SharePoint 2010 “error” opening Excel that was created through C#

Here's the situation: on a certain page within SharePoint, a user clicks a button in a webpart to create a report based on data in SharePoint. 情况如下:在SharePoint中的某个页面上,用户单击Web部件中的按钮以基于SharePoint中的数据创建报表。 This report is an Excel document that is created through C# code. 此报告是通过C#代码创建的Excel文档。 When the report is created, it will be uploaded to SharePoint. 创建报告后,它将被上传到SharePoint。 When the upload is done, the file will be automatically opened for the user so the file can be saved locally or printed or some other action. 上传完成后,文件将自动为用户打开,因此可以将文件保存在本地或打印或执行其他操作。

Here's a simplified version of how my code works: 这是我的代码的简化版本:

//Prepare the data in a table
//Normally there are multiple rows and columns here
var fileName = "Report.xlsx";
var table = new System.Web.UI.WebControls.Table();
table.Rows.Add(new TableRow());
table.Rows[0].Cells.Add(new TableCell());
table.Rows[0].Cells[0].Text = "Some cell value";
CreateWorkbook("My Report", fileName, table);

private void CreateWorkbook(string name, string fileName, Table table)
{
    using (var doc = SpreadsheetDocument.Create(_tempfolder + fileName, SpreadsheetDocumentType.Workbook))
    {
        var sheetData = new SheetData();
        var excelRow = new Row { RowIndex = 1 };

        sheetData .AppendChild(excelRow);

        //Normally there's a loop here going over all the rows and columns
        var tableCellValue = table.Rows[0].Cells[0].Text;
        var excelCell = CreateTextCell("A", 1, tableCellValue);
        excelRow.AppendChild(excelCell);

        doc.AddWorkbookPart().AddNewPart<WorksheetPart>().Worksheet = new Worksheet(sheetData);
        doc.WorkbookPart.Workbook = new Workbook(
            new Sheets(
                new Sheet
                {
                    Id = doc.WorkbookPart.GetIdOfPart(doc.WorkbookPart.WorksheetParts.First()),                    
                    SheetId = 1,
                    Name = name
                }));
        doc.WorkbookPart.Workbook.Save();
        doc.Close();

        //Upload the file to SharePoint
        //Omitted as this is not the issue here
    }
}

private static Cell CreateTextCell(string col, int row, string cellValue)
{
    var cell = new Cell { DataType = new EnumValue<CellValues>(CellValues.InlineString), CellReference = col + row };
    var inlineString = new InlineString();
    var text = new Text { Text = value };

    inlineString.Append(l_objText);
    cell.Append(inlineString);

    return cell;
}

Everything in the code works fine and the Excel-file is succesfully created and uploaded and after all this Excel opens up and then I get following message: 代码中的所有内容均正常运行,并且成功创建并上传了Excel文件,并且在打开所有此Excel之后,我得到以下消息:

在此处输入图片说明

Excel found unreadable content in '...'. Excel在“ ...”中发现了不可读的内容。 Do you want to recover the contents of this workbook? 您要恢复此工作簿的内容吗? If you trust the source of this workbook, click Yes. 如果您信任此工作簿的来源,请单击“是”。

When I click Yes, Excel magically repairs and opens the content and everything is shown as normal. 当我单击“是”时,Excel将神奇地修复并打开内容,所有内容均显示为正常。 But where does this error come from? 但是这个错误是从哪里来的呢? I have looked on the internet for this problem and in almost all the cases the source of this problem are macros, formulas and other exotic Excel-stuff. 我已经在互联网上找到了这个问题,在几乎所有情况下,这个问题的根源都是宏,公式和其他奇特的Excel资料。 But in this case, only one cell is added with just a string-value. 但是在这种情况下,仅添加一个带有字符串值的单元格。

Does anyone know how to solve this issue? 有谁知道如何解决这个问题?

Edit: 编辑:

Opening the file in Notepad is not a solution as this gives output like this: 在记事本中打开文件不是解决方案,因为这会产生以下输出:

PK ©QiAÙ9 1A A xl/workbook.xml ¢ ( í½ I–%&/mÊ{JõJ×àt¡€ $Ø@ìÁˆÍæ'ìiG#)« ÊeVe]f@Ìí¼÷Þ{ï½÷Þ{ï½÷º;N'÷ßÿ?\\fdlöÎJÚÉž!€ªÈ?~|?"þÇ¿÷|ü{¼[”ée^7Eµüì£ÝñÎGi¾œV³byñÙGëö|ûà£ßãèñ»GWUývRUoSj¿l½ûì£yۮݽÛLçù"kÆÕ _ÒwçU½ÈZú³¾¸Û¬ê<›5óš·íêÑÝ»Ítž/²f\\ò%}w^Õ‹¬ PK©QiAÙ91A甲XL / workbook.xml¢(í½ I–%&/mÊ{JõJ×àt¡€ $Ø@ìÁÍæ'ìiG#)«ÊeVeF的@Ìí¼÷Þ{ï½÷Þ{ï½÷º; N'÷ßÿ?\\fdlöÎJÚÉž!€ªÈ?〜|?þ 5óš·íêÑÝ»Ítž/²f\\ò%} w ^Õ‹¬

The error "sneaks" into the file upon creation. 错误在创建时“潜入”文件。 When I create the file I save it in a temporary folder and from there I upload the file to SharePoint. 创建文件时,我将其保存在一个临时文件夹中,然后从那里将文件上传到SharePoint。 Both the temp-file and the SharePoint-file contain the error, so the error must occur upon creation of the file. 临时文件和SharePoint文件都包含错误,因此该错误必须在创建文件时发生。 How is it possible that even adding a single text value to one single cell creates a corrupted document? 甚至将单个文本值添加到一个单元格中,怎么可能会损坏文档?

Thanks to this article I found the problem. 由于这篇文章,我发现了问题。 With the OpenXmlValidator class you can easily find errors in a created file. 使用OpenXmlValidator类,您可以轻松地在创建的文件中查找错误。 In my case the problem was that the title/name of one of my sheets was longer than 31 characters and 31 seems to be the limit. 在我的情况下,问题是我的一张纸的标题/名称超过31个字符,而31个似乎是限制。 Problem solved, so for anyone with a similar issue, I strongly recommend the use of the validator! 问题已解决,因此对于有类似问题的任何人,我强烈建议您使用验证器! ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM