更改OpenXml的逗号分隔符

时间:2018-03-23 06:41:48

标签: c# openxml

我正在使用OpenXml和以下方法获取Excel表格的单元格值,但它似乎无法识别我在“控制面板”中设置为“,”的逗号分隔符。如果单元格中有例如2,5,则得到值25。

public static string GetCellValue(string fileName, string sheetName, string addressName)
    {
        string value = null;
        // Open the spreadsheet document for read-only access.
        using (SpreadsheetDocument document =
            SpreadsheetDocument.Open(fileName, false))
        {
            // Retrieve a reference to the workbook part.
            WorkbookPart wbPart = document.WorkbookPart;

            // Find the sheet with the supplied name, and then use that 
            // Sheet object to retrieve a reference to the first worksheet.
            Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().
              Where(s => s.Name == sheetName).FirstOrDefault();

            // Throw an exception if there is no sheet.
            if (theSheet == null)
            {
                throw new ArgumentException("sheetName");
            }

            // Retrieve a reference to the worksheet part.
            WorksheetPart wsPart =
                (WorksheetPart)(wbPart.GetPartById(theSheet.Id));

            // Use its Worksheet property to get a reference to the cell 
            // whose address matches the address you supplied.
            Cell theCell = wsPart.Worksheet.Descendants<Cell>().
              Where(c => c.CellReference == addressName).FirstOrDefault();

            // If the cell does not exist, return an empty string.
            if (theCell != null)
            {
                value = theCell.InnerText;

                // If the cell represents an integer number, you are done. 
                // For dates, this code returns the serialized value that 
                // represents the date. The code handles strings and 
                // Booleans individually. For shared strings, the code 
                // looks up the corresponding value in the shared string 
                // table. For Booleans, the code converts the value into 
                // the words TRUE or FALSE.
                if (theCell.DataType != null)
                {
                    switch (theCell.DataType.Value)
                    {
                        case CellValues.SharedString:

                            // For shared strings, look up the value in the
                            // shared strings table.
                            var stringTable =
                                wbPart.GetPartsOfType<SharedStringTablePart>()
                                .FirstOrDefault();

                            // If the shared string table is missing, something 
                            // is wrong. Return the index that is in
                            // the cell. Otherwise, look up the correct text in 
                            // the table.
                            if (stringTable != null)
                            {
                                value =
                                    stringTable.SharedStringTable
                                    .ElementAt(int.Parse(value)).InnerText;
                            }
                            break;

                        case CellValues.Boolean:
                            switch (value)
                            {
                                case "0":
                                    value = "FALSE";
                                    break;
                                default:
                                    value = "TRUE";
                                    break;
                            }
                            break;
                    }
                }
            }
        }
        return value;
    }

我将返回的值存储在数组中,如下所示:

for (int i = 0; i < array.Length; i++)
{
            array[i] = Convert.ToDouble(GetCellValue(path, "Sheet1", "A" + (i + 2).ToString()));         
}

我想这是因为我没有将CultureInfo设置为CultureInfo.InstalledUICulture,但我试图进入该方法并且它不起作用。

1 个答案:

答案 0 :(得分:0)

1)如果你想手动转换......

您可以使用以下类来定义小数分隔符:

NumberFormatInfo numberFormatInfo = new NumberFormatInfo();
numberFormatInfo.NumberDecimalSeparator = ".";

然后你可以使用它,例如。在:

value.ToString(numberFormatInfo);

double.Parse(value, numberFormatInfo)

2)为当前线程更改它......

Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator = ",";
Thread.CurrentThread.CurrentUICulture.NumberFormat.CurrencyDecimalSeparator = ",";