java提取docx内嵌附件

如何编程将word中的附件(嵌入式对象)保存到文件? 有使用过word另存为docx格式,解压再解析embeddings

1、不明白为什么要解压缩出来,也许LZ是希望提取Word中的各种“对象”吧

包括word、ppt、excel等,然后将提取的这些附件“另存为”一个一个单独的文件,如果是这样的话

那么可以参考:向word插入一个已经存在的excel

object oMissing = System.Reflection.Missing.Value;

Microsoft.Office.Interop.Word.Application word = 

    new Microsoft.Office.Interop.Word.Application();//创建word对象

word.Visible = true;//显示出来

Microsoft.Office.Interop.Word.Document dcu = 

    word.Documents.Add(ref oMissing, ref oMissing,

    ref oMissing, ref oMissing);//创建一个新的空文档,格式为默认的

dcu.Activate();//激活当前文档

object type = @”Excel.Sheet.12″;//插入的excel 格式,这里我用的是excel 2010,所以是.12

object filename = @”C:\list.xlsx”;//插入的excel的位置

word.Selection.InlineShapes.AddOLEObject(

    ref type, ref filename, ref oMissing, ref oMissing);//执行插入操作

那么只需要进行相反的操作就可以了,所以这里的关键是获取Document.InlineShapes集合对象,通过该对象识别插入的“对象”,然后再将这些对象转换为合适的类型,通过SaveAs方法另存一下

2、将doc转换为docx,原理类似,我的方法是在安装了word2007(组件版本office12)的前提下,用C#打开文档,然后利用SaveAs方法将其转换为docx(2007)格式

java中如何实现向已有的PDF文件插入附件?

可以用Spire.Pdf for Java类库给PDF文档添加附件,下面的代码是插入Excel和Word附件给你参考:

import com.spire.pdf.annotations.*;

import com.spire.pdf.attachments.PdfAttachment;

import com.spire.pdf.graphics.*;

import java.awt.*;

import java.awt.geom.Dimension2D;

import java.awt.geom.Rectangle2D;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

public class AttachFiles {

public static void main(String[] args) throws IOException {

//创建PdfDocument对象

PdfDocument doc = new PdfDocument();

//加载PDF文档

doc.loadFromFile(“C:\\Users\\Administrator\\Desktop\\sample.pdf”);

//添加附件到PDF

PdfAttachment attachment = new PdfAttachment(“C:\\Users\\Administrator\\Desktop\\使用说明书.docx”);

doc.getAttachments().add(attachment);

//绘制标签

String label = “财务报表.xlsx”;

PdfTrueTypeFont font = new PdfTrueTypeFont(new Font(“Arial Unicode MS”,Font.PLAIN,12),true);

double x = 35;

double y = doc.getPages().get(0).getActualSize().getHeight() – 200;

doc.getPages().get(0).getCanvas().drawString(label, font, PdfBrushes.getOrange(), x, y);

//添加注释附件到PDF

String filePath = “C:\\Users\\Administrator\\Desktop\\财务报表.xlsx”;

byte[] data = toByteArray(filePath);

Dimension2D size = font.measureString(label);

Rectangle2D bound = new Rectangle2D.Float((float) (x + size.getWidth() + 2), (float) y, 10, 15);

PdfAttachmentAnnotation annotation = new PdfAttachmentAnnotation(bound, filePath, data);

annotation.setColor(new PdfRGBColor(new Color(0, 128, 128)));

annotation.setFlags(PdfAnnotationFlags.Default);

annotation.setIcon(PdfAttachmentIcon.Graph);

annotation.setText(“点击打开财务报表.xlsx”);

doc.getPages().get(0).getAnnotationsWidget().add(annotation);

//保存文档

doc.saveToFile(“Attachments.pdf”);

}

//读取文件到byte数组

public static byte[] toByteArray(String filePath) throws IOException {

File file = new File(filePath);

long fileSize = file.length();

if (fileSize Integer.MAX_VALUE) {

System.out.println(“file too big…”);

return null;

}

FileInputStream fi = new FileInputStream(file);

byte[] buffer = new byte[(int) fileSize];

int offset = 0;

int numRead = 0;

while (offset buffer.length (numRead = fi.read(buffer, offset, buffer.length – offset)) = 0) {

offset += numRead;

}

if (offset != buffer.length) {

throw new IOException(“Could not completely read file “

+ file.getName());

}

fi.close();

return buffer;

}

}

效果:

java提取docx内嵌附件

word文档中的附件如何提取

打开一个WORD文档,文档中有另外一个WORD文档附件。双击附件文档,打开附件文件。点击菜单栏上的”文件-另存为“。选择存放的路径,点击保存按钮,附件单独保存下来了,详细步骤:

1、打开一个WORD文档,文档中有另外一个WORD文档附件。

2、双击附件文档,打开附件文件。

3、点击菜单栏上的”文件-另存为“。

4、选择存放的路径,点击保存按钮,附件单独保存下来了。

java读取带格式word内容

用jacob吧。。

/**

*@author eyuan

*/

package per.eyuan.word2txt.core;

import com.jacob.*;

import com.jacob.com.*;

import com.jacob.activeX.*;

import java.io.*;

import java.util.Scanner;

public class Core {

/**

* 实现转换的函数

* @param sourceFilesPath

* @param destinationFilesPath

* @param destinationFilesType

* @return void

* @see import com.jacob.activeX.*;

*/

public static void change(String sourceFilesPath,String destinationFilesPath,int destinationFilesType){

//使用word文件所在的目录(源路径)建立目录文件

File sourcePathFile=new File(sourceFilesPath);

//取得word文件(源文件列表)

File sourceFilesList[]=sourcePathFile.listFiles();

System.out.println(“共有”+sourceFilesList.length+”个文件(文件夹)”);

//指定要转换的文件所在的目录下,如果有子目录,

//则进入子目录,继续查找word文档并将其转换,

//直到将指定目录下的所有word文档转换完。

//子目录名

String sourceChildPath=new String(“”);

//保持原来的层次关系,将子目录下的文件存放在新建的子目录中

String destiNationChildPath=new String(“”);

//检索文件,过滤掉非word文件,通过扩展名过滤

for(int i=0;isourceFilesList.length;i++){

//排除掉子文件夹

if(sourceFilesList[i].isFile()){

System.out.println(“第”+(i+1)+”个文件:”);

//取得文件全名(包含扩展名)

String fileName=sourceFilesList[i].getName();

String fileType=new String(“”);

//取得文件扩展名

fileType=fileName.substring((fileName.length()-4), fileName.length());

//word2007-2010扩展名为docx

//判断是否为word2007-2010文档,及是否以docx为后缀名

if(fileType.equals(“docx”)){

System.out.println(“正在转换。。。”);

//输出word文档所在路劲

System.out.println(“目录:”+sourceFilesPath);

//输出word文档名

System.out.println(“文件名:”+fileName);

//System.out.println(fileName.substring(0, (fileName.length()-5)));

//核心函数

//启动word

ActiveXComponent app=new ActiveXComponent(“Word.Application”);

//要转换的文档的全路径(所在文件夹+文件全名)

String docPath=sourceFilesPath+”\\”+fileName;

//转换后的文档的全路径(所在文件夹+文件名)

String othersPath=destinationFilesPath+”\\”+fileName.substring(0,(fileName.length()-5));

//

String inFile=docPath;

String outFile=othersPath;

//

boolean flag=false;

//核心代码

try{

//设置word可见性

app.setProperty(“Visible”, new Variant(false));

//

Dispatch docs=app.getProperty(“Documents”).toDispatch();

//打开word文档

Dispatch doc=Dispatch.invoke(docs, “Open”, Dispatch.Method, new Object[]{inFile,new Variant(false),new Variant(true)}, new int[1]).toDispatch();

//0:Microsoft Word 97 – 2003 文档 (.doc)

//1:Microsoft Word 97 – 2003 模板 (.dot)

//2:文本文档 (.txt)

//3:文本文档 (.txt)

//4:文本文档 (.txt)

//5:文本文档 (.txt)

//6:RTF 格式 (.rtf)

//7:文本文档 (.txt)

//8:HTML 文档 (.htm)(带文件夹)

//9:MHTML 文档 (.mht)(单文件)

//10:MHTML 文档 (.mht)(单文件)

//11:XML 文档 (.xml)

//12:Microsoft Word 文档 (.docx)

//13:Microsoft Word 启用宏的文档 (.docm)

//14:Microsoft Word 模板 (.dotx)

//15:Microsoft Word 启用宏的模板 (.dotm)

//16:Microsoft Word 文档 (.docx)

//17:PDF 文件 (.pdf)

//18:XPS 文档 (.xps)

//19:XML 文档 (.xml)

//20:XML 文档 (.xml)

//21:XML 文档 (.xml)

//22:XML 文档 (.xml)

//23:OpenDocument 文本 (.odt)

//24:WTF 文件 (.wtf)

//另存为指定格式的文档

Dispatch.invoke(doc, “SaveAs”, Dispatch.Method, new Object[]{outFile,new Variant(destinationFilesType)}, new int[1]);

//

Variant file=new Variant(false);

//关闭文档

Dispatch.call(doc, “Close”,file);

//

flag=true;

}catch(Exception e){

e.printStackTrace();

System.out.println(“文档转换失败”);

}finally{

app.invoke(“Quit”,new Variant[]{});

}

System.out.println(“转换完毕”);

}

//word97-2003扩展名为doc

//判断是否为word2003-2007文档,及是否以doc为后缀名

else if(fileType.equals(“.doc”)){

System.out.println(“正在转换。。。”);

//输出word文档所在路劲

System.out.println(“目录:”+sourceFilesPath);

//输出word文档名

System.out.println(“文件名:”+fileName);

//System.out.println(fileName.substring(0, (fileName.length()-4)));

//核心函数

//启动word

ActiveXComponent app=new ActiveXComponent(“Word.Application”);

//要转换的文档的全路径(所在文件夹+文件全名)

String docPath=sourceFilesPath+”\\”+fileName;

//转换后的文档的全路径(所在文件夹+文件名)

String othersPath=destinationFilesPath+”\\”+fileName.substring(0,(fileName.length()-4));

//

String inFile=docPath;

String outFile=othersPath;

//

boolean flag=false;

//核心代码

try{

//设置word可见性

app.setProperty(“Visible”, new Variant(false));

//

Dispatch docs=app.getProperty(“Documents”).toDispatch();

//打开word文档

Dispatch doc=Dispatch.invoke(docs, “Open”, Dispatch.Method, new Object[]{inFile,new Variant(false),new Variant(true)}, new int[1]).toDispatch();

//另存为指定格式的文档

Dispatch.invoke(doc, “SaveAs”, Dispatch.Method, new Object[]{outFile,new Variant(destinationFilesType)}, new int[1]);

//

Variant file=new Variant(false);

//关闭文档

Dispatch.call(doc, “Close”,file);

//

flag=true;

}catch(Exception e){

e.printStackTrace();

System.out.println(“文档转换失败”);

}finally{

app.invoke(“Quit”,new Variant[]{});

}

System.out.println(“转换完毕”);

}

//文档的扩展名不是doc或docx

else{

System.out.println(“非word文档”);

}

}

//如果是子文件夹,则递归遍历,将所有的word文档转换

else{

//

sourceChildPath=sourceFilesPath;

//该文件是目录

sourceChildPath=sourceChildPath+”\\”+sourceFilesList[i].getName()+”\\”;

System.out.println(“源文件所在路径:”+sourceChildPath);

//修改目标文件夹,保持原来的层级关系

destiNationChildPath=destinationFilesPath;

destiNationChildPath=destinationFilesPath+”\\”+sourceFilesList[i].getName()+”\\”;

System.out.println(“转换后文件所在路径”+destiNationChildPath);

//

mkdir(destiNationChildPath);

//递归遍历所有目录,查找word文档,并将其转换

change(sourceChildPath, destiNationChildPath,destinationFilesType);

}

}

System.out.println(“所有文档转换完毕”);

}

/**

 * 用于创建文件夹的方法

 * @param mkdirName

 */

public static void mkdir(String mkdirName){

try{

//使用指定的路径创建文件对象

File dirFile = new File(mkdirName);

//

boolean bFile = dirFile.exists();

//已经存在文件夹,操作???提醒是否要替换

if( bFile == true ) {

System.out.println(“已经存在文件夹”+mkdirName);

}

//不存在该文件夹,则新建该目录

else{

System.out.println(“新建文件夹”+mkdirName);

bFile = dirFile.mkdir();

if( bFile == true ){

System.out.println(“文件夹创建成功”);

}else{

System.out.println(” 文件夹创建失败,清确认磁盘没有写保护并且空件足够”);

System.exit(1);

}

}

}catch(Exception err){

System.err.println(“ELS – Chart : 文件夹创建发生异常”);

err.printStackTrace();

}finally{

}

}

/**

* 判断某个文件夹是否存在

* @param path

*/

public static boolean isPathExist(String path){

boolean isPathExist=false;

try{

File pathFile = new File(path);

if(pathFile.exists())

isPathExist= true;

else

isPathExist= false;

}catch(Exception err){

err.printStackTrace();

}

return isPathExist;

}

/**

* 主函数

*/

public static void main(String[] args){

Scanner sc=new Scanner(System.in);

//源文档所在路径

String sourceFilesPath=””;

//String inputSourcePath=””;

//boolean sourcePathFlag=true;

//System.out.println(“请输入要转换文档所在的文件夹”);

//while(sourcePathFlag){

//inputSourcePath=sc.next();

//if(!isPathExist(inputSourcePath))

//System.out.println(“源路径不存在,请输入正确的路径”);

//else

//sourcePathFlag=false;

//}

//sourceFilesPath=inputSourcePath;

sourceFilesPath=”D:\\word”;

//目标文档要存放的目录

String destinationFilesPath=””;

//String inputdestinationPath=””;

//boolean destinationPathFlag=true;

//System.out.println(“请输入转换后文档要存放的文件夹”);

//while(destinationPathFlag){

//inputdestinationPath=sc.next();

////目标文件不存在时,是否要提示用户创建文件

//if(!isPathExist(inputdestinationPath))

//System.out.println(“目标路径不存在,请输入正确的路径”);

//else

//destinationPathFlag=false;

//}

//destinationFilesPath=inputdestinationPath;

destinationFilesPath=”D:\\txt”;

//选择要转换的类型

int destinationFilesType=0;

int inputNumber=0;

boolean numFlag=true;

System.out.println(“您要将word文档转换为哪种文档格式?”);

System.out.println(“0:doc \t 2:txt \t 8:html \t 9:htm \t 11:xml \t 12:docx \t 17:pdf \t 18:xps”);

while(numFlag){

inputNumber=sc.nextInt();

if(inputNumber!=2inputNumber!=8inputNumber!=9inputNumber!=11inputNumber!=12inputNumber!=17){

System.out.println(“您的输入有误,请输入要转换的文档类型前的数字”);

}else

numFlag=false;

}

destinationFilesType=inputNumber;

//实行转换

change(sourceFilesPath, destinationFilesPath,destinationFilesType);

//测试各种类型转换

//for(int i=0;i25;i++){

//destinationFilesType=i;

//System.out.println(“文件类型”+destinationFilesType);

//System.out.println(“存放目录:”+destinationFilesPath+”\\”+i);

//mkdir(destinationFilesPath+”\\”+i);

//change(sourceFilesPath, destinationFilesPath+”\\”+i,destinationFilesType);

//}

}

}

这个我刚用的。。格式都能带过来的。 你自己再下载个 jacob的包和dll文件

请教:java从数据库获取数据导出docx,有多个表格,请问怎么导出?

第一步:如何用POI操作Excel

@Test

public void createXls() throws Exception{

//声明一个工作薄

HSSFWorkbook wb = new HSSFWorkbook();

//声明表

HSSFSheet sheet = wb.createSheet(“第一个表”);

//声明行

HSSFRow row = sheet.createRow(7);

//声明列

HSSFCell cel = row.createCell(3);

//写入数据

cel.setCellValue(“你也好”);

FileOutputStream fileOut = new FileOutputStream(“d:/a/b.xls”);

wb.write(fileOut);

fileOut.close();

}

第二步:导出指定数据库的所有表

分析:

1:某个数数据库有多少表,表名是什么?―――DataBaseMetadate.getMetadate().getTables(null,null,null,new String[]{Table}); – excel的文件名称。

2:对每一个表进行select * 操作。 - 每一个sheet的名称。

3:分析表结构,rs.getMetadate(); ResultSetMedated

4:多个列,列名是什么.- 字段名就是sheet的第一行信息。

5:获取每一行的数据 – 放到sheet第一行以后。

@Test

public void export() throws Exception{

//声明需要导出的数据库

String dbName = “focus”;

//声明book

HSSFWorkbook book = new HSSFWorkbook();

//获取Connection,获取db的元数据

Connection con = DataSourceUtils.getConn();

//声明statemen

Statement st = con.createStatement();

//st.execute(“use “+dbName);

DatabaseMetaData dmd = con.getMetaData();

//获取数据库有多少表

ResultSet rs = dmd.getTables(dbName,dbName,null,new String[]{“TABLE”});

//获取所有表名 - 就是一个sheet

ListString tables = new ArrayListString();

while(rs.next()){

String tableName = rs.getString(“TABLE_NAME”);

tables.add(tableName);

}

for(String tableName:tables){

HSSFSheet sheet = book.createSheet(tableName);

//声明sql

String sql = “select * from “+dbName+”.”+tableName;

//查询数据

rs = st.executeQuery(sql);

//根据查询的结果,分析结果集的元数据

ResultSetMetaData rsmd = rs.getMetaData();

//获取这个查询有多少行

int cols = rsmd.getColumnCount();

//获取所有列名

//创建第一行

HSSFRow row = sheet.createRow(0);

for(int i=0;icols;i++){

String colName = rsmd.getColumnName(i+1);

//创建一个新的列

HSSFCell cell = row.createCell(i);

//写入列名

cell.setCellValue(colName);

}

//遍历数据

int index = 1;

while(rs.next()){

row = sheet.createRow(index++);

//声明列

for(int i=0;icols;i++){

String val = rs.getString(i+1);

//声明列

HSSFCell cel = row.createCell(i);

//放数据

cel.setCellValue(val);

}

}

}

con.close();

book.write(new FileOutputStream(“d:/a/”+dbName+”.xls”));

}

请教如何用java 提取word 文件的内嵌对象的内容,比如一个word 文档,里面嵌入sql文件,想提取sql文件内容

目前java操作word很弱,几乎不可能实现。

你可是试试jni,与微软的编程语言或操作系统做接口!

只能告诉你目前有个jacob(微软的com+桥)可以操作word

本文来自投稿,不代表【】观点,发布者:【

本文地址: ,如若转载,请注明出处!

举报投诉邮箱:253000106@qq.com

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2024年3月28日 03:11:41
下一篇 2024年3月28日 03:20:50

相关推荐

  • 深入java虚拟机pdf,深入java虚拟机 中村成洋 pdf

    在linux环境下,java怎么实现从word格式转换为pdf格式 //设置当前使用的打印机,我的Adobe Distiller打印机名字为 Adobe PDF wordCom.setProperty( ActivePrinter , new Variant( Adobe PDF ));//设置printout的参数,将word文档打印为postscript…

    2024年5月23日
    4400
  • java截取指定长度字符串,java截取指定字符串之后的

    java中如何截取字符串中的指定一部分 第一个参数是开始截取的字符位置。(从0开始)第二个参数是结束字符的位置+1。(从0开始)indexof函数的作用是查找该字符串中的某个字的位置,并且返回。 int end);截取s中从begin开始至end结束时的字符串,并将其赋值给s;split讲解:java.lang.string.split split 方法 将…

    2024年5月23日
    4200
  • java绑定一个端口,java使用端口

    java如何多个service共用一个端口 你如果有多个项目的话,你可以把多个项目放到一个tomcat里面,这样端口相同使用项目名称来进行区分项目。你如果非要使用同一个,你也可以配置不同的域名导向不同的项目。就是访问的域名不同转接到的项目不同。 如果需要同时启动多个程序,要么修改tomcat的配置文件中的监听端口。要么修改jar包程序的监听端口。不能在一台服…

    2024年5月23日
    3400
  • java多线程并发编程基础,Java多线程并发执行返回

    电脑培训分享Java并发编程:核心理论 电脑培训发现本系列会从线程间协调的方式(wait、notify、notifyAll)、Synchronized及Volatile的本质入手,详细解释JDK为我们提供的每种并发工具和底层实现机制。 人们开始意识到了继承的众多缺点,开始努力用聚合代替继承。软件工程解决扩展性的重要原则就是抽象描述,直接使用的工具就是接口。接…

    2024年5月23日
    4600
  • 自学java找工作,自学java找工作需要包装简历吗

    自学java学多久可以找到工作 1、自学Java至少需要一年以上的时间才能达到找工作的水平。报班培训四到六个月的时间就可以找到一份不错的工作。 2、自学Java至少需要一年以上的时间才能达到找工作的水平。 3、如果要想找到一份Java相关的工作,需要至少学习5-6个月时间才能就业。Java开发需要掌握一些基础的编程语言知识,比如掌握面向对象的编程思想、基本的…

    2024年5月23日
    4300
  • java左移右移,java 左移

    java位移问题 1、思路:直接用Integer类的bit运算操作。 2、移位操作:左移:向左移位,符号后面的数字是移了多少位,移的位用0补齐,例如2进制数01111111左移一位后变为11111110,移位是字节操作。 3、Java 位运算 Java 位运算[转]一,Java 位运算表示方法: 在Java语言中,二进制数使用补码表示,最高位为符号位,正数的…

    2024年5月23日
    4200
  • java技术规范,java规范性要求

    现在主流的JAVA技术是什么? java最流行开发技术程序员必看 1 、Git Git一直是世界上最受欢迎的Java工具之一,也是Java开发人员最杰出的工具之一。Git是一个开源工具,是-种出色的分布式版本控制解决方案。 (1).Java基础语法、数组、类与对象、继承与多态、异常、范型、集合、流与文件、反射、枚举、自动装箱和注解。(2).Java面向对象编…

    2024年5月23日
    4000
  • javasocket编程,Java socket编程中,禁用nagle算法的参数

    Java进行并发多连接socket编程 1、Java可利用ServerSocket类对外部客户端提供多个socket接口。基本的做法是先创建一个ServerSocket实例,并绑定一个指定的端口,然后在这个实例上调用accept()方法等待客户端的连接请求。 2、Socket socket=server.accept(0;Thread handleThrea…

    2024年5月23日
    4600
  • java死亡,java死代码是什么意思

    我的世界传送回死亡点指令是什么? 1、下面就让我们一起来了解一下吧:我的世界回到死的地方的指令是输入/back,就可以回到死亡地点了,当然也可以看信标,因为死亡后会有一道光集中在死亡点,只要循着光就可以找到目的地了。 2、在服务器中的指令 首先打开指令台,在指令行输入“/back”就可以回到自己的死亡地点了。在单人游戏中的指令 在单人游戏中,您无法直接返回到…

    2024年5月23日
    4800
  • myeclipse能部署java工程么,myeclipse支持jdk18

    myeclipse如何建java文件 1、点击【File】—【New】–【Class】在如下界面,输入Class的名字,如Test,点击【Finish】。Test.java文件创建成功。 2、点击【File】—【New】–【Class】 在如下界面,输入Class的名字,如Test,点击【Finish】。 Te…

    2024年5月23日
    3900

发表回复

登录后才能评论



关注微信