openxml 2.5,如何将字符串插入单元格?

我已经尝试了几天,现在向openxml电子表格中插入一个字符串。 其他一切(迄今)都有效,除此之外的一切。

这是我目前正在运行的代码(注意,这纯粹是为了测试目的,而且非常基本):

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

一切似乎都起作用,文件保存在我想要的地方,而且通常看起来是我期望的方式。 “唯一”的问题是,当我将字符串添加到单元格时,excel会给我一个错误,指出该文件已损坏,并继续删除所述单元格。

难道我做错了什么?


试试这个,用你的方法..

 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方法:

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

我认为你应该创建一个电子表格(使用Excel),将文本添加到单元格中,然后在“OpenXML 2.5生产力工具”中打开这个电子表格。 生产力工具中有一个“反映代码”按钮,可以帮助您在代码中复制需要完成的工作。 这是我发现解决此类错误的最简单方法。

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

上一篇: openxml 2.5, how to insert a string into a cell?

下一篇: Embed Excel into Powerpoint 2007 using openxml and C#