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
0
投稿
猜你喜欢
- 程序的结构分类:顺序结构:按照写代码的顺序 一次执行选择结构:根据条件的不同有选择的执行不同的代码循环结构:在一定条件下 反复执行某一片代码
- maven周期maven的生命周期不止package,compile,clean。其实这是主要部分。以下截图其实展示的是maven的所有周期
- 前言我们在日常的开发过程中针对一些字段采用整型的方式去代替某些具体的含义,比如性别0代表男,1代表女。如果只是一些不会变更的转译我们可以采用
- 本文实例讲述了java实现新浪微博Oauth接口发送图片和文字的方法。分享给大家供大家参考。具体如下:基于网上很多人利用新浪api开发新浪微
- Maven打包没有指定主类在使用IDEA开发项目的时候经常会遇到使用Maven打包项目(打成jar包或者zip包),但是之前不太清楚打包插件
- 秒杀功能秒杀场景现在已经非常常见了,各种电商平台都有秒杀的产品,接下来我们模拟一个秒杀的项目,最终能够确保高并发下的线程安全。界面比较简单,
- 一、JDBC简介JDBC是连接java应用程序和数据库之间的桥梁。什么是JDBC?Java语言访问数据库的一种规范,是一套API。JDBC
- 目录对象的创建创建方式对象的内存布局创建过程对象的内存分配分配方式并发安全代码优化逃逸分析的不成熟性实际的对象空间分配过程对象的访问句柄直接
- Springboot使用test无法启动test无法启动,遇到java.lang.IllegalStateException: Unable
- 1.给定时间戳返回指定的时间格式private string StampToDate(string timeStamp,string for
- 目录前言简单示例基础操作定时取消关联取消判断取消源码探究构造入手小插曲WaitHandle注册操作取消操作Cancel操作CancelAft
- 实例如下:package com.bwsk.modules.weixin.util;import java.util.Random;/**
- Android手势解锁本文讲述的是一个手势解锁的库,可以定制显示隐藏宫格点、路径、并且带有小九宫格显示图,和震动!让你学会使用这个简单,高效
- 这篇文章主要介绍了Java并发CopyOnWrite容器原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值
- 1、满二叉树、完全二叉树、平衡二叉树、红黑树、二叉搜索树的区别?参考文章:树、二叉树(完全二叉树、满二叉树)概念图解① 满二叉树高度为&nb
- 本文介绍了Java实现动态获取图片验证码的示例代码,分享给大家,具体如下:import javax.imageio.ImageIO;impo
- 涉及知识点在本示例中,从数据绑定,到数据展示,涉及知识点如下所示:DataGrid,要WPF提供的进行二维数据展示在列表控件,默认功能非常简
- 这篇文章主要介绍了springboot多租户设计过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 本文主要关注如何使用mybatis/mybatis plus连接SQL Server数据库,因此将省略其他项目配置、代码。框架选择应用框架:
- JVM内存组成结构JVM栈由堆、栈、本地方法栈、方法区等部分组成,结构图如下所示:1)堆所有通过new创建的对象的内存都在堆中分配,其大小可