openxml 2.5, how to insert a string into a cell?

I have been trying for a couple of days now to insert a string into an openxml spreadsheet. Everything else (so far) works, everything but that.

This is the code i'm currently running (note, this is purely for testing purposes and is pretty basic):

using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Create(file + "test.zip", SpreadsheetDocumentType.Workbook))
{
   spreadSheet.AddWorkbookPart();
   spreadSheet.WorkbookPart.Workbook = new Workbook();

   spreadSheet.WorkbookPart.AddNewPart<SharedStringTablePart>();
   spreadSheet.WorkbookPart.SharedStringTablePart.SharedStringTable = new SharedStringTable() {Count=1, UniqueCount=1};

   spreadSheet.WorkbookPart.SharedStringTablePart.SharedStringTable.AppendChild(new SharedStringItem(new Text("test")));                
   spreadSheet.WorkbookPart.SharedStringTablePart.SharedStringTable.Save();

   preadSheet.WorkbookPart.AddNewPart<WorksheetPart>();
   spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet = new Worksheet();                

   spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.AppendChild(new SheetData());
   spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.First().AppendChild(new Row());

   Row r2 = new Row() { RowIndex = 5 };
   spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.First().AppendChild(r2);

   r2.AppendChild(new Cell() { CellReference = "A5", CellValue = new CellValue("0"), DataType = new EnumValue<CellValues>(CellValues.SharedString) });

   spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.Save();

   spreadSheet.WorkbookPart.Workbook.GetFirstChild<Sheets>().AppendChild(new Sheet()
            {
                Id = spreadSheet.WorkbookPart.GetIdOfPart(spreadSheet.WorkbookPart.WorksheetParts.First()),
                SheetId = 1,
                Name = "test"
            });

            spreadSheet.WorkbookPart.Workbook.Save();  
        }

Everything seems to work, the file saves where i want it to and, generally, looks the way i expect it to. The "only" issue is that, when i add the string to the cell, excel will give me an error saying that the file is corrupt and continues to delete said cell.

Am i doing something wrong?


Try this, in your method..

 if (spreadSheet.WorkbookPart.GetPartsOfType<SharedStringTablePart>().Count() > 0)
                    {
                    shareStringPart = spreadSheet.WorkbookPart.GetPartsOfType<SharedStringTablePart>().First();
                    }
                else
                    {
                    shareStringPart = spreadSheet.WorkbookPart.AddNewPart<SharedStringTablePart>();
                    }
index = InsertSharedStringItem(cell_value, shareStringPart);
cell.CellValue = new CellValue(index.ToString());
cell.DataType = new EnumValue<CellValues>(CellValues.SharedString);

InsertSharedString Method:

 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;
            }

I think you should create a spreadsheet (using Excel), add the text into the cell and then open this spreadsheet in "OpenXML 2.5 Productivity Tool". There is a "Reflect code" button in the productivity tool that would help you replicate in code what needs to be done. That's the easiest way, I've found to solve such bugs.

链接地址: http://www.djcxy.com/p/52344.html

上一篇: 导入文本文件并删除以范围名称开头的错误

下一篇: openxml 2.5,如何将字符串插入单元格?