Setzen Sie den Inhalt von mehreren .txt-fileen in Excel 2010

Gibt es irgendeine Weise, die ich den Inhalt von verschiedenen mehrfachen .txt Akten (praktisch den Inhalt aller .txt Akte in einem Faltblatt) in Excel 2010 setzen kann? Ich brauche eine Zelle (A1), um der Name der file zu sein, und die andere Zelle (A2) ist der ganze Inhalt dieser .txt-file. Gleiches gilt für die anderen .txt fileen, dh B1-B2, C1-C2, etc.

Danke im Voraus.

Solutions Collecting From Web of "Setzen Sie den Inhalt von mehreren .txt-fileen in Excel 2010"

Wenn ein CSV akzeptabel ist, können Sie ein kleines Programm schreiben, um alle Textdateien im angegebenen Verzeichnis zu lesen und die data zu schreiben, die Sie im CSV-Format benötigen:

"File Name 1","Contents of File 1" "File Name 2","Contents of File 2" 

Wenn Sie die CSV in Excel öffnen, werden die data genau so angezeigt, wie Sie angeben.

Wenn Sie eine echte Excel-file (.xls, .xlsx) haben, können Sie Interop verwenden, um auf die Excel-Bibliotheken von C # zuzugreifen, aber diese Lösung ist etwas komplexer.

http://msdn.microsoft.com/en-us/library/ms173186(v=vs.80).aspx

Sie können Directory.EnumerateFiles um alle filenamen im gewünschten Ordner File.ReadAllText , und File.ReadAllText , um den Inhalt jeder file zu lesen.

Bei der Arbeit mit CSV-fileen gibt es einige Nuancen um das richtige Zitieren der Ausgabe (siehe Wikipedia-Link zu Beginn meiner Antwort). Ich schrieb eine kleine Erweiterungsmethode , um es einfacher zu machen, richtig zitierte CSV auszugeben:

  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } statice öffentliche classnerweiterungen  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } statice öffentliche characterfolge CsvQuote (dieser characterfolgetext)  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } if (text == null)  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } return string.Empty;  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } bool enthältQuote = false;  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } bool enthältComma = false;  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } int len ​​= text.Length;  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } für (int i = 0; i <len && (enthältComma == false || enthältQuote == false); i ++)  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } char ch = text [i];  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } wenn (ch == '' ')  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } enthältQuote = true;  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } sonst if (ch == ',')  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } enthältComma = true;  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } bool mustQuote = enthältComma ||  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } enthältQuote;  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } if (enthältQuote)  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } text = text.Replace ("\", "\" ");  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } wenn (mustQuote)  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } return "\" + text + "\" ";  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } // Zitat der Zelle und ersetze eingebettete Zitate mit Doppel-Zitat  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } Text zurückgeben;  static public class Extensions { static public string CsvQuote(this string text) { if (text == null) { return string.Empty; } bool containsQuote = false; bool containsComma = false; int len = text.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = text[i]; if (ch == '"') { containsQuote = true; } else if (ch == ',') { containsComma = true; } } bool mustQuote = containsComma || containsQuote; if (containsQuote) { text = text.Replace("\"", "\"\""); } if (mustQuote) { return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote } else { return text; } } } 

BEARBEITEN:

Von der Oberseite meines Kopfes (nicht debugged oder irgendetwas), könnte der Code, um das CSV zu schreiben, so etwas aussehen:

 string myDirectory = @"C:\Temp"; StringBuilder csv = new StringBuilder(); foreach (string fileName in Directory.EnumerateFiles(myDirectory)) { string fileContents = File.ReadAllText(fileName); csv.Append(fileName).Append(",").AppendLine(fileContents.CsvQuote()); } File.WriteAllText(@"C:\SomePath\SomeFile.csv", csv.ToString()); string myDirectory = @ "C: \ Temp"; string myDirectory = @"C:\Temp"; StringBuilder csv = new StringBuilder(); foreach (string fileName in Directory.EnumerateFiles(myDirectory)) { string fileContents = File.ReadAllText(fileName); csv.Append(fileName).Append(",").AppendLine(fileContents.CsvQuote()); } File.WriteAllText(@"C:\SomePath\SomeFile.csv", csv.ToString()); StringBuilder csv = new StringBuilder (); string myDirectory = @"C:\Temp"; StringBuilder csv = new StringBuilder(); foreach (string fileName in Directory.EnumerateFiles(myDirectory)) { string fileContents = File.ReadAllText(fileName); csv.Append(fileName).Append(",").AppendLine(fileContents.CsvQuote()); } File.WriteAllText(@"C:\SomePath\SomeFile.csv", csv.ToString()); foreach (string fileName in Directory.EnumerateFiles (myDirectory)) string myDirectory = @"C:\Temp"; StringBuilder csv = new StringBuilder(); foreach (string fileName in Directory.EnumerateFiles(myDirectory)) { string fileContents = File.ReadAllText(fileName); csv.Append(fileName).Append(",").AppendLine(fileContents.CsvQuote()); } File.WriteAllText(@"C:\SomePath\SomeFile.csv", csv.ToString()); string fileContents = File.ReadAllText (fileName); string myDirectory = @"C:\Temp"; StringBuilder csv = new StringBuilder(); foreach (string fileName in Directory.EnumerateFiles(myDirectory)) { string fileContents = File.ReadAllText(fileName); csv.Append(fileName).Append(",").AppendLine(fileContents.CsvQuote()); } File.WriteAllText(@"C:\SomePath\SomeFile.csv", csv.ToString()); csv.Append (fileName) .Append (",") AppendLine (fileContents.CsvQuote ()); string myDirectory = @"C:\Temp"; StringBuilder csv = new StringBuilder(); foreach (string fileName in Directory.EnumerateFiles(myDirectory)) { string fileContents = File.ReadAllText(fileName); csv.Append(fileName).Append(",").AppendLine(fileContents.CsvQuote()); } File.WriteAllText(@"C:\SomePath\SomeFile.csv", csv.ToString());