C# 使用C将Excel工作表中的数据写入文本文件#

C# 使用C将Excel工作表中的数据写入文本文件#,c#,excel,text-files,C#,Excel,Text Files,我需要使用C#将Excel工作表/工作簿中的数据写入文本文件 另外,我希望所有Excel工作表数据都在一个文本文件中 除了逐个读取Excel工作表中的数据,然后将其附加到现有文本文件之外,还有其他简单的方法吗?我建议使用连接字符串从读取Excel文件。您可以使用您最喜欢的方法,例如,将数据输出到文本文件。我建议使用连接字符串从读取Excel文件。您可以使用自己喜欢的方法,例如,将数据输出到文本文件。有很多方法可以做到这一点。尝试在搜索框中搜索Excel 在您的情况下,我认为最简单的方法是自动使用

我需要使用C#将Excel工作表/工作簿中的数据写入文本文件

另外,我希望所有Excel工作表数据都在一个文本文件中


除了逐个读取Excel工作表中的数据,然后将其附加到现有文本文件之外,还有其他简单的方法吗?

我建议使用连接字符串从读取Excel文件。您可以使用您最喜欢的方法,例如,将数据输出到文本文件。

我建议使用连接字符串从读取Excel文件。您可以使用自己喜欢的方法,例如,将数据输出到文本文件。

有很多方法可以做到这一点。尝试在搜索框中搜索Excel


在您的情况下,我认为最简单的方法是自动使用Excel打开Excel工作簿,然后使用SaveAs功能将其保存为CSV。

有很多方法可以做到这一点。尝试在搜索框中搜索Excel

在您的情况下,我认为最简单的方法是自动使用Excel打开Excel工作簿,然后使用SaveAs功能将其保存为CSV。

只需几行代码即可:

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 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();
            }
        }
    }
}
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();
        }
    }
}
你可以下载一个免费试用版,自己试一试

免责声明:我拥有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();
        }
    }
}
使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.IO; 使用System.Text.RegularExpressions; 利用制度全球化; 名称空间模块2 { 公共类档案 { public void readArchive() { //这将从存档中读取 StreamReader SR; 字符串S; int i=0; SR=File.OpenText(@“excel归档文件的路径”); S=SR.ReadToEnd(); 高级关闭(); 控制台。写入线(S); string[]words=S.Split(“;”); 数组。排序(单词); for(i=0;i 将读取、拆分文本并将其放在数组中,然后在.txt文件中写入每个单词。。 我想就是这样了。

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
使用System.Text.RegularExpressions;
利用制度全球化;
名称空间模块2
{
公共类档案
{
public void readArchive()
{
//这将从存档中读取
StreamReader SR;
字符串S;
int i=0;
SR=File.OpenText(@“excel归档文件的路径”);
S=SR.ReadToEnd();
高级关闭();
控制台。写入线(S);
string[]words=S.Split(“;”);
数组。排序(单词);
for(i=0;i
将读取、拆分文本并将其放在数组中,然后在.txt文件中写入每个单词。。
我想就是这样了。

我想他需要将多张工作表输出到一个CSV文件中。自动将SaveAs函数另存为CSV将导致多个文件(我想您可以将这些文件连接起来,但这可能会变得有点复杂)。否则是个好建议。我认为他需要将多张图纸输出到一个CSV文件。自动将SaveAs函数另存为CSV将导致多个文件(我想您可以将这些文件连接起来,但这可能会变得有点复杂)。另外,这是一个很好的建议。好吧,我试着用这段代码阅读xlsx文件,但它只打印一些奇怪的数据。我想这就是excel分隔列和行的方式。。它使用“;”,就是这样?我只是复制粘贴的ur代码,但仍然不能正常工作。它正在打印一些askii代码。它正在返回ASCII代码,所以这可能不是读取Excel文件的正确方法。好吧,我尝试使用此代码读取xlsx文件,但它只打印一些奇怪的数据。我猜Excel就是这样将列和行分开的。。它使用“;”,就是这样?我只是复制粘贴的ur代码,但仍然不能正常工作。它正在打印一些askii代码。它正在返回ASCII代码,所以这可能不是读取Excel文件的正确方法