Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 读取创建的excel(打开xml)时,共享字符串表返回null_C#_Openxml_Openxml Sdk - Fatal编程技术网

C# 读取创建的excel(打开xml)时,共享字符串表返回null

C# 读取创建的excel(打开xml)时,共享字符串表返回null,c#,openxml,openxml-sdk,C#,Openxml,Openxml Sdk,我正在使用OpenXMLSDK创建excel文件。我想用开放式xml阅读它。当我想读它时,我在这一行遇到了错误 SharedStringTablePart sstpart=workbookPart.GetPartSoftType().First();它返回空值 如果我打开excel文件并再次保存,它将创建共享字符串表并运行 从excel中读取 #region OpenFile public void OpenFile(string directory) {

我正在使用OpenXMLSDK创建excel文件。我想用开放式xml阅读它。当我想读它时,我在这一行遇到了错误
SharedStringTablePart sstpart=workbookPart.GetPartSoftType().First();它返回空值

如果我打开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
#区域打开文件
公共void OpenFile(字符串目录)
{
尝试
{
fs=新文件流(目录,FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
doc=电子表格文档。打开(fs,false);
}
捕获(FileNotFoundException ex)
{
stringexception=string.Format(“Doya bulunamadı{0}”,目录);
抛出新的ApplicationException(异常);
}
}
#端区
#区域获取工作表
公共工作表(int sheetID)
{
如果(fs==null | | doc==null)
抛出新的应用程序异常(“Dosyaılamad”);
WorkbookPart WorkbookPart=doc.WorkbookPart;
SharedStringTablePart sstpart=workbookPart.GetPartSoftType().First();
sst=sstpart.SharedStringTable;
var workbook=workbookPart.workbook;
var sheets=workbook.subjections();
var sheetINVOICE=sheets.ElementAt(sheetID);
var worksheetPartINVOICE=(WorksheetPart)workbookPart.GetPartById(sheetINVOICE.Id);
工作表=工作表partinvoice.工作表;
//退回发票;
}
#端区
它使用下面的代码创建一个写一些文本和数字值

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();
var fileName=string.Format(@“C:\Sonuc\{0}\{1}.xlsx”、systemType.ToString()、systemTypeEnum.ToString()+“”+fileType.ToString());
SpreadsheetDocument xl=SpreadsheetDocument.Create(文件名,SpreadsheetDocumentType.工作簿);
WorkbookPart wbp=xl.AddWorkbookPart();
工作表部件wsp=wbp.AddNewPart();
工作簿wb=新工作簿();
FileVersion fv=新的FileVersion();
fv.ApplicationName=“Microsoft Office Excel”;
工作表ws=新工作表();
SheetData sd=新的SheetData();
//int rowCount=1;
DocumentFormat.OpenXml.uint32值行数=1;
foreach(结果列表中的变量项)
{
rowCount=rowCount+1;
行r3=新行(){RowIndex=rowCount};
单元c6=新单元();
c6.DataType=CellValues.String;
c6.CellValue=新的CellValue(项目.业务伙伴);
r3.附加(c6);
单元c7=新单元();
c7.DataType=CellValues.Number;
c7.CellValue=新的CellValue(项目VKN);
r3.追加(c7);
单元c8=新单元();
c8.DataType=CellValues.Number;
c8.CellValue=新的CellValue(item.InvoiceCount.ToString());
r3.追加(c8);
单元c9=新单元();
c9.DataType=CellValues.Number;
c9.CellValue=新的CellValue(item.TaxTotalAmount.ToString());
r3.追加(c9);
sd.追加(r3);
}
ws.Append(sd);
wsp.Worksheet=ws;
wsp.Worksheet.Save();
板材=新板材();
板材=新板材();
sheet.Name=“Sheet1”;
sheet.SheetId=1;
sheet.Id=wbp.GetIdOfPart(wsp);
附页(页);
wb.Append(fv);
wb.追加(张);
xl.WorkbookPart.Workbook=wb;
xl.WorkbookPart.Workbook.Save();
xl.Close();

默认情况下,MS EXCEL使用
SharedStringTablePart
保存
字符串
值。这就是为什么当您使用MS EXCEL打开并保存文件时,代码似乎正常工作的原因

但是在这里,当您创建文件时,您将
Cell.Datatype
定义为
CellValues.String
(而不是
CellValues.SharedString

Cell.Datatype
CellValues.String
时,必须通过读取
Cell.CellValue
属性来读取值


要使用
SharedStringPart
保存
字符串
值,请参阅联机文档:


谢谢Chris,我已经阅读并通过阅读您的第一个链接解决了我的问题
c6.DataType = CellValues.String;