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. Linux kernel get_prng_bytes函数数字错误漏洞

    漏洞名称: Linux kernel get_prng_bytes函数数字错误漏洞 CNNVD编号: CNNVD-201310-142 发布时间: 2013-10-11 更新时间: 2013-10-1 ...

  2. java 全角半角转换函数

    /** * 半角转全角 * @param input String. * @return 全角字符串. */ public static String ToSBC(String input) { ch ...

  3. 【转】 Android SDK无法更新解决方法---不错

    原文网址:http://blog.csdn.net/shi_weihappy/article/details/41847997 自己的修改: 203.208.39.238 dl.google.com7 ...

  4. 【转】windows7 64位系统认不出8g内存显示只有3G可用

    原文网址:http://www.jb51.neos/windows/93721.html   我的电脑安装的是Win7 64位系统,当时内存是用的8G的,系统里面显示出来只有3.00G可用,真是崩溃啊 ...

  5. python urllib2模块携带cookie

    今天干活遇到一个事.有一些网站的一些操作非得要求你登陆才能做,比如新浪微博,你要随便看看吧,不行,非得让你登陆了才能看,再比如一些用户操作,像更改自己的资料啦,个人的隐私啦巴拉巴拉的.想抓取这样的ur ...

  6. Win7,8的上帝模式文件夹 GodMode.{ED7BA470-8E54-465E-825C-99712043E01C}

    新建一文件夹,命名为:“GodMode.{ED7BA470-8E54-465E-825C-99712043E01C}”, 创建后双击进入,可以看到计算机所有的配置选项信息.

  7. input框自动填充内容背景颜色为黄色解决方法

    谷歌浏览器input自动填充内容,背景色会是黄色,想改的话: input:-webkit-autofill { box-shadow: 0 0 0px 1000px white inset;} 这种方 ...

  8. Objective-C categories in static library

    ASK: Can you guide me how to properly link static library to iphone project. I use staic library pro ...

  9. URAL 1146 Maximum Sum(最大子矩阵的和 DP)

    Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...

  10. linux 通用时钟框架CCF

    linux CCF 时钟框架 简单介绍 这里讲的时钟是给soc各组件提供时钟的树状框架,并非内核使用的时间,和其它模块一样,clk也有框架,用以适配不同的平台.适配层之上是客户代码和接口,也就是各模块 ...