views:

348

answers:

4

Hello,

i need to write the data from an excel sheet/workbook to the text file though 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?

Can anyone help me with this?

Thanks in advance

+3  A: 

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.

Jason
A: 

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.

Rune Grimstad
I think he needs multiple sheets output to a single CSV file. Automating the SaveAs function to save as CSV would result in multiple files (which I guess you could always concatenate, but that could get a little complicated). Otherwise a good suggestion.
John M Gant
A: 

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

Joe Erickson
A: 
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 ..

Daniel
well, i tried reading xlsx file with this code, but it only prints some weird data.
Archie
I guess it's how excel separates the columns and rows .. It uses ";", that's it ?
Daniel
i just copy pasted ur code but still its not working properly.Its printing some askii code..
Archie