简体   繁体   中英

Write the data from an Excel sheet to the text file using C#

I need to write the data from an Excel sheet/workbook to the text file using C#.

Also, I want all Excel worksheet data in single text file.

Apart from reading the data from Excel sheets one by one and then appending it to the existing text file, is there any other simple way to do it?

I would suggest using OleDbDataReader with a connection string from www.connectionstrings.com to read the Excel file. You can use your favorite method, say StreamWriter , to spit the data out to a text file.

SpreadsheetGear for .NET can do it with a few lines of code:

using System;
using SpreadsheetGear;

namespace WorkbookToCSV
{
    class Program
    {
        static void Main(string[] args)
        {
            string inputFilename = @"c:\CSVIn.xlsx";
            string outputFilename = @"c:\CSVOut.csv";
            // Create the output stream.
            using (System.IO.FileStream outputStream = new System.IO.FileStream(outputFilename, System.IO.FileMode.Create, System.IO.FileAccess.Write))
            {
                // Load the source workbook.
                IWorkbook workbook = Factory.GetWorkbook(inputFilename);
                foreach (IWorksheet worksheet in workbook.Worksheets)
                {
                    // Save to CSV in a memory buffer and then write it to
                    // the output FileStream.
                    byte[] csvBuffer = worksheet.SaveToMemory(FileFormat.CSV);
                    outputStream.Write(csvBuffer, 0, csvBuffer.Length);
                }
                outputStream.Close();
            }
        }
    }
}

You can download a free trial here and try it yourself.

Disclaimer: I own SpreadsheetGear LLC

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Globalization;

namespace Module2
{
    public class Archives
    {
        public void readArchive()
        {
            //this will read from the archive
            StreamReader SR;
            string S;
            int i = 0;
            SR = File.OpenText(@"the path here for the excel archive");

            S = SR.ReadToEnd();
            SR.Close();
            Console.WriteLine(S);
            string[] words = S.Split(';');
            Array.Sort(words);
            for (i = 0; i < words.Length; i++)
                Console.WriteLine(words[i]);

            //this will create the archive
            StreamWriter SW;
            SW = File.CreateText(@"the path here for the .txt");
            for (i = 0; i < words.Length; i++)
                SW.WriteLine(words[i]);
            SW.Close();
        }
    }
}

Will read, split the text putting them on an array and write every word in the .txt file .. I guess that`s it ..

There are many ways to do this. Try to search for Excel in the search box.

In your case I suppose the easiest option will be to automate Excel to open the excel workbook and then using the SaveAs function to save it as CSV.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM