Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. £ symbol in csv file for Excel - Encoding weirdness - HELP!

£ symbol in csv file for Excel - Encoding weirdness - HELP!

Scheduled Pinned Locked Moved C#
questioncsharphelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gizz
    wrote on last edited by
    #1

    Hi all. We have a C# 2.0 prog writing to a csv file for emailing bills to customers. So, we have the contents of the csv file in a string. We use a stream writer to send it to the file. We open the file in Excel (2003) and the £ symbol is now prepended with Ậ Seems the encoding has turned the £ into UTF-8 and stuffed and extra charachter in there, that Excel renders incorrectly. We tried everything, Encoding in various ways, using File.WriteText, converting to bytes using Encoding, nothing worked as expected. In the end we settled on this: byte[] bArr = new byte[str.Length]; for (int idx = 0; idx < str.Length; idx++) { char c = str[idx]; byte b = (byte)c; bArr[idx] = b; } File.WriteAllBytes(csvFileName, bArr); Pretty nasty, really. So, question: What is the correct encoding level/object/type to use to do this more elegantly? Thanks in advance.

    G 1 Reply Last reply
    0
    • G Gizz

      Hi all. We have a C# 2.0 prog writing to a csv file for emailing bills to customers. So, we have the contents of the csv file in a string. We use a stream writer to send it to the file. We open the file in Excel (2003) and the £ symbol is now prepended with Ậ Seems the encoding has turned the £ into UTF-8 and stuffed and extra charachter in there, that Excel renders incorrectly. We tried everything, Encoding in various ways, using File.WriteText, converting to bytes using Encoding, nothing worked as expected. In the end we settled on this: byte[] bArr = new byte[str.Length]; for (int idx = 0; idx < str.Length; idx++) { char c = str[idx]; byte b = (byte)c; bArr[idx] = b; } File.WriteAllBytes(csvFileName, bArr); Pretty nasty, really. So, question: What is the correct encoding level/object/type to use to do this more elegantly? Thanks in advance.

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      A would guess that Excel is reading the file as an ANSI file, that is/was the standard text format in windows prior to unicode. Try the Encoding.Default encoding, that will give you the encoding currently used by the system for ANSI files.

      --- "Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things." -- Douglas Adams

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups