1

似乎有很多建议可以做到这一点,但似乎没有一个可行。

实际上,我想将 Excel 工作表中的文本值更改为数字(这是一个已设置为存储为文本的数字的单元格,旁边有一个绿色菱形)。

网页详细介绍了如何通过 UI 在 Excel 中解决问题,我已将其记录为下面的宏(但那是 VBA)...

包括将值设置为自身:

                Range allCellsRng;
                string lowerRightCell = "AZ500";
                allCellsRng = wSheet.get_Range("A1", lowerRightCell).Cells;
                foreach (Range cell in allCellsRng)
                {
                    if (cell.Value2.ToString().Length > 0)
                    {
                        cell.Value2 = cell.Value2;
                    }
                }

这是一个录制的 VB 宏,它显示了将解决问题的方法,但我在用 C# 表示它时遇到了问题:

ActiveCell.FormulaR1C1 = "0"
Range("A1").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd, SkipBlanks _
    :=False, Transpose:=False
Range("A1").Select
4

3 回答 3

1

With Clear Office, the code is very easy:

SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName);
foreach (Worksheet worksheet in spreadsheetDocument.Workbook.Sheets.OfType<Worksheet>())
{
    foreach (Cell cell in worksheet.GetRange("A1:AZ500"))
    {
        string s = cell.Value as string;
        if (s == null)
            continue;
        double d;
        if (double.TryParse(s, out d))
            cell.Value = d;
        }
    }
spreadsheetDocument.Save();
于 2010-02-03T23:01:56.653 回答
0

改变单元格的格式不是更好吗?我相信你应该能够使用

range.NumberFormat

and change that to the actual format you want... You could then set the range for an entire column, or whole sheet.

于 2010-01-27T12:38:29.320 回答
0

Ok, so raising it as a question on here triggered a brainwave. This looks to work:

Range cellA1 = wSheet.get_Range("A1", System.Type.Missing);
cellA1.Value2 = "0";
cellA1.Copy(System.Type.Missing);
Range cellAll = wSheet.get_Range("A1:AZ500", System.Type.Missing);
cellAll.PasteSpecial(XlPasteType.xlPasteAll, XlPasteSpecialOperation.xlPasteSpecialOperationAdd,
        false, false);
于 2010-01-27T12:44:59.600 回答