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 Microsoft.Office.Interop.Excel;
using System.Reflection; namespace ReadTextToExcel
{
public partial class FrmReadTxtToExcel : Form
{
public FrmReadTxtToExcel()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
this.ofdRead.ShowDialog();
this.tbxReadFile.Text = this.ofdRead.FileName; } private void FrmReadTxtToExcel_Load(object sender, EventArgs e)
{ } private void btnWrite_Click(object sender, EventArgs e)
{
this.ofdWrite.ShowDialog();
this.tbxWriteFile.Text = this.ofdWrite.FileName;
} private void btnOperate_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(this.tbxReadFile.Text.ToString(), FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs,System.Text.Encoding.GetEncoding("gb2312"));
Microsoft.Office.Interop.Excel.Application xApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
xApp.Visible = true;
//得到WorkBook对象, 可以用两种方式之一: 下面的是打开已有的文件
Microsoft.Office.Interop.Excel.Workbook xBook = xApp.Workbooks._Open(this.tbxWriteFile.Text.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);
//xBook=xApp.Workbooks.Add(Missing.Value);//新建文件的代码 //指定要操作的Sheet,如下两种方式
Microsoft.Office.Interop.Excel.Worksheet xSheet = (Microsoft.Office.Interop.Excel.Worksheet)xBook.Sheets[];
//Excel.Worksheet xSheet=(Excel.Worksheet)xApp.ActiveSheet; sr.BaseStream.Seek(, SeekOrigin.Begin); //定位操作点,begin 是一个参考点
sr.BaseStream.Seek(, SeekOrigin.Begin); //读一下,看看文件内有没有内容,为下一步循环 提供判断依据
string str = sr.ReadLine();//如果 文件有内容
int index = ;
while (str != null)
{
//输出字符串,str 在上面已经定义了 读入一行字符
this.textBox3.Text = textBox3.Text.ToString() + str; //写入数据
Microsoft.Office.Interop.Excel.Range rng3 = xSheet.get_Range(Convert.ToString("A"+index), Missing.Value);
rng3.Value2 = str;
//rng3.Interior.ColorIndex = 6; //设置Range的背景色
index++;
str = sr.ReadLine();
}
//C#读取TXT文件之后关闭文件,注意顺序,先对文件内部进行关闭,然后才是文件
sr.Close();
fs.Close(); //保存方式一:保存WorkBook
//xBook.SaveAs(this.textBox2.Text.ToString(),
//Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
//Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value,
//Missing.Value, Missing.Value); //保存方式二
xBook.Save();
xSheet = null;
xBook = null;
xApp.Quit(); //这一句是非常重要的,否则Excel对象不能从内存中退出
xApp = null;
} private void btnExit_Click(object sender, EventArgs e)
{
this.Dispose();
System.Windows.Forms.Application.Exit();
} }
}

C# 读取txt文本内容写入到excel的更多相关文章

  1. C++ 读取txt文本内容,并将结果保存到新文本

    循序渐进学习读文件 // readFile.cpp : 定义控制台应用程序的入口点. #include "stdafx.h" #include <iostream> # ...

  2. python3+xlwt 读取txt信息并写入到excel中

    # coding = utf-8 import os import xlwt import re def readTxt_toExcel(valueList, Pathlist): workbook ...

  3. PHP将富文本编辑后的内容,去除样式图片等只保留txt文本内容

    1.从数据库读取富文本内容样式如下: <p style=";text-indent: 0;padding: 0;line-height: 26px"><span ...

  4. PHP将富文本内容去除各类样式图片等只保留txt文本内容(作用于SEO的description)

    1.从数据库读取富文本内容样式如下: <p style=";text-indent: 0;padding: 0;line-height: 26px"><span ...

  5. Java HashSet对txt文本内容去重(统计小说用过的字或字数)

    Java HashSet对txt文本内容去重(统计小说用过的字或字数) 基本思路: 1.字节流读需要去重的txt文本.(展示demo为当前workspace下名为utf-8.txt的文本) 2.对读取 ...

  6. C#异步将文本内容写入文件

    在C#/.NET中,将文本内容写入文件最简单的方法是调用 File.WriteAllText() 方法,但这个方法没有异步的实现,要想用异步,只能改用有些复杂的 FileStream.WriteAsy ...

  7. java读取txt文件内容

    package read; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; public ...

  8. .Net ->> iTextSharp工具读取PDF文本内容

    分享一个开源的C#DLL,可以读取PDF文本内容. 地址:http://sourceforge.net/projects/itextsharp/ 这里还有相关的链接:http://www.codepr ...

  9. python读取、写入txt文本内容

    转载:https://blog.csdn.net/qq_37828488/article/details/100024924 python常用的读取文件函数有三种read().readline().r ...

随机推荐

  1. Phpwind v9.0 存储型xss跨站漏洞

    漏洞版本: Phpwind v9.0 漏洞描述: Phpwind专注于中小网站应用的整合和价值的发掘,我们认为,以社区为网站的基础,可以提供丰富的应用,满足人们获取信息.交流.娱乐.消费等生活需求.获 ...

  2. 【转】Android中如何使用Bundle传递对象[使用Serializable或者Parcelable] -- 不错

    原文网址:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1211/694.html Android中Bundle类的作用 Bun ...

  3. sharepoint 2010 如何创建文档库内容类型content type

    转:http://biancheng.dnbcw.info/linux/441643.html 这次主要是记录下,如何来创建文档内容类型,例如新建文档的时候,可以选择不同模板,有word,excel文 ...

  4. 数学:lucas定理的总结

    今天考试的题目中有大组合数取模,不会唉,丢了45分,我真是个弱鸡,现在还不会lucas. 所以今天看了一下,定理差不多是: (1)Lucas定理:p为素数,则有: 即:lucas(n,m,p)=c(n ...

  5. jquery+NHibernate3.3.3+MVC的分页效果

    目录结构 先设计数据表结构 DROP SEQUENCE BDLLY_2V.SEQ_CUSTOMER; CREATE SEQUENCE BDLLY_2V.SEQ_CUSTOMER START MAXVA ...

  6. IIS 7.0, ASP.NET, pipelines, modules, handlers, and preconditions

    1.0 What is the IIS Pipeline Conceptually, the IIS pipeline is a state machine with the following st ...

  7. Linux 修改默认文件关联打开程序

    从总体上讲 /etc/gnome/defaults.list 保存了全局的打开方式-/.local/share/applications/mimeapps.list 保存了个人的打开方式当这两个文件不 ...

  8. ARM学习笔记11——GNU ARM汇编程序设计

    GNU ARM汇编程序设计中,每行的语法格式如下: [<label>:] [<instruction | directive | pseudo-instruction>] @c ...

  9. hadoop的安装

    1. 获取linux操作系统 可以申请云主机. 2. 安装JDK,配置环境变量 sudo apt-get install openjdk-7-jdk vim /etc/profile 在配置文件中配置 ...

  10. [置顶] 深入理解android之IPC机制与Binder框架

    [android之IPC机制与Binder框架] [Binder框架.Parcel.Proxy-Stub以及AIDL] Abstract [每个平台都会有自己一套跨进程的IPC机制,让不同进程里的两个 ...