本文介绍了阅读表格标题rom excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我要读取第一张纸的唯一标题(不是数据),形成一张包含多张纸的excel文档。我不想在内存中加载完整的文档,因此没有DOM方法。我正在使用的代码读取文件的第二张而不是第一张?I've to read the only headers (not data)of the first sheet form an excel document with multiple sheets . I don't want to load the complete document in the memory hence no DOM approach. The code I'm using reads the second sheet of the file and not the first ?internal DataTable GetSchema(string fileName){ int i = 0; DataTable dataTable = new DataTable(); using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, false)){ WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart; foreach (WorksheetPart worksheetPart in workbookPart.WorksheetParts){ OpenXmlReader reader = OpenXmlReader.Create(worksheetPart);// ( while (reader.Read()){ if (reader.ElementType == typeof(Row))///loops through row{reader.ReadFirstChild(); if (i > 1){return dataTable; // we have recieved all column names till here} i++; //increments for the first time which means we are reading columns do{ if (reader.ElementType == typeof(Cell)){ Cell c = (Cell)reader.LoadCurrentElement(); string cellValue = string.Empty; if (c.CellValue != null){ if (c.DataType != null && c.DataType == CellValues.SharedString){ SharedStringItem ssi = workbookPart.SharedStringTablePart.SharedStringTable.Elements<sharedstringitem>().ElementAt(int.Parse(c.CellValue.InnerText)); cellValue = Convert.ToString(ssi.InnerText);}else{ cellValue = Convert.ToString(c.CellValue.InnerText);}} else{cellValue = c.InnerText;} if (i <= 1){dataTable.Columns.Add(cellValue);} else{ break;}} } while (reader.ReadNextSibling());}}} return dataTable;}} excel文件有三张: 1.记录 2.sa 3.da(无专栏) 和我的代码读取2(sa)的标题。 有人可以帮助我从1读取标题(记录)文件?the excel file has three sheets :1. records2.sa3.da (no columns)and my code reads headers for the 2 (sa).Can some one please help me to read the header from 1 (records) file ?推荐答案我删除了每个,尝试下面的行,它工作。 显然它看起来是纸张的顺序是当我们直接阅读它时反转。 WorksheetPart worksheetPart = workbookPart.WorksheetParts.Last();I removed the for each, tried below line and it worked.Apparently it looks that the order of the sheets is reversed when we read it directly.WorksheetPart worksheetPart = workbookPart.WorksheetParts.Last(); 这篇关于阅读表格标题rom excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 20:57