c#如何循环将一个txt的内容写入excel

yby123 2010-11-20 10:11:03
txt内容:

csdn
论坛
中的前辈
非常感谢

要将“csdn”写入excel的第一行第一列,“论坛”写入excel第一行第二列

“中的前辈”写入excel第二行第二列
“非常感谢”写入excel第三行第四列

并且使用循环写入,可是在我这个while循环中我不知道怎么定位写入了。希望有人能给予指点。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using Microsoft.Office.Interop.Excel;
using System.Reflection;



namespace 从文本写入excel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
int counter = 0;
string str;
string line;
string myxlspath = System.IO.Directory.GetCurrentDirectory() + @"\myexcel.xls";
string mylogpath = System.IO.Directory.GetCurrentDirectory()+@"\temp.txt";
FileStream fs = new FileStream(mylogpath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);//加入System.Text.Encoding.Default出现汉字不会有乱满
sr.BaseStream.Seek(0, SeekOrigin.Begin);
Microsoft.Office.Interop.Excel.Application xApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
xApp.Visible = true;
Microsoft.Office.Interop.Excel.Workbook xBook = xApp.Workbooks._Open(myxlspath, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
//设定预写入的页为sheet1
Microsoft.Office.Interop.Excel.Worksheet xSheet = (Microsoft.Office.Interop.Excel.Worksheet)xBook.Sheets[1];
//int lineCount = 0; //文本行数计数器出示

//while (sr.Peek() >= 0) //取文本行数
//{
// sr.ReadLine();
// lineCount ++;

// //textBox1.Text = lineCount.ToString();
// string unm = lineCount.ToString();
// textBox1.Text = unm ;

//}


while ((str = sr.ReadLine()) != null)
{


textBox1.Text += str;
//xSheet.Cells[1, 1] = str;
//xSheet.Cells[1, 2] = str;
//counter++;

}






sr.Close();
fs.Close();





}
}
}





...全文
288 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
luoyuanxd 2010-11-22
  • 打赏
  • 举报
回复
用Excel.Worksheet.getRange()来定位写入Excel的位置,内容你自己控制就好了。具体给你个例子:
Excel.Worksheet worksheet;
worksheet.get_Range(worksheet.Cells[1,1],worksheet.Cells[1,2])
就是定位第一行的一二两列。
yun_feiyang 2010-11-22
  • 打赏
  • 举报
回复


for(int i = 0;i < 4;i++)
{
xSheet.Cells[i, i] = "csdn";
xSheet.Cells[i, i] = "论坛";
xSheet.Cells[i, i] = "中的前辈";
xSheet.Cells[i, i] = "非常感谢";
}

// 打印
xSheet.Protect(System.DateTime.Now.ToString(), Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
yby123 2010-11-21
  • 打赏
  • 举报
回复
怎么没有人关注啊??
yby123 2010-11-20
  • 打赏
  • 举报
回复
我感觉应该是while ((str = sr.ReadLine()) != null)中做点什么

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧