求助,对C#里逐行读取TXT文本到新建WORD并进行排版的问题?
逐行读取TXT文本到新建WORD并进行排版,比如说把其中某一行的字体变成部一样的字体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.Threading;
using Word = Microsoft.Office.Interop.Word;
namespace 生成WORD文件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Word.Application G_wa;//定义Word应用程序字段
private object G_missing = //定义missing字段并赋值
System.Reflection.Missing.Value;
private object G_str_path;//定义文件保存路径字段
private FolderBrowserDialog G_FolderBrowserDialog;//定义文件夹浏览对话框字段
private OpenFileDialog G_OpenFileDialog;
private void btn_data_Click(object sender, EventArgs e)
{
G_OpenFileDialog = new OpenFileDialog();//创建文件对话框对象
G_OpenFileDialog.Filter = "所有文件(*.*)|*.*";//筛选文件
DialogResult P_DialogResult = //显示文件对话框
G_OpenFileDialog.ShowDialog();
if (P_DialogResult == DialogResult.OK)//确认已经选择文件
{
txt_txtpath.Text = //显示打开文件路径
G_OpenFileDialog.FileName;
btn_word.Enabled = true;//启用浏览按钮
}
}
private void btn_word_Click(object sender, EventArgs e)
{
G_FolderBrowserDialog =//创建浏览文件夹对象
new FolderBrowserDialog();
DialogResult P_DialogResult = //浏览文件夹
G_FolderBrowserDialog.ShowDialog();
if (P_DialogResult == DialogResult.OK)//确认已经选择文件夹
{
btn_new.Enabled = true;//启用新建按钮
word_path.Text = //显示选择路径
G_FolderBrowserDialog.SelectedPath;
}
}
private void btn_new_Click(object sender, EventArgs e)
{
btn_new.Enabled = false;//停用新建按钮
ThreadPool.QueueUserWorkItem(//使用线程池
(P_temp) =>//使用lambda表达式
{
G_wa = new Word.Application();//创建Word应用程序对象
Word.Document P_wd = G_wa.Documents.Add(//建立新文档
ref G_missing, ref G_missing, ref G_missing, ref G_missing);
P_wd.PageSetup.PageHeight = G_wa.CentimetersToPoints(27.94F);
P_wd.PageSetup.PageWidth = G_wa.CentimetersToPoints(24.13F);
P_wd.PageSetup.LeftMargin = G_wa.CentimetersToPoints(2f);
P_wd.PageSetup.RightMargin = G_wa.CentimetersToPoints(2f);
G_wa.Selection.ParagraphFormat.LineSpacing = 10f;//设置文档的行间距
G_wa.Selection.ParagraphFormat.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceExactly;//设置文档的行间距
Word.Range P_Range = P_wd.Paragraphs[1].Range;
P_Range.Font.Name = "宋体";
P_Range.Font.Size = 9;
//using (StreamReader P_StreamReader =//创建流读取器对象
// new StreamReader(G_OpenFileDialog.FileName, Encoding.Default))
//{
// P_Range.Text = P_StreamReader.ReadToEnd();
//}
G_str_path = string.Format(//计算文件保存路径
@"{0}\{1}", G_FolderBrowserDialog.SelectedPath,
DateTime.Now.ToString("yyyy年M月d日h时s分m秒fff毫秒") + ".doc");
P_wd.SaveAs(//保存Word文件
ref G_str_path,
ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing);
((Word._Application)G_wa.Application).Quit(//退出应用程序
ref G_missing, ref G_missing, ref G_missing);
this.Invoke(//开始执行窗体线程
(MethodInvoker)(() =>//使用lambda表达式
{
btn_Display.Enabled = true;//启用显示按钮
MessageBox.Show("成功创建Word文档!", "提示!");//弹出消息对话框
}));
});
}
}
}
以上是代码,已可以生成WORD,并可以对全篇文档进行调节,但是无法单行进行定位和调整,求高手帮忙。。。。
补充:.NET技术 , C#