Excelのシート/ブックからC#を使用してテキストファイルにデータを書き込む必要があります。ExcelのデータをExcelのテキストファイルに書き込むC#
また、すべてのExcelワークシートデータを1つのテキストファイルに入れたいと思っています。
エクセルシートからデータを1つずつ読み込んだ後に既存のテキストファイルに追加するだけでなく、それを行う簡単な方法はありますか?
誰もがこれを手伝ってくれますか?
おかげで、事前
Excelのシート/ブックからC#を使用してテキストファイルにデータを書き込む必要があります。ExcelのデータをExcelのテキストファイルに書き込むC#
また、すべてのExcelワークシートデータを1つのテキストファイルに入れたいと思っています。
エクセルシートからデータを1つずつ読み込んだ後に既存のテキストファイルに追加するだけでなく、それを行う簡単な方法はありますか?
誰もがこれを手伝ってくれますか?
おかげで、事前
に私は、Excelファイルを読むためにwww.connectionstrings.comから接続文字列でOleDbDataReader
を使用してお勧めします。好きな方法、たとえばStreamWriter
を使って、データをテキストファイルに吐き出すことができます。
これを行う方法はたくさんあります。検索ボックスでExcelを検索してみてください。
あなたのケースでは、Excelブックを開いてSaveAs関数を使用してCSVとして保存するようにExcelを自動化するのが最も簡単な方法だと思います。あなたは無料トライアルhereをダウンロードし、それを自分で試すことができます
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();
}
}
}
}
:
は、数行のコードでそれを行うことができます。
免責事項:私は、私はそれthat`s推測 ..テキストは、アレイ上にそれらを置く分割し、.txtファイル内のすべての単語を書き、読みますスプレッドシートギア社
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();
}
}
}
を所有..
私は彼が単一のCSVファイルに複数のシート出力を必要と思う。 SaveAs関数を自動化してCSV形式で保存すると、複数のファイルが生成されます(これは常に連結できますが、少し複雑になる可能性があります)。そうでなければ良い提案です。 –