2013-01-03 49 views
1

在閱讀this StackOverflow文章後,我意識到我與我的CSV文件有同樣的問題,即有人從Word複製粘貼破折號/超( - )字符到Excel中。替換CSV文件中的特殊字符

我正在創建自己的CSV文件,其中包含從Excel電子表格中讀取的數據,並且我注意到奇怪的字符,例如「出現在Excel中,在記事本中查看時未出現。當我使用SSIS將CSV文件傳輸到SQL Server表時,那裏也有奇怪的現象。在檢查了每個ASC值之後,我決定用ASC 150(連字符)替換ASC 150(短劃線),並且這樣糾正了這個問題,並且在Excel中查看時也看起來正常。

這讓我去質疑還有什麼其他角色可能需要更換,以及是否有一個可以用來保護我的CSV文件不會出現類似問題的常規例程。

這是我目前爲每個想要寫入CSV文件的值所做的工作。請注意,我的getCharacterString函數與返回與ASCII值關聯的ASC charact的VB CHR函數類似。

/// <summary> 
    /// Locates occurrences of targeted special characters found in the input string and replaces each with a space. 
    /// </summary> 
    /// <param name="inputString">The input string.</param> 
    /// <returns>The updated inputString.</returns> 
    private string ReplaceSpecialCharacters(string inputString) 
    { 
     StringBuilder stringBuilder = new StringBuilder(inputString); 

     const string doubleQuoteCharacter = "\""; 

     stringBuilder.Replace("\r\n", " "); // Carriage Return/Line Feed characters replaced with single space 
     stringBuilder.Replace("\r", " "); // Carriage Return replaced with one space if only \r is found 
     stringBuilder.Replace("\n", " "); // Likewise, Line Feed with a single space   
     stringBuilder.Replace(this.columnSeparator, " "); // Tab    
     stringBuilder.Replace(Character.GetCharacterString(150), Character.GetCharacterString(45)); // Replace Dash with Hypen 
     stringBuilder.Replace(Character.GetCharacterString(147), doubleQuoteCharacter); // Replace angled left quote, 「, with simple double quote, ". 
     stringBuilder.Replace(Character.GetCharacterString(148), doubleQuoteCharacter); // Replace angled left quote, 「, with simple double quote, ". 

     return stringBuilder.ToString(); 
    }   

這裏是我發現的轉換功能:

// ----------------------------------------------------------------------- 
// <copyright file="Character.cs" company="Joes bar and grill"> 
// TODO: Update copyright text. 
// </copyright> 
// ----------------------------------------------------------------------- 

namespace JoesBarAndGrill.FinanceIT.HhsSweeper 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 

    /// <summary> 
    /// TODO: Update summary. 
    /// </summary> 
    public static class Character 
    { 
     /// <summary> 
     /// See http://bytes.com/topic/c-sharp/answers/273734-c-chr-asc-function-equivalents-undocumented-truth. 
     /// </summary> 
     /// <param name="asciiValue"></param> 
     /// <returns></returns> 
     public static string GetCharacterString(int asciiValue) 
     { 
      if ((asciiValue < 0) || (asciiValue > 255)) 
      { 
       throw new ArgumentOutOfRangeException("asciiValue", asciiValue, "Must be between 0 and 255."); 
      } 
      byte[] bytBuffer = new byte[] { (byte)asciiValue }; 
      return Encoding.GetEncoding(1252).GetString(bytBuffer); 
     } 

     public static int GetAsciiValue(string character) 
     { 
      if (character.Length != 1) 
      { 
       throw new ArgumentOutOfRangeException("character", character, "Must be a single character."); 
      } 
      char[] chrBuffer = { Convert.ToChar(character) }; 
      byte[] bytBuffer = Encoding.GetEncoding(1252).GetBytes(chrBuffer); 
      return (int)bytBuffer[0]; 
     } 
    } 
} 

同樣,我的問題是這樣的:

我有什麼做的,拿出一個通用的方法來識別所有字符那最終可能會出現像這樣的轉換問題?我想我可能只是確定了常見的。我還希望讓人們幫助我提出一個更完整的目標字符列表來替換和提出替代字符。

我不確定這是否相關,但如果有人建議我在CSV文件中使用文本分隔符,我不使用文本限定符,因爲我確信SSIS 2008無法正確處理它們(請參閱a previous question of mine

+1

在SSIS之前,我們使用[Unicode Hammer](http://code.activestate.com/recipes/251871-latin1-to-ascii-the-unicode-hammer/)來粉碎所有那些「有趣」的角色轉化爲基本的ascii值。我還沒有考慮過如何在.NET時代最好地處理它 – billinkc

+1

爲了爭辯,最好使用一個字符串生成器來替換和重建該字符串多次。 –

+0

@尼克,謝謝。我使用StringBuilders進行字符串連接操作,並且從未注意到SB對象上有一個Replace函數。謝謝。代碼已更新。 – ChadD

回答

2

刪除所有腳本代碼。編輯您的平面文件的連接對象。將代碼頁更改爲65001(UTF-8)。

+0

這聽起來像你知道你在說什麼,我會嘗試它... – ChadD

+0

思考完這個之後,我決定如果用戶爲了調整數據而使用Excel打開CSV文件,他們應該能夠看到有意義的字符。更改代碼頁面只會改變數據加載的方式。 – ChadD