OpenXML将值写为string,并将值转换为数值时更改

我正在使用OpenXML将一些数据写入Excel。 我已经成功地将所有数据input到电子表格中,但是当我这样做的时候,它们都是以string格式。 这是因为我主要利用MSDN上提供的示例代码,并将它们作为InsertSharedStringItemstring )插入到Excel中。 但是我find了两种将单元格转换为数字的方法。

第一个是一个简单的CellValues.Number而不是CellValues.SharedString但似乎改变了单元格中的所有实际值以及将其更改为一个数字。 一个例子是我现在有一个0,现在是5,11现在是0,21是33,这样的奇怪的东西(如果数字重复0,他们都改变,在0的情况下,5)。 显然这是行不通的。

然后我发现另一种方法在Excel中将string转换为数字,并在此处find(如果您单击该答案中的链接转到该用户源,则需要在URL中添加“www”)。 这个解决scheme的问题是,它会将所有的string转换为双精度值,但是这次的值与双精度格式的第一个解决scheme相同。

还值得注意的是,我的第三列应该是一个string,不需要转换为数字,但它反正。

这是我正在与:

 private static void WriteToExcel(string[] contents, char[] desiredColumns) { string filePath = @"D:\Folder\test.xlsx"; try { using (SpreadsheetDocument StatsCollection = SpreadsheetDocument.Open(filePath, true)) { string sheetName = ""; int totalRows = contents.Length / 15; int contentsIndex = 0; for (uint indexRow = 1; indexRow < totalRows; indexRow++) { for (int indexCol = 1; indexCol < 16; indexCol++) { sheetName = "Sheet1"; SharedStringTablePart shareStringPart; if (StatsCollection.WorkbookPart.GetPartsOfType<SharedStringTablePart>().Count() > 0) { shareStringPart = StatsCollection.WorkbookPart.GetPartsOfType<SharedStringTablePart>().First(); } else { shareStringPart = StatsCollection.WorkbookPart.AddNewPart<SharedStringTablePart>(); } int index = InsertSharedStringItem(contents[contentsIndex], shareStringPart); WorkbookPart bookPart = StatsCollection.WorkbookPart; Sheet mySheet = bookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == sheetName).FirstOrDefault(); WorksheetPart sheetPart = (WorksheetPart)(bookPart.GetPartById(mySheet.Id)); Cell cell = InsertCellInWorksheet(desiredColumns[indexCol - 1].ToString(), indexRow, sheetPart); cell.CellValue = new CellValue(index.ToString()); //I added the following if structure to check for numbers if (IsNumeric(contents[contentsIndex].ToString())) { cell.DataType = new EnumValue<CellValues>(CellValues.Number); } else { cell.DataType = new EnumValue<CellValues>(CellValues.SharedString); } /*I removed the following lines Stylesheet styleSheet = bookPart.WorkbookStylesPart.Stylesheet; uint _doubleStyleId = createCellFormat(styleSheet, null, null, UInt32Value.FromUInt32(4)); cell.StyleIndex = _doubleStyleId; */ sheetPart.Worksheet.Save(); contentsIndex++; } } Console.WriteLine("Copy Complete."); } } catch (Exception ex) { Console.WriteLine("Cannot find output Excel file.\n" + ex.Message); } } 

这里是来自其他解决scheme的createCellFormat方法。 与我的更新有关。 由于我所做的更改,我不再需要这种方法,但是由于它与原始问题有关,所以我将把它留下。

 private static UInt32Value createCellFormat(Stylesheet styleSheet, UInt32Value fontIndex, UInt32Value fillIndex, UInt32Value numberFormatId) { CellFormat cellFormat = new CellFormat(); if (fontIndex != null) { cellFormat.FontId = fontIndex; } if (fillIndex != null) { cellFormat.FillId = fillIndex; } if (numberFormatId != null) { cellFormat.NumberFormatId = numberFormatId; cellFormat.ApplyNumberFormat = BooleanValue.FromBoolean(true); } styleSheet.CellFormats.Append(cellFormat); UInt32Value result = styleSheet.CellFormats.Count; styleSheet.CellFormats.Count++; return result; } 

当我尝试将单元格的值从string转换为数字时,按照预期进行转换,但会更改该值。 为什么会发生这种情况,我怎样才能解决这个问题,并防止我的一列实际string被改为一个数字?

更新:

我设法修复string数字转换通过以下解决scheme,我相应地更新了我的代码,并添加以下方法。

 private static bool IsNumeric(string value) { double output = 0; if(Double.TryParse(value, out output)) { return true; } else { return false; } } 

现在所有应该是一个数字的东西都是一个数字,所有需要保持一个string的东西都是一个string。 然而,我的号码的实际值仍然在改变(应为0的数字是5,11是0,21是33等)

问题是CellValue只是作为一个string的索引。 您应该将实际的数组contents[contentsindex]用于数字,因为您要显示该数组的内容而不是index的值。 将CellValue部分添加到if..else …语句中,如下所示。 @BlueBarren和我一起通过聊天工作。

 if (IsNumeric(contents[contentsIndex])) { cell.DataType = new EnumValue<CellValues>(CellValues.Number); cell.CellValue = new CellValue(contents[contentsIndex]); } else { cell.DataType = new EnumValue<CellValues>(CellValues.SharedString); cell.CellValue = new CellValue(index.ToString()); }