“This is my 40th day of participating in the First Challenge 2022. For more details: First Challenge 2022.”

Welcome friends to search “Andy Hui” on wechat to pay attention to a wave!

Write some thoughts of the programmer, hope it will help you.

Welcome to become my reader, hope this article can give you some help.

preface

Hi, I’m Hui.

Let’s take a look at some of the most common examples of exporting Excel files when C# is dealing with streaming files.

In the daily business coding process, most of the time, the requirements require to export the table file that Office can open. Our usual approach is to use the Help of the Office component, or better yet, use the NPOI component to export (free of the limitations of its Office component). But both of these export components have more or less drawbacks.

For example, in NPOI software, when the amount of data is too large to exceed 60W, there will be overflow problems in the components. There is no solution yet, so you have to switch to another component or restrict the exported data.

So is there any other way we can export files that Office can open, at this point we stream file.csv format to play.

.csv is a comma-separated value file format that stores tabular data (numeric and text) in plain text format. A CSV file consists of any number of records separated by a newline character. Each record consists of fields separated by other characters or strings, most commonly commas or tabs. Typically, all records have exactly the same sequence of fields. They are usually plain text files.

It has many advantages, can store a large amount of data, time-consuming, easy to operate, can be opened by Office and so on.

For the current data interaction with the hardware of the lower computer, the data transferred in the upper computer is generally CSV files.

CSV format

C# export CSV file

try
{
   string strBufferLine = "";
   StreamWriter strmWriterObj = new StreamWriter(strFilePath, false, Encoding.UTF8);                
  strmWriterObj.WriteLine(tableheader);                
  for (int i = 0; i < dt.Rows.Count; i++)
  {
      strBufferLine = "";
      for (int j = 0; j < dt.Columns.Count; j++)
      {
          if (j > 0)
              strBufferLine += ",";
          strBufferLine += dt.Rows[i][j].ToString();
      }
      strmWriterObj.WriteLine(strBufferLine);
  }
  strmWriterObj.Close();
  strmWriterObj.Dispose();
  return dt.Rows.Count;
 }
 catch (Exception ex)
 {
     throw new Exception(ex.Message);
 }
Copy the code

C# import CSV file

public static DataTable Csv2Dt(string filePath, int n, DataTable dt) { try { var encoding = CommonFileHelper.GetFileEncodeType(filePath); StreamReader reader = new StreamReader(filePath, encoding, false); int i = 0, m = 0; reader.Peek(); DataRow dr; while (reader.Peek() > 0) { m = m + 1; string str = reader.ReadLine(); if (m >= n + 1) { string[] split = str.Split(','); dr = dt.NewRow(); for (i = 0; i < split.Length; i++) { if (i == 0) { dr[i] = split[i]; } else { if (string.IsNullOrEmpty(split[i])) { dr[i] = DBNull.Value; } else { dr[i] = Convert.ToDouble(split[i]); } } } dt.Rows.Add(dr); } } reader.Close(); reader.Dispose(); return dt; } catch (Exception ex) { throw new Exception(ex.Message); }}Copy the code

I’m glad you can see it. I hope it helps.

remarks

Life is short. I don’t want to go for what I can’t see. I want to catch what I can see.

Original is not easy, give a attention.

I am Hui, thank you for reading, if it is helpful to you, please like, forwarding thank you.

I’m glad to make friends with you.