我怎样才能写一个string数组到Excel文件,用制表符分隔符?

我正在创build一个读取制表符分隔的文本文件的小应用程序,进行一些更改,然后创build一个Excel 2007 .xlsx文件。 我无法弄清楚如何从string数组中取出这些行并将它们写入到Excel文件中,使用这些制表符将行分成若干列。 我希望这是有道理的。

我有string Lines[]包含这样的东西:

 Item1\tItem2\tItem3\tItem4 ItemA\tItemB\tItemC\tItemD Item5\tItem6\tItem7\tItem8 

我想创build一个如下所示的Excel文件:

 ABCD Item1 Item2 Item3 Item4 ItemA ItemB ItemC ItemD Item5 Item6 Item7 Item8 

我尝试了以下,但它只是将Lines[]的第一行放入每一行,并不分隔成列:

 string Lines[] = GetLines(); Excel.Application xlApp; Excel.Workbook xlWb; Excel.Worksheet xlWs; object misValue = System.Reflection.Missing.Value; xlApp = new Excel.Application(); xlWb = xlApp.Workbooks.Add(misValue); xlWs = (Excel.Worksheet)xlWb.Worksheets.get_Item(1); Excel.Range c1 = (Excel.Range)xlWs.Cells[2, 1]; Excel.Range c2 = (Excel.Range)xlWs.Cells[2 + lines.Length, 1]; Excel.Range range = xlWs.get_Range(c1, c2); range.Value = lines; range.TextToColumns( range, Microsoft.Office.Interop.Excel.XlTextParsingType.xlDelimited, Microsoft.Office.Interop.Excel.XlTextQualifier.xlTextQualifierNone, false, true // This is flag to say it is tab delimited ); xlApp.Visible = true; 

任何意见,将不胜感激! 谢谢!

这是我目前得到的输出:

 ABCD Item1\tItem2\tItem3\tItem4 Item1\tItem2\tItem3\tItem4 Item1\tItem2\tItem3\tItem4 

编辑:我已经用@ jiverson的build议更新了我的代码,现在行被分隔成Excel中的列,但Lines[]的第一行仍然出现在Excel的每一行中。 为什么?

编辑#2:这是更新的工作代码:

 Excel.Application xlApp; Excel.Workbook xlWb; Excel.Worksheet xlWs; object misValue = System.Reflection.Missing.Value; xlApp = new Excel.Application(); xlWb = xlApp.Workbooks.Add(misValue); xlWs = (Excel.Worksheet)xlWb.Worksheets.get_Item(1); int currentRow = 2; string[] lines = GetLines(); for (int i = 0; i < lines.Length; i++) { string line = lines[i]; //get the current line string[] values = line.Split('\t'); //split the line at the tabs // // .. i do some things to specific values here .. // lines[i] = String.Join("\t", values); //put the updated line back together Excel.Range currentRange = (Excel.Range)xlWs.Cells[currentRow, 1]; //get the next row currentRange.Value = lines[i]; //write the line to Excel currentRow++; } Excel.Range c1 = (Excel.Range)xlWs.Cells[2, 1]; //get the first cell Excel.Range c2 = (Excel.Range)xlWs.Cells[2 + lines.Length, 1]; //get the last cell Excel.Range range = xlWs.get_Range(c1, c2); //set the range as the used area range.TextToColumns( //split the row into columns range, Excel.XlTextParsingType.xlDelimited, Excel.XlTextQualifier.xlTextQualifierNone, false, true // This is flag to say it is tab delimited ); 

循环添加每行,然后在设置范围值后使用文本到列:

  for (int i = 0; i < range.Rows.Count; i++) { range.Rows[i].Value = lines[i]; range.Rows[i].TextToColumns( range.Rows[i], Microsoft.Office.Interop.Excel.XlTextParsingType.xlDelimited, Microsoft.Office.Interop.Excel.XlTextQualifier.xlTextQualifierNone, false, true ); } 

MSDN参考

这可能会帮助你。 我创buildexcel文件的方式是从文件中读取string,并将数据和消息分开,并保存为.csv文件。

可能是你的情况尝试用\treplace\t ,然后尝试创build该文件。

这个代码可能会给你一个想法。 我还没有testing过。

 1 string filePath = @"C:\test.csv"; 2 string delimiter = ","; 3 4 string[][] output = new string[][]{ 5 new string[]{"Col 1 Row 1", "Col 2 Row 1", "Col 3 Row 1"}, 6 new string[]{"Col1 Row 2", "Col2 Row 2", "Col3 Row 2"} 7 }; 8 int length = output.GetLength(0); 9 StringBuilder sb = new StringBuilder(); 10 for (int index = 0; index < length; index++) 11 sb.AppendLine(string.Join(delimiter, output[index])); 12 13 File.WriteAllText(filePath, sb.ToString()); 

您可以使用选项卡'\t'分割lines[]中的每个string,然后将每个值写入相应的单元格。 这是一个应该让你开始的例子:

  for(int i = 0; i < lines.Length; i++) { String line = lines[i]; Excel.Range c1 = (Excel.Range)xlWs.Cells[i+1, 1]; Excel.Range c2 = (Excel.Range)xlWs.Cells[i+1, 1 + line.Length]; Excel.Range range = xlWs.get_Range(c1, c2); string[] split = line.Split('\t'); for (int c = 1; c <= split.Length; c++) { range.Cells[1, c] = split[c-1]; } } 

您可能有兴趣使用Excel的“ Text to Columnsfunction,这对大数据集更有效,而不是循环遍历单元格。

以下是您可能适应从C#运行的Excellogging的macros。 我首先把它转换成~ ,但也可能是其他的一些字符。

 Sub Macro1() Selection.Replace What:="\t", Replacement:="~", LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _ TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _ Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _ :="~", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1)), _ TrailingMinusNumbers:=True End Sub 

修改从C#工作可能有点棘手,但是了解这个选项是有用的。