1.JAVA编辑WORD文件插入图片
试试这个代码,需要添加spire.doc jar依赖
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.*;
import com.spire.doc.fields.DocPicture;
public class InsertImage {
public static void main(String[] args) {
//实例化Document对象
Document doc = new Document();
//加载文档
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\test.docx");
//获取第一个section
Section section = doc.getSections().get(0);
//添加一个段落
Paragraph para = section.addParagraph();
//添加图片到段落
DocPicture picture = para.appendPicture("C:\\Users\\Administrator\\Desktop\\Cartoon.png");
//设置文字环绕方式(居于文字上方)
picture.setTextWrappingStyle(TextWrappingStyle.In_Front_Of_Text);
//指定图片的相对位置
picture.setHorizontalOrigin(HorizontalOrigin.Page);
picture.setHorizontalPosition(250f);
picture.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
picture.setVerticalPosition(150f);
//设置图片大小
picture.setWidth(80f);
picture.setHeight(80f);
//保存到文档
doc.saveToFile("output/InsertImage.docx", FileFormat.Docx);
}
}生成的Word:
2.如何用java将图片插入到word指定位置
以前做过其他office的没做过微软的,不过大同小异
这个你就需要在java中获取到word的某种对象,方法有很多,但是要看你用什么方式调用的office了,如果是某个牌子的中间件,那就用那个中间件的二次开发文档里面应该就有提到这些方法,如果是第三方的jar包,应该也不麻烦,通过jar包中的方法,可能要有一个word的实例创建的过程吧,然后获取到range或者textrange等这些对象(应该是这俩对象……)通过这对象然后找到插入图片的方法,然后传入位置参数(可能是两个数用逗号隔开)
3.java中怎么将word文档怎么生成图片
public class CreateWordDemo{ public void createDocContext(String file)throws DocumentException,IOException { //设置纸张大小 Document document = newDocument(PageSize.A4); //建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中 RtfWriter2.getInstance(document, newFileOutputStream(file)); document.open(); //设置中文字体 BaseFont bfChinese =BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); //标题字体风格 Font titleFont = new Font(bfChinese, 12,Font.BOLD); //正文字体风格 Font contextFont = new Font(bfChinese, 10,Font.NORMAL); Paragraph title = newParagraph("标题"); //设置标题格式对齐方式 title.setAlignment(Element.ALIGN_CENTER); title.setFont(titleFont); document.add(title); String contextString ="iText是一个能够快速产生PDF文件的java类库。
" + " \n"//换行 +"iText的java类对于那些要产生包含文本," + "表格,图形的只读文档是很有用的。它的类库尤其与javaServlet有很好的给合。
" +"使用iText与PDF能够使你正确的控制Servlet的输出。"; Paragraph context = newParagraph(contextString); //正文格式左对齐 context.setAlignment(Element.ALIGN_LEFT); context.setFont(contextFont); //离上一段落(标题)空的行数 context.setSpacingBefore(5); //设置第一行空的列数 context.setFirstLineIndent(20); document.add(context); //利用类FontFactory结合Font和Color可以设置各种各样字体样式 Paragraph underline = new Paragraph("下划线的实现",FontFactory.getFont( FontFactory.HELVETICA_BOLDOBLIQUE, 18,Font.UNDERLINE, new Color(0, 0,255))); document.add(underline); // 设置 Table表格 Table aTable = newTable(3); int width[] = { 25, 25, 50}; aTable.setWidths(width);//设置每列所占比例 aTable.setWidth(90); // 占页面宽度90% aTable.setAlignment(Element.ALIGN_CENTER);//居中显示 aTable.setAlignment(Element.ALIGN_MIDDLE);//纵向居中显示 aTable.setAutoFillEmptyCells(true); //自动填满 aTable.setBorderWidth(1); //边框宽度 aTable.setBorderColor(new Color(0, 125, 255)); //边框颜色 aTable.setPadding(2);//衬距,看效果就知道什么意思了 aTable.setSpacing(3);//即单元格之间的间距 aTable.setBorder(2);//边框 //设置表头 Cell haderCell = newCell("表格表头"); haderCell.setHeader(true); haderCell.setColspan(3); aTable.addCell(haderCell); aTable.endHeaders(); Font fontChinese = new Font(bfChinese, 12, Font.NORMAL,Color.GREEN); Cell cell = new Cell(new Phrase("这是一个测试的 3*3 Table 数据",fontChinese)); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setBorderColor(new Color(255, 0,0)); cell.setRowspan(2); aTable.addCell(cell); aTable.addCell(newCell("#1")); aTable.addCell(newCell("#2")); aTable.addCell(newCell("#3")); aTable.addCell(newCell("#4")); Cell cell3 = new Cell(new Phrase("一行三列数据",fontChinese)); cell3.setColspan(3); cell3.setVerticalAlignment(Element.ALIGN_CENTER); aTable.addCell(cell3); document.add(aTable); document.add(newParagraph("\n")); //添加图片 Image.getInstance即可以放路径又可以放二进制字节流 Image img =Image.getInstance("d:\\img01800.jpg"); img.setAbsolutePosition(0,0); img.setAlignment(Image.RIGHT);//设置图片显示位置 img.scaleAbsolute(60, 60);//直接设定显示尺寸 //img.scalePercent(50);//表示显示的大小为原尺寸的50% // img.scalePercent(25,12);//图像高宽的显示比例 //img.setRotation(30);//图像旋转一定角度 document.add(img); document.close(); } public static void main(String[] args){ CreateWordDemo word = newCreateWordDemo(); String file ="d:/demo1.doc"; try{ word.createDocContext(file); } catch (DocumentException e){ e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); } } }。
4.JAVA编辑WORD文件插入图片
试试这个代码,需要添加spire.doc jar依赖 import com.spire.doc.Document;import com.spire.doc.FileFormat;import com.spire.doc.Section;import com.spire.doc.documents.*;import com.spire.doc.fields.DocPicture;public class InsertImage { public static void main(String[] args) { //实例化Document对象 Document doc = new Document(); //加载文档 doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\test.docx"); //获取第一个section Section section = doc.getSections().get(0); //添加一个段落 Paragraph para = section.addParagraph(); //添加图片到段落 DocPicture picture = para.appendPicture("C:\\Users\\Administrator\\Desktop\\Cartoon.png"); //设置文字环绕方式(居于文字上方) picture.setTextWrappingStyle(TextWrappingStyle.In_Front_Of_Text); //指定图片的相对位置 picture.setHorizontalOrigin(HorizontalOrigin.Page); picture.setHorizontalPosition(250f); picture.setVerticalOrigin(VerticalOrigin.Top_Margin_Area); picture.setVerticalPosition(150f); //设置图片大小 picture.setWidth(80f); picture.setHeight(80f); //保存到文档 doc.saveToFile("output/InsertImage.docx", FileFormat.Docx); }}生成的Word: 。
5.java 如何使用poi 在word 中插入图片和数据,给我一个实现的例子,
实际上还真就得一个一个set进去
我现在做的程序就有这部分机能,思路就是excel→sheet→row
然后遍历所有row,取出所有的cell放到一个存储用结构体中。
无论如何,解析的过程都需要自己来写的。
=======================================================
你的想法我明白,其实就是想要一个java与poi的databinding,定义好的数据模块可以自动和excel的对应列进行匹配。但是这个东西确实就没有啊,人家只封装了读取excel内容,没有提供这样的模块,只有自己写啊。即使是60个列,事实上绑定的逻辑也就只用写一次,而且大部分的内容只要复制粘贴就好了。
如果像下面说的做循环,赋值简单了,可是用的时候就痛苦了,要取某一个字段的时候难道要用数组下标去取?
PS 鄙视5楼的,人家要的是数据导入,拷贝一段倒出的处理来。
转载请注明出处51数据库 » java导出word嵌入