c# – 如何使用制表符分隔符将字符串数组写入Excel文件?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何使用制表符分隔符将字符串数组写入Excel文件?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3013字,纯文字阅读大概需要5分钟。
内容图文
![c# – 如何使用制表符分隔符将字符串数组写入Excel文件?](/upload/InfoBanner/zyjiaocheng/824/f2f8b3cf21944d719f5e5c0201d9b21f.jpg)
我正在创建一个小应用程序,它读取制表符分隔的文本文件,进行一些更改,然后创建一个Excel 2007 .xlsx文件.我无法弄清楚如何从字符串数组中取出行并将它们写入Excel文件,使用选项卡将行分成列.我希望这是有道理的.
我有字符串Lines []包含这样的东西:
Item1\tItem2\tItem3\tItem4
ItemA\tItemB\tItemC\tItemD
Item5\tItem6\tItem7\tItem8
我想创建一个如下所示的Excel文件:
A B C D
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;
任何意见,将不胜感激!谢谢!
这是我目前得到的输出:
A B C D
Item1\tItem2\tItem3\tItem4
Item1\tItem2\tItem3\tItem4
Item1\tItem2\tItem3\tItem4
编辑:我已经用@ jiverson的建议更新了我的代码,现在该行已分成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
);
}
内容总结
以上是互联网集市为您收集整理的c# – 如何使用制表符分隔符将字符串数组写入Excel文件?全部内容,希望文章能够帮你解决c# – 如何使用制表符分隔符将字符串数组写入Excel文件?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。