C#을 사용하여 Excel 시트에서 텍스트 파일에 데이터를 작성하십시오.

StackOverflow https://stackoverflow.com/questions/1037913

  •  10-07-2019
  •  | 
  •  

문제

C#을 사용하여 Excel 시트/통합 문서에서 텍스트 파일에 데이터를 작성해야합니다.

또한 모든 Excel 워크 시트 데이터를 단일 텍스트 파일로 원합니다.

Excel Sheets의 데이터를 하나씩 읽은 다음 기존 텍스트 파일에 추가하는 것 외에도 다른 간단한 방법이 있습니까?

도움이 되었습니까?

해결책

사용하는 것이 좋습니다 OleDbDataReader 연결 문자열로 www.connectionstrings.com Excel 파일을 읽으려면. 좋아하는 방법을 사용할 수 있습니다 StreamWriter, 데이터를 텍스트 파일로 뱉어 내기 위해.

다른 팁

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

무료 평가판을 다운로드 할 수 있습니다 여기 그리고 직접 시도하십시오.

면책 조항 : 스프레드 시트 기어 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 파일에 모든 단어를 씁니다.

이를 수행하는 방법에는 여러 가지가 있습니다. 검색 상자에서 Excel을 검색하십시오.

귀하의 경우 가장 쉬운 옵션은 Excel을 자동화하여 Excel 통합 문서를 열고 Saveas 기능을 사용하여 CSV로 저장하는 것입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top