A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 赵玮 黑马帝   /  2011-12-27 23:15  /  3491 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 赵玮 于 2012-1-9 15:58 编辑

我想将doc命令中显示的代码生成一个txt文档,请问该如何操作?

5 个回复

倒序浏览
打开doc格式的文件,在菜单栏 >>文件 >>另存为... >>在另存为对话框的保存类型中选择后缀名为.TXT的 >>保存,即可.

打开一个文本文件,把doc中的内容复制到文本文件中也可.
回复 使用道具 举报
/------------------------------------------------------------------------------
  //版权所有 (C) 浪潮集团商用系统有限公司  保留所有权利
  //文件名称: wordtohtml           文件版本: 1.00.00
  //作    者: 郭铸     作者邮箱: guozhu@langchao.com  完成日期: 2004-10-20
  //文件描述:
  //其它描述:
  //类 列 表:
  //  wordtohtml: 将指定目录下面所有的doc文件转化为HTML并存储在相同目录下
  //修改历史:
  //  #   版本     修改日期    作者                 修改内容
  //  ----------------------------------------------------------------------------
  //  1   1.00.01  2004-10-14  作者姓名             修改内容描述
  //  ----------------------------------------------------------------------------
  //------------------------------------------------------------------------------
  import com.jacob.com.*;
  import com.jacob.activeX.*;
  import java.io.*;

  //取得指定目录下面所有的doc文件名称
  public class wordtohtml
  {
  //------------------------------------------------------------------------------
  //方法原型: change(String paths)
  //功能描述: 将指定目录下面所有的doc文件转化为HTML并存储在相同目录下
  //输入参数: String
  //输出参数: 无
  //返 回 值: 无
  //其它说明: 递归
  //------------------------------------------------------------------------------
  public static void change(String paths, String savepaths)
  {
  
  File d = new File(paths);
  //取得当前文件夹下所有文件和目录的列表
  File lists[] = d.listFiles();
  String pathss = new String("");

  //对当前目录下面所有文件进行检索
  for(int i = 0; i < lists.length; i ++)
  {
  if(lists.isFile())
  {
  String filename = lists.getName();
  String filetype = new String("");
  //取得文件类型
  filetype = filename.substring((filename.length() - 3), filename.length());
  
  //判断是否为doc文件
  if(filetype.equals("doc"))
  {
  System.out.println("当前正在转换......");
  //打印当前目录路径
  System.out.println(paths);
  //打印doc文件名
  System.out.println(filename.substring(0, (filename.length() - 4)));
  
  ActiveXComponent app = new ActiveXComponent("Word.Application");//启动word
  
  String docpath = paths + filename;
  String htmlpath = savepaths + filename.substring(0, (filename.length() - 4));
  
  String inFile = docpath;
  //要转换的word文件
  String tpFile = htmlpath;
  //HTML文件

  boolean flag = false;
  
  try
  {
  app.setProperty("Visible", new Variant(false));
  //设置word不可见
Object docs = app.getProperty("Documents").toDispatch();
  Object doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch();
  //打开word文件
  Dispatch.invoke(doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
  //作为html格式保存到临时文件
  Variant f = new Variant(false);
  Dispatch.call(doc, "Close", f);
  flag = true;
  }
  catch (Exception e)
  {
  e.printStackTrace();
  }
  finally
  {
  app.invoke("Quit", new Variant[] {});
  }
  System.out.println("转化完毕!");
  }
  }
  else
  {
  pathss = paths;
  //进入下一级目录
  pathss = pathss + lists.getName() + "\\";   
  //递归遍历所有目录
  change(pathss, savepaths);
  }
  }
  
  }
  //------------------------------------------------------------------------------
  //方法原型: main(String[] args)
  //功能描述: main文件
  //输入参数: 无
  //输出参数: 无
  //返 回 值: 无
  //其它说明: 无
  //------------------------------------------------------------------------------
  public static void main(String[] args)
  {
  
  String paths = new String("D:\\Work\\2004.10.8\\test system\\test01\\word\\");
  String savepaths = new String ("D:\\Work\\2004.10.8\\test system\\test01\\html\\");

  change(paths, savepaths);

  }
  }

  其中import的jar包是一个开源的东东,网上搜索即得。
  Dispatch.invoke(doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
  修改Variant(8)},里面得参数即可将Word转化为各种类型。
回复 使用道具 举报
将DOC文件转换成无格式的TXT文件:

Option Explicit   
   
  Public Function GetDocText(ByVal sFQFilename As String) As String   
  '提取Word文档的纯文本。   
  'INPUT-----------------------------------------------------------   
  'sFQFilename Word文档的全路径名   
  'OUTPUT----------------------------------------------------------   
  'Return Value 提取的纯文本   
  '----------------------------------------------------------------   
   
  Dim Wapp As Object, Doc As Object 'Word Application 对象、Document 对象   
   
  try: On Error GoTo catch   
  '{   
  Set Wapp = CreateObject("Word.Application") '创建Word Application 对象   
  Set Doc = Wapp.Documents.Open(sFQFilename) '打开文档,返回一个文档对象   
  GetDocText = Doc.Content.Text '提取纯文本   
  '}   
  GoTo finally   
  catch:   
  '{   
  GetDocText = ""   
  '}   
  finally:   
  '{   
  '如果文档对象不为空,那么说明打开了文档,关闭它,并销毁文档对象   
  If Not (Doc Is Nothing) Then Doc.Close: Set Doc = Nothing   
  '如果word application对象不为空,那么说明创建了word对象,   
  '退出它,并销毁对象   
  If Not (Wapp Is Nothing) Then Wapp.Quit: Set Wapp = Nothing   
  '}   
   
  End Function   
   
  Public Function HasWordInstalled() As Boolean   
  '判断用户的电脑是否安装有word。如果没有安装word或是word版本过低(无法提供外部可创建对象)   
  '则返回FALSE。   
  Dim Wapp As Object   
   
  On Error Resume Next   
   
  Err.Clear   
   
  Set Wapp = CreateObject("Word.Application")   
   
  If Err = 0 Then '无错误,创建成功,说明安装了Word,返回真   
  HasWordInstalled = True   
   
  '释放资源   
  Wapp.Quit: Set Wapp = Nothing   
  Else   
  HasWordInstalled = False '有错误发生,返回FAlSE   
  End If   
   
  End Function   
   



Public Function GetDocText(ByVal sFQFilename As String) As String   
  '提取Word文档的纯文本。   
  'INPUT-----------------------------------------------------------   
  'sFQFilename Word文档的全路径名   
  'OUTPUT----------------------------------------------------------   
  'Return Value 提取的纯文本   
  '----------------------------------------------------------------   
   
  Dim Wapp As Object, Doc As Object 'Word Application 对象、Document 对象   
   
  try: On Error GoTo catch   
  '{   
  Set Wapp = CreateObject("Word.Application") '创建Word Application 对象   
  Set Doc = Wapp.Documents.Open(sFQFilename) '打开文档,返回一个文档对象   
  GetDocText = Doc.Content.Text '提取纯文本   
  '***********************************************   
  '加上这几句,是保存GetDocText的内容到一个新的Word里,分段标志还在,但是首行缩进标志没了   
  Set Doc = Wapp.documents.Add   
  Wapp.Selection.TypeText GetDocText   
  Doc.SaveAs "c:\temp.doc"   
  '************************************************   
  '}   
  GoTo finally   
  catch:   
  '{   
  GetDocText = ""   
  '}   
  finally:   
  '{   
  '如果文档对象不为空,那么说明打开了文档,关闭它,并销毁文档对象   
  If Not (Doc Is Nothing) Then Doc.Close: Set Doc = Nothing   
  '如果word application对象不为空,那么说明创建了word对象,   
  '退出它,并销毁对象   
  If Not (Wapp Is Nothing) Then Wapp.Quit: Set Wapp = Nothing   
  '}   
   
  End Function  
回复 使用道具 举报
hashcode不是真的内存地址,hashcode只是内存地址的一个引用,因为对象的散列码是可以在hashcode方法里自己定义的.所以说hashcode相等内存地址不一定相等,他与equals是对应的,指是比较两个对象内容是否相等时才用到的
回复 使用道具 举报
本帖最后由 苳眠 于 2012-1-8 12:13 编辑

首先将你要拷贝的内容选定并复制.这里用*表示你复制的内容(在dos里面复制就是选定好你要拷贝的内容后按enter.让后在你要粘贴的地方右击一下就可以粘贴了)
后在dos中输入 echo   *>11.txt  (然后回车)
注意:命令中的*是粘贴上去的.
这样就会在所在的路径中生成一个名为11.txt的文本文件,里面的内容就是*(代表你复制的内容).

点评

LZ 问的应该是这个答案吧...  发表于 2012-1-10 10:00
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马