Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4614514
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:42:38+00:00 2026-05-22T01:42:38+00:00

I’m trying to export some data I have (stored in a datatable). Some of

  • 0

I’m trying to export some data I have (stored in a datatable). Some of those values have a linebreak in them. Now every time I try and import the file in Excel 2010, the linbreaks get recognised as a new row, instead of an actual linebreak.

The way I output my CSV file (the variable csvfile is a stringbuilder):

context.Response.Clear();
context.Response.ContentType = "text/csv";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name + ".csv");
context.Response.Write(csvfile.ToString());
context.Response.End();

When I open it with Excel manually, it displays fine. But because Excel 2003 doesn’t support the file format, I have to import it. With the import, it sees the linebreaks (\n in the fields) as a new row.

Here is an anonymised example of the data gone wrong:

Header1,Header2,Header3
"value1","value2","value 3
and this is where its going wrong"

It’s a simple CSV file, and when you import it you’ll see where it goes wrong. I encapsulate fields with double quotation marks by default. I also remove leading spaces from values by default.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:42:39+00:00Added an answer on May 22, 2026 at 1:42 am

    Any of the three solutions below work for me:

    • Setting Response.ContentEncoding = System.Text.Encoding.UTF8 isn’t enough to make Excel open UTF-8 files correctly. Instead, you have to manually write a byte-order-mark (BOM) header for the excel file:

      if (UseExcel2003Compatibility)
      {
          // write UTF-16 BOM, even though we export as utf-8. Wrong but *I think* the only thing Excel 2003 understands
          response.Write('\uFEFF');
      }
      else
      {
          // use the correct UTF-8 bom. Works in Excel 2008 and should be compatible to all other editors
          // capable of reading UTF-8 files
          byte[] bom = new byte[3];
          bom[0] = 0xEF;
          bom[1] = 0xBB;
          bom[2] = 0xBF;
          response.BinaryWrite(bom);
      }
      
    • Send as octet-stream, use a filename with .csv extension and do quote the filename as is required by the HTTP spec:

      response.ContentType = "application/octet-stream";
      response.AppendHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
      
    • use double quotes for all fields

    I just checked and for me Excel opens downloaded files like this correctly, including fields with line breaks.

    But note that Excel still won’t open such CSV correctly on all systems that have a default separator different from ",". E.g. if a user is running Excel on a Windows system set to German regional settings, Excel will not open the file correctly, because it expects a semicolon instead of a comma as separator. I don’t think there is anything that can be done about that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.