本文介绍了Openxml自定义dateformat单元格返回数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的要求是使用OpenXml在excel文件中阅读,编写电子表格。 我的电子表格单元格有多种格式,如数字,字符串,日期等。 我的代码正在读取除dateformat单元格之外的所有内容一个单元格是自定义日期格式(d-mmm),它返回数字(如7月8日返回41128)。我没有得到解决这个... 我搜索了很多。请帮助 我的代码如下。谢谢提前.. 使用(SpreadsheetDocument xl = SpreadsheetDocument.Open (filepath, true )) { WorkbookPart wbp = xl.WorkbookPart; foreach (Sheet isheet in wbp.Workbook.Sheets) { string relationshipId = isheet.Id.Value; WorksheetPart worksheetPart =(WorksheetPart)xl.WorkbookPart.GetPartById(relationshipId); SheetData sheetData = worksheetPart.Worksheet.Elements< SheetData>()。First(); IEnumerable< Row> rows = sheetData.Elements< Row>(); for ( int irow = 1 ; irow < = rows.Count(); irow ++) { 尝试 { IEnumerable< Cell> cells = rows.ElementAt(irow).Elements< Cell>(); for ( int Ccount = 0 ; Ccount < cells.Count(); Ccount ++) { string cellIndex = GetCellIndex(cells.ElementAt(Ccount)); WorksheetPart wsPart =(WorksheetPart)(wbp.GetPartById(isheet.Id)); Cell theCell = wsPart.Worksheet.Descendants< Cell>()。其中​​(c = > c.CellReference = = cellIndex).FirstOrDefault(); if (theCell!= null ) { value = theCell.InnerText; if (theCell.DataType!= null ) { switch (theCell.DataType.Value) { case CellValues.SharedString: var stringTable = wbp.GetPartsOfType< SharedStringTablePart>()。FirstOrDefault(); if (stringTable!= null ) value = stringTable.SharedStringTable.ElementAt( int .Parse( value )) .InnerText; break ; case CellValues.Date: { int i = 0 ; string s = value ; bool result = int .TryParse(s, out i); if (result) value = JulianToDateTime(Convert.ToInt32(值))ToShortDateString(); else value = ; } break ; case CellValues。 Boolean : switch ( value ) { case 0: value = FALSE; break ; 默认: value = TRUE; break ; } break ; } } } } } catch (ArgumentOutOfRangeException argex) { break ; } } } } 解决方案 在以前版本的Excel应用程序中,有一个 NumberFormat [ ^ ]单元格的属性。 请按照以下链接: http://stackoverflow.com/questions/4730152/what-indicates- an-office-open-xml-cell-contains-a-date-value [ ^ ] http://social.msdn.microsoft.com/Forums/en-US / oxmlsdk / thread / c02fc9f9-0a7d-40b5-ac16-48199df474ba [ ^ ] 数字格式 [ ^ ] CellStyleFormats [ ^ ] My requirement is to read,write spreadsheets in excel file using OpenXml.My spreadsheet cells are in many formats like numbers,strings,dates etc..My code is reading everything except dateformat cell. one cell is a Custom dateformat(d-mmm), which is returning numbers (like 7-Aug is returning 41128). I am not getting resolved this...I searched alot. pls helpmy code is as below. Thanks advance.. using (SpreadsheetDocument xl = SpreadsheetDocument.Open(filepath, true)) { WorkbookPart wbp = xl.WorkbookPart; foreach (Sheet isheet in wbp.Workbook.Sheets) { string relationshipId = isheet.Id.Value; WorksheetPart worksheetPart = (WorksheetPart)xl.WorkbookPart.GetPartById(relationshipId); SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First(); IEnumerable<Row> rows = sheetData.Elements<Row>(); for (int irow = 1; irow <= rows.Count(); irow++) { try { IEnumerable<Cell> cells = rows.ElementAt(irow).Elements<Cell>(); for (int Ccount = 0; Ccount < cells.Count(); Ccount++) { string cellIndex = GetCellIndex(cells.ElementAt(Ccount)); WorksheetPart wsPart = (WorksheetPart)(wbp.GetPartById(isheet.Id)); Cell theCell = wsPart.Worksheet.Descendants<Cell>().Where(c => c.CellReference == cellIndex).FirstOrDefault(); if (theCell != null) { value = theCell.InnerText; if (theCell.DataType != null) { switch (theCell.DataType.Value) { case CellValues.SharedString: var stringTable = wbp.GetPartsOfType<SharedStringTablePart>().FirstOrDefault(); if (stringTable != null) value = stringTable.SharedStringTable.ElementAt(int.Parse(value)).InnerText; break; case CellValues.Date: { int i = 0; string s = value; bool result = int.TryParse(s, out i); if (result) value = JulianToDateTime(Convert.ToInt32(value)).ToShortDateString(); else value = ""; } break; case CellValues.Boolean: switch (value) { case "0": value = "FALSE"; break; default: value = "TRUE"; break; } break; } } } } } catch (ArgumentOutOfRangeException argex) { break; } } } } 解决方案 In previous version of Excel application, there was a NumberFormat[^] property for cell.Please, follow the below links:http://stackoverflow.com/questions/4730152/what-indicates-an-office-open-xml-cell-contains-a-date-time-value[^]http://social.msdn.microsoft.com/Forums/en-US/oxmlsdk/thread/c02fc9f9-0a7d-40b5-ac16-48199df474ba[^]NumberingFormats[^]CellStyleFormats[^] 这篇关于Openxml自定义dateformat单元格返回数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 06:20