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

  • Home
  • SEARCH
  • 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 7583429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:40:32+00:00 2026-05-30T18:40:32+00:00

I use next code to generate .csv file from my logs table. public ActionResult

  • 0

I use next code to generate .csv file from my logs table.

public ActionResult Index()
{
var records = loggingService.GetLogRecords(model.Since, model.For);

var mainSb = new StringBuilder();
mainSb.Append("Date,User name\n");

foreach (var logRecord in records)
{
    mainSb.Append(logRecord.Time.ToString("dd/MM/yyyy hh:mm:ss")).Append(",");
    mainSb.Append("\"").Append(logRecord.UserName ?? "").Append("\"").Append(",");
}
return File(Encoding.Unicode.GetBytes(mainSb.ToString()), "text/csv", string.Format("logs.csv"));
}

Usernames contains cyrillic chars, and when I open my .csv file – cyrillic chars is bad displayed. I tried to use UTF8 and UTF32 encodings too. Whan encoding should I use or what changes should I do?

  • 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-30T18:40:33+00:00Added an answer on May 30, 2026 at 6:40 pm

    You could use UTF-8 with preamble. Try like this at the end:

    var data = Encoding.UTF8.GetBytes(mainSb.ToString());
    var result = Encoding.UTF8.GetPreamble().Concat(data).ToArray();
    return File(result, "text/csv", "logs.csv");
    

    Also you seem to be using \n which is not the correct new line separator on Windows and you don’t seem to be appending new lines in your foreach loop, so everything will be on the same line resulting in invalid CSV file. Please, please, please, do not roll your own CSV parser.

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

Sidebar

Related Questions

I use next code to print current time Calendar cal = Calendar.getInstance(); System.out.println(cal.getTime()); I
In Flash Builder (flex 4) I try to use next code to set selected
I allocate a color entry with the next code, then I use it to
I am using this code to generate random text : from collections import defaultdict,
I would like to use http://code.google.com/p/stateless in my code to separate the functionality from
I use Haxe to generate PHP code. (This means you write you code in
I am use next type of strings: LPCSTR, TCHAR, String i want to convert:
I am planning to use Drizzle in my next C# Mono app. Since there
I have the next query that I run via SQLCMD.EXE use [AxDWH_Central_Reporting] GO EXEC
I am looking to use/buy a OCR solution for my next iPhone app. Searching

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.