xlsx没有打开*某些*移动应用程序 [英] xlsx not opening in *some* mobile apps

查看:53
本文介绍了xlsx没有打开*某些*移动应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


您好, 


我有一个应用程序,通过OpenXML 2.5 SDK生成xlsx下载。  它在桌面上的Excel中打开而没有问题。验证(代码和使用Open XML 2.5生产力工具)报告没有错误。


我的用户报告称iPhone和iPad无法使用Excel或Google表格打开它。  我的Android设备使用Google表格或Excel打开它没有问题,但Polaris Office 6.5.2只显示最后一行数据。


如果我在Excel桌面上打开文件并重新保存它,我的Android现在也可以使用Polaris Office打开它。   iPad无论其默认应用是什么,都可以正常打开它。


将原始文件与重新保存的文件进行比较表明它们差别很大(使用了不同的命名空间,单元格内容已全部移动到共享字符串表等。)


似乎*某些*错误的文件未通过SDK验证检测到。  有谁知道调试这些问题的方法?  更严格的验证器?  任何见解都将非常受欢迎。



谢谢

解决方案

你好ddecar,


我试图重现这个问题但是失败了。 Open XML 2.5 SDK生成的工作簿已成功在IPhone中打开。


以下是用于创建工作簿并在工作表中插入文本以供参考的测试代码:

 class CreateWorkbookforIphone 
{

public void Main()
{
string fileName = @ " C:\book.xlsx英寸;
CreateSpreadsheetWorkbook(fileName);
InsertText(fileName," Hello");
}


//给定文档名称和文本,
//插入新工作表并将文本写入单元格"A1"。新工作表。
public static void InsertText(string docName,string text)
{
//打开文档进行编辑。
using(SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName,true))
{
//获取SharedStringTablePart。如果它不存在,请创建一个新的。
SharedStringTablePart shareStringPart;
if(spreadSheet.WorkbookPart.GetPartsOfType< SharedStringTablePart>()。Count()> 0)
{
shareStringPart = spreadSheet.WorkbookPart.GetPartsOfType< SharedStringTablePart>()。First();
}
else
{
shareStringPart = spreadSheet.WorkbookPart.AddNewPart< SharedStringTablePart>();
}

//将文本插入SharedStringTablePart。
int index = InsertSharedStringItem(text,shareStringPart);

//插入新工作表。
WorksheetPart worksheetPart = InsertWorksheet(spreadSheet.WorkbookPart);

//将单元格A1插入新工作表。
Cell cell = InsertCellInWorksheet(" A",7,worksheetPart);

//设置单元格A1的值。
cell.CellValue = new CellValue(index.ToString());
cell.DataType = new EnumValue< CellValues>(CellValues.SharedString);

//保存新工作表。
worksheetPart.Worksheet.Save();
}
}

//给定文本和SharedStringTablePart,创建一个具有指定文本
//的SharedStringItem,并将其插入SharedStringTablePart。如果该项已存在,则返回其索引。
private static int InsertSharedStringItem(string text,SharedStringTablePart shareStringPart)
{
//如果部件不包含SharedStringTable,请创建一个。
if(shareStringPart.SharedStringTable == null)
{
shareStringPart.SharedStringTable = new SharedStringTable();
}

int i = 0;

//迭代SharedStringTable中的所有项目。如果文本已存在,则返回其索引。
foreach(shareStringPart.SharedStringTable.Elements< SharedStringItem>()中的SharedStringItem项目)
{
if(item.InnerText == text)
{
return i;
}

i ++;
}

//部件中不存在该文本。创建SharedStringItem并返回其索引。
shareStringPart.SharedStringTable.AppendChild(new SharedStringItem(new DocumentFormat.OpenXml.Spreadsheet.Text(text)));
shareStringPart.SharedStringTable.Save();

返回i;
}

//给定WorkbookPart,插入一个新的工作表。
private static WorksheetPart InsertWorksheet(WorkbookPart workbookPart)
{
//在工作簿中添加一个新的工作表部分。
WorksheetPart newWorksheetPart = workbookPart.AddNewPart< WorksheetPart>();
newWorksheetPart.Worksheet = new Worksheet(new SheetData());
newWorksheetPart.Worksheet.Save();

Sheets sheets = workbookPart.Workbook.GetFirstChild< Sheets>();
string relationshipId = workbookPart.GetIdOfPart(newWorksheetPart);

//获取新工作表的唯一ID。
uint sheetId = 1;
if(sheets.Elements< Sheet>()。Count()> 0)
{
sheetId = sheets.Elements< Sheet>()。选择(s => s。 SheetId.Value).Max()+ 1;
}

string sheetName =" Sheet" + sheetId;

//附加新工作表并将其与工作簿关联。
工作表=新工作表(){Id = relationshipId,SheetId = sheetId,Name = sheetName};
sheets.Append(sheet);
workbookPart.Workbook.Save();

返回newWorksheetPart;
}
//给定列名,行索引和WorksheetPart,将单元格插入工作表。
//如果单元格已存在,则返回它。
private static Cell InsertCellInWorksheet(string columnName,uint rowIndex,WorksheetPart worksheetPart)
{
Worksheet worksheet = worksheetPart.Worksheet;
SheetData sheetData = worksheet.GetFirstChild< SheetData>();
string cellReference = columnName + rowIndex;

//如果工作表不包含具有指定行索引的行,请插入一行。
行排;
if(sheetData.Elements< Row>()。Where(r => r.RowIndex == rowIndex).Count()!= 0)
{
row = sheetData.Elements< ; Row>()。其中​​(r => r.RowIndex == rowIndex).First();
}
else
{
row = new Row(){RowIndex = rowIndex};
sheetData.Append(row);
}

//如果没有指定列名的单元格,请插入一个。
if(row.Elements< Cell>()。Where(c => c.CellReference.Value == columnName + rowIndex).Count()> 0)
{
return row.Elements< Cell>()。where(c => c.CellReference.Value == cellReference).First();
}
else
{
//根据CellReference,单元格必须按顺序排列。确定插入新单元格的位置。
Cell refCell = null;
foreach(行中的单元格。元素< Cell>())
{
if(string.Compare(cell.CellReference.Value,cellReference,true)> 0)
{
refCell = cell;
休息;
}
}

Cell newCell = new Cell(){CellReference = cellReference};
row.InsertBefore(newCell,refCell);

worksheet.Save();
返回newCell;
}
}

public static void CreateSpreadsheetWorkbook(string filepath)
{
//通过提供文件路径创建电子表格文档。
//默认情况下,AutoSave = true,Editable = true,Type = xlsx。
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument。
Create(filepath,SpreadsheetDocumentType.Workbook);

//在文档中添加WorkbookPart。
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook();

//将WorkheetPart添加到WorkbookPart。
WorksheetPart worksheetPart = workbookpart.AddNewPart< WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());

//将工作表添加到工作簿。
表格表格= spreadsheetDocument.WorkbookPart.Workbook。
AppendChild< Sheets>(new Sheets());

//附加新工作表并将其与工作簿关联。
工作表=新工作表()
{
Id = spreadsheetDocument.WorkbookPart。
GetIdOfPart(worksheetPart),
SheetId = 1,
Name =" mySheet"
};
sheets.Append(sheet);

workbookpart.Workbook.Save();

//关闭文件。
spreadsheetDocument.Close();
}

}

您是否介意分享创建电子表格的代码?此外,如果在iPhone和iPad上无法在Excel中打开,那么错误信息是什么?


问候&费


Hello,  

I have an app that generates an xlsx download through the OpenXML 2.5 SDK.  It opens in Excel on the desktop without issue. Validation (both in code and by using the Open XML 2.5 Productivity Tool) reports no errors.

My users have reported that iPhone and iPad fail to open it using either Excel or Google Sheets.  My Android device opens it using Google Sheets or Excel without issue, but Polaris Office 6.5.2 shows only the last row of data.

If I open the file on the desktop in Excel and re-save it, my Android now can open it with Polaris Office as well.  iPad is opening it properly in whatever its default app is.

Comparing the original file with the re-saved one shows that they're vastly different (different namespaces are in use, the cell contents have all been moved to the shared strings table, etc.)

It would seem that *something* is wrong with the file that isn't detected with the SDK validation.  Does anyone know of a way to debug these issues?  A more rigorous validator?  Any insight would be most appreciated.

thanks

解决方案

Hi ddecar,

I am try to reproduce the issue however failed. The workbook generated by Open XML 2.5 SDK opened in IPhone successfully.

Here is the test code for creating a workbook and insert text into the worksheet for your reference:

  class CreateWorkbookforIphone
    {

        public void Main()
        {
            string fileName = @"C:\book.xlsx";
            CreateSpreadsheetWorkbook(fileName);
            InsertText(fileName, "Hello");
        }


        // Given a document name and text, 
        // inserts a new worksheet and writes the text to cell "A1" of the new worksheet.
        public static void InsertText(string docName, string text)
        {
            // Open the document for editing.
            using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName,true))
            {
                // Get the SharedStringTablePart. If it does not exist, create a new one.
                SharedStringTablePart shareStringPart;
                if (spreadSheet.WorkbookPart.GetPartsOfType<SharedStringTablePart>().Count() > 0)
                {
                    shareStringPart = spreadSheet.WorkbookPart.GetPartsOfType<SharedStringTablePart>().First();
                }
                else
                {
                    shareStringPart = spreadSheet.WorkbookPart.AddNewPart<SharedStringTablePart>();
                }

                // Insert the text into the SharedStringTablePart.
                int index = InsertSharedStringItem(text, shareStringPart);

                // Insert a new worksheet.
                WorksheetPart worksheetPart = InsertWorksheet(spreadSheet.WorkbookPart);

                // Insert cell A1 into the new worksheet.
                Cell cell = InsertCellInWorksheet("A", 7, worksheetPart);

                // Set the value of cell A1.
                cell.CellValue = new CellValue(index.ToString());
                cell.DataType = new EnumValue<CellValues>(CellValues.SharedString);

                // Save the new worksheet.
                worksheetPart.Worksheet.Save();
            }
        }

        // Given text and a SharedStringTablePart, creates a SharedStringItem with the specified text 
        // and inserts it into the SharedStringTablePart. If the item already exists, returns its index.
        private static int InsertSharedStringItem(string text, SharedStringTablePart shareStringPart)
        {
            // If the part does not contain a SharedStringTable, create one.
            if (shareStringPart.SharedStringTable == null)
            {
                shareStringPart.SharedStringTable = new SharedStringTable();
            }

            int i = 0;

            // Iterate through all the items in the SharedStringTable. If the text already exists, return its index.
            foreach (SharedStringItem item in shareStringPart.SharedStringTable.Elements<SharedStringItem>())
            {
                if (item.InnerText == text)
                {
                    return i;
                }

                i++;
            }

            // The text does not exist in the part. Create the SharedStringItem and return its index.
            shareStringPart.SharedStringTable.AppendChild(new SharedStringItem(new DocumentFormat.OpenXml.Spreadsheet.Text(text)));
            shareStringPart.SharedStringTable.Save();

            return i;
        }

        // Given a WorkbookPart, inserts a new worksheet.
        private static WorksheetPart InsertWorksheet(WorkbookPart workbookPart)
        {
            // Add a new worksheet part to the workbook.
            WorksheetPart newWorksheetPart = workbookPart.AddNewPart<WorksheetPart>();
            newWorksheetPart.Worksheet = new Worksheet(new SheetData());
            newWorksheetPart.Worksheet.Save();

            Sheets sheets = workbookPart.Workbook.GetFirstChild<Sheets>();
            string relationshipId = workbookPart.GetIdOfPart(newWorksheetPart);

            // Get a unique ID for the new sheet.
            uint sheetId = 1;
            if (sheets.Elements<Sheet>().Count() > 0)
            {
                sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
            }

            string sheetName = "Sheet" + sheetId;

            // Append the new worksheet and associate it with the workbook.
            Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
            sheets.Append(sheet);
            workbookPart.Workbook.Save();

            return newWorksheetPart;
        }
        // Given a column name, a row index, and a WorksheetPart, inserts a cell into the worksheet. 
        // If the cell already exists, returns it. 
        private static Cell InsertCellInWorksheet(string columnName, uint rowIndex, WorksheetPart worksheetPart)
        {
            Worksheet worksheet = worksheetPart.Worksheet;
            SheetData sheetData = worksheet.GetFirstChild<SheetData>();
            string cellReference = columnName + rowIndex;

            // If the worksheet does not contain a row with the specified row index, insert one.
            Row row;
            if (sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).Count() != 0)
            {
                row = sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).First();
            }
            else
            {
                row = new Row() { RowIndex = rowIndex };
                sheetData.Append(row);
            }

            // If there is not a cell with the specified column name, insert one.  
            if (row.Elements<Cell>().Where(c => c.CellReference.Value == columnName + rowIndex).Count() > 0)
            {
                return row.Elements<Cell>().Where(c => c.CellReference.Value == cellReference).First();
            }
            else
            {
                // Cells must be in sequential order according to CellReference. Determine where to insert the new cell.
                Cell refCell = null;
                foreach (Cell cell in row.Elements<Cell>())
                {
                    if (string.Compare(cell.CellReference.Value, cellReference, true) > 0)
                    {
                        refCell = cell;
                        break;
                    }
                }

                Cell newCell = new Cell() { CellReference = cellReference };
                row.InsertBefore(newCell, refCell);

                worksheet.Save();
                return newCell;
            }
        }

        public static void CreateSpreadsheetWorkbook(string filepath)
        {
            // Create a spreadsheet document by supplying the filepath.
            // By default, AutoSave = true, Editable = true, and Type = xlsx.
            SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.
                Create(filepath, SpreadsheetDocumentType.Workbook);

            // Add a WorkbookPart to the document.
            WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
            workbookpart.Workbook = new Workbook();

            // Add a WorksheetPart to the WorkbookPart.
            WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
            worksheetPart.Worksheet = new Worksheet(new SheetData());

            // Add Sheets to the Workbook.
            Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.
                AppendChild<Sheets>(new Sheets());

            // Append a new worksheet and associate it with the workbook.
            Sheet sheet = new Sheet()
            {
                Id = spreadsheetDocument.WorkbookPart.
                    GetIdOfPart(worksheetPart),
                SheetId = 1,
                Name = "mySheet"
            };
            sheets.Append(sheet);

            workbookpart.Workbook.Save();

            // Close the document.
            spreadsheetDocument.Close();
        }

    }

Would you mind sharing the code how you created the spreadsheet? Also if it is failed to open in Excel on iPhone and iPad, what's the error message?

Regards & Fei


这篇关于xlsx没有打开*某些*移动应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆