从excel工作表中的数据写入文本文件虽然C#

我需要通过C#将Excel表单/工作簿中的数据写入文本文件。

另外,我想要在单个文本文件中的所有Excel工作表数据。

除了逐一读取excel表格中的数据,然后将其附加到现有的文本文件,还有其他简单的方法吗?

谁能帮我这个?

提前致谢

我build议使用OleDbDataReader与来自www.connectionstrings.com的连接string来读取Excel文件。 你可以使用你最喜欢的方法,比如说StreamWriter ,把数据吐出到一个文本文件中。

SpreadsheetGear for .NET可以通过几行代码来完成:

 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(); } } } } 

你可以在这里下载一个免费的试用版,并自己试用。

免责声明:我自己的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(); } } } 

将阅读,拆分文本把他们放在一个数组,并写在.txt文件中的每个单词..我想这是它..

有很多方法可以做到这一点。 尝试在search框中searchExcel。

在你的情况下,我想最简单的select将是自动化Excel打开Excel工作簿,然后使用SaveAsfunction将其另存为CSV。