读取创建的Excel(Open XML)时,共享字符串表返回空值。

3
我正在使用Open XML SDK创建Excel文件,并希望使用Open XML阅读它。当我想要读取它时,我会在这一行代码处出现错误:
SharedStringTablePart sstpart = workbookPart.GetPartsOfType<SharedStringTablePart>().First();它返回null。

如果我打开Excel文件并重新保存,它会创建共享字符串表并运行。

从Excel中进行读取。

#region OpenFile
        public void  OpenFile(string directory)
        {
            try
            {
                fs = new FileStream(directory, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                doc = SpreadsheetDocument.Open(fs, false);
            }
            catch (FileNotFoundException ex)
            {
                string exception = string.Format("Doya bulunamadı{0}", directory); 
                throw new ApplicationException(exception);
            }
        }
        #endregion

        #region GetWorkSheet
        public void AssignWorkSheet(int sheetID)
        {
            if (fs == null || doc == null)
                throw new ApplicationException("Dosya açılamadı");

            WorkbookPart workbookPart = doc.WorkbookPart;
            SharedStringTablePart sstpart = workbookPart.GetPartsOfType<SharedStringTablePart>().First();
            sst = sstpart.SharedStringTable;

            var workbook = workbookPart.Workbook;
            var sheets = workbook.Descendants<Sheet>();
            var sheetINVOICE = sheets.ElementAt(sheetID);
            var worksheetPartINVOICE = (WorksheetPart)workbookPart.GetPartById(sheetINVOICE.Id);
            WorkSheet = worksheetPartINVOICE.Worksheet;
            //return sheetInvoice;
        }
        #endregion

它使用以下代码创建一个文本和数字值:

这个程序可以写入一些文本和数字值:

var fileName = string.Format(@"C:\Sonuc\{0}\{1}.xlsx", systemType.ToString(), systemTypeEnum.ToString() + "_" + fileType.ToString());
            SpreadsheetDocument xl = SpreadsheetDocument.Create(fileName, SpreadsheetDocumentType.Workbook);
            WorkbookPart wbp = xl.AddWorkbookPart();

            WorksheetPart wsp = wbp.AddNewPart<WorksheetPart>();
            Workbook wb = new Workbook();

            FileVersion fv = new FileVersion();
            fv.ApplicationName = "Microsoft Office Excel";
            Worksheet ws = new Worksheet();

            SheetData sd = new SheetData();

            //int rowCount = 1;
            DocumentFormat.OpenXml.UInt32Value rowCount = 1;



            foreach (var item in resultList)
            {
                rowCount = rowCount + 1;
                Row r3 = new Row() { RowIndex = rowCount };
                Cell c6 = new Cell();
                c6.DataType = CellValues.String;
                c6.CellValue = new CellValue(item.BusinessPartner);
                r3.Append(c6);

                Cell c7 = new Cell();
                c7.DataType = CellValues.Number;
                c7.CellValue = new CellValue(item.VKN);
                r3.Append(c7);

                Cell c8 = new Cell();
                c8.DataType = CellValues.Number;
                c8.CellValue = new CellValue(item.InvoiceCount.ToString());
                r3.Append(c8);

                Cell c9 = new Cell();
                c9.DataType = CellValues.Number;
                c9.CellValue = new CellValue(item.TaxTotalAmount.ToString());
                r3.Append(c9);
                sd.Append(r3);

            }

            ws.Append(sd);
            wsp.Worksheet = ws;
            wsp.Worksheet.Save();

            Sheets sheets = new Sheets();
            Sheet sheet = new Sheet();
            sheet.Name = "Sheet1";
            sheet.SheetId = 1;
            sheet.Id = wbp.GetIdOfPart(wsp);
            sheets.Append(sheet);
            wb.Append(fv);
            wb.Append(sheets);

            xl.WorkbookPart.Workbook = wb;
            xl.WorkbookPart.Workbook.Save();
            xl.Close();
1个回答

11

默认情况下,MS EXCEL使用SharedStringTablePart保存String值。这就是为什么当您使用MS EXCEL打开和保存文件时,您的代码似乎有效的原因。

但是在这里,当您创建文件时,定义Cell.DatatypeCellValues.String(而不是CellValues.SharedString)。

c6.DataType = CellValues.String;
Cell.DatatypeCellValues.String 时,必须通过读取 Cell.CellValue 属性来读取值。
要使用 SharedStringPart 保存 String 值,请参阅在线文档:

谢谢Chris,我已经阅读了你提供的第一个链接并解决了我的问题。 - altandogan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接