C#实现Word转换TXT的方法详解
作者:芝麻粒儿 发布时间:2022-12-26 04:27:57
标签:C#,Word,TXT
实践过程
效果
代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static void WordToHtmlText(string WordFilePath)
{
try
{
Microsoft.Office.Interop.Word.Application wApp = new Microsoft.Office.Interop.Word.Application();
//指定原文件和目标文件
object docPath = WordFilePath;
string htmlPath;
if (WordFilePath.Contains(".docx"))
{
htmlPath = WordFilePath.Substring(0, WordFilePath.Length - 4) + "txt";
}
else
{
htmlPath = WordFilePath.Substring(0, WordFilePath.Length - 3) + "txt";
}
object Target = htmlPath;
//缺省参数
object Unknown = Type.Missing;
//只读方式打开
object readOnly = true;
//打开doc文件
Microsoft.Office.Interop.Word.Document document = wApp.Documents.Open(ref docPath, ref Unknown,
ref readOnly, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown);
// 指定格式
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatText;
// 转换格式
document.SaveAs(ref Target, ref format,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown);
// 关闭文档和Word程序
document.Close(ref Unknown, ref Unknown, ref Unknown);
wApp.Quit(ref Unknown, ref Unknown, ref Unknown);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
WordToHtmlText(textBox1.Text.Trim());
MessageBox.Show("转换成功,保存在Word文件的同目录下!");
}
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
textBox1.Text = openFileDialog1.FileName;
}
}
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// button2
//
this.button2.Location = new System.Drawing.Point(154, 50);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 7;
this.button2.Text = "转换";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Enabled = false;
this.textBox1.Location = new System.Drawing.Point(94, 23);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(206, 21);
this.textBox1.TabIndex = 6;
//
// button1
//
this.button1.Location = new System.Drawing.Point(306, 21);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 4;
this.button1.Text = "浏览";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(11, 26);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(77, 12);
this.label1.TabIndex = 5;
this.label1.Text = "选择Word文档";
//
// openFileDialog1
//
this.openFileDialog1.Filter = "Word文档|*.doc";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(413, 92);
this.Controls.Add(this.button2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
}
来源:https://zhima.blog.csdn.net/article/details/128358323


猜你喜欢
- 题目要求思路一:反向点+并查集根据题意不喜欢就不在一个组可以想到使用并查集,本题是两个集合所以对每一个节点引入一个反向点,使两者分属于不同集
- 今天在接手别人的一个项目的时候遇到一个坑,坑死我了;是一个打包的问题,好不容易我把代码写完了准备打包测试了,结果java -jar xxx.
- 1.概述在之前的博文中简单介绍过如何实现fragment之间的信息交互:《Android中Fragment与Activity之间的交互(两种
- 目前经常出现的时间有三个:本地时间(locale time)格林威治时间(Greenwich Mean Time GMT)时间协调时间 (U
- 目录MeasureSpecLayoutParamViewViewGroupTextColumn使用总结
- 本文汇总了android 8种对话框(Dialog)使用方法,分享给大家供大家参考,具体内容如下1.写在前面Android提供了丰富的Dia
- 先看下这个问题的背景:假设有一个spring应用,开发人员希望自定义一个注解@Log,可以加到指定的方法上,实现自动记录日志(入参、出参、响
- 加锁和解锁我们来看下ReentrantLock的基本用法ThreadDomain35类public class ThreadDomain35
- 快速幂取模算法的引入是从大数的小数取模的朴素算法的局限性所提出的,在朴素的方法中我们计算一个数比如5^1003%31是非常消耗我们的计算资源
- 本文实例讲述了C#可用于登录验证码的四位随机数生成方法。分享给大家供大家参考。具体实现方法如下:这里提供了两种方法,调用其一即可:using
- 本文实例为大家分享了Android自定义view完成车载可调整轨迹线的具体代码,供大家参考,具体内容如下同事做的view,拿过来做个记录。/
- 目录java 反射调用Service导致Spring注入Dao失效问题发生背景:1、错误方法:通过反射执行service的方法2、解决方法:
- 本文实例为大家分享了Unity UI实现拖拽旋转的具体代码,供大家参考,具体内容如下跟随鼠标旋转第一种效果是跟随鼠标旋转,原理是计算下鼠标位
- 一、关键字关键字:被Java语言赋予特定含义的单词。组成关键字的字母全部小写。注:goto和const作为保留字存在,目前并不使用。main
- 前言刚看到这个题目的朋友第一反应肯定是好奇,之后再细细思考下就会发现这个题目眼熟了。就算是同一个答案,如果提问的方式不同,往往会对回答造成干
- Spring security 重写Filter实现json登录在使用SpringSecurity中,大伙都知道默认的登录数据是通过key/
- 什么是jdkjdk是什么呢?jdk的是java development kit的缩写,意思是java程序开发的工具包。也可以说jdk是jav
- 1. Mybatis分页插件1.1 分页插件介绍分页可以将很多条结果进行分页显示。如果当前在第一页,则没有上一页。如果当前在最后一页,则没有
- 按行读取文件package test; import java.io.*; import java.util.*; public class
- 上篇文章给大家介绍了新浪微博第三方登录界面上下拉伸图片之第三方开源PullToZoomListViewEx(一),需要了解的朋友可以点击了解