You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Excel会计格式数字中的特殊空白字符无法识别与清除问题

解决Excel导入后Price字段特殊空白字符的问题

问题根源

Excel会计格式的千分位分隔空白,大概率是非断行空格(Unicode值U+00A0,对应SQL的CHAR(160)),这是你之前未检测到的空白类型。


SQL端解决方案

1. 确认特殊空白类型

执行以下SQL检测CHAR(160):

SELECT
    price,
    nonbreaking_space_160 = IIF(CHARINDEX(CHAR(160), price) > 0, 1, 0)
FROM QuoteStagingDataSep2022;

如果返回结果为1,说明目标空白就是该字符。

2. 替换去除空白

直接用REPLACE语句清理:

UPDATE QuoteStagingDataSep2022
SET price = REPLACE(price, CHAR(160), '')
WHERE CHARINDEX(CHAR(160), price) > 0;

C#端解决方案

你的原有代码存在两个核心问题:

  1. 单个char无法触发string.IsNullOrEmpty(item.ToString())判断,应使用char.IsWhiteSpace(item)或直接匹配特定非断空格'\u00A0'
  2. string.Replace是 immutable 方法,不会修改原字符串,需重新赋值或直接过滤

修正后的代码示例:

private async void button1_Click(object sender, EventArgs e)
{
    DataTable dt = await logic.QueryQuoteStagingData();
    StringBuilder priceBuilder = new StringBuilder();
    
    for (int i = 7; i < dt.Rows.Count; i++)
    {
        string rawPrice = dt.Rows[i]["Price"].ToString();
        // 针对性替换非断空格和常规空格
        string cleanedPrice = rawPrice.Replace('\u00A0', '').Replace(' ', '');
        priceBuilder.Append(cleanedPrice);
    }
}

若需要过滤所有空白字符,可改用更简洁的写法:

string cleanedPrice = new string(rawPrice.Where(c => !char.IsWhiteSpace(c)).ToArray());

内容的提问来源于stack exchange,提问作者GrammatonCleric

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 18:16:07