用POI怎么向word书签位置插入或生成表格,指定位置。
//创建一个表格XWPFTable table = doc.createTable(4,2);table.setCellMargins(50, 0, 50,3000);//top, left, bottom, right// table.setInsideHBorder(XWPFBorderType.NONE, 0, 0, "");//去除单元格间的横线table.getRow(0).getCell(0).setText("字段一:");table.getRow(0).getCell(1).setText("字段二:");table.getRow(1).getCell(0).setText("字段三:");table.getRow(1).getCell(1).setText("字段四:");...
跪求POI根据模板生成word文档的例子。
注意啦,是用书签标记的方...
1. 读取word 2003及word 2007需要的jar包2. 读取 2003 版本(.doc)的word文件相对来说比较简单,只需要 poi-3.5-beta6-.jar 和 poi-scratchpad-3.5-beta6-.jar 两个 jar 包即可, 而 2007 版本(.docx)就麻烦多,我说的这个麻烦不是我们写代码的时候麻烦,是要导入的 jar 包比较的多,有如下 7 个之多:3. 1. openxml4j-bin-beta.jar4. 2. poi-3.5-beta6-.jar5. 3. poi-ooxml-3.5-beta6-.jar6. 4 .dom4j-1.6.1.jar7. 5. geronimo-stax-api_1.0_spec-1.0.jar8. 6. ooxml-schemas-1.0.jar9. 7. xmlbeans-2.3.0.jar10. 其中 4-7 是 poi-ooxml-3.5-beta6-.jar 所依赖的 jar 包(在 poi-bin-3.5-beta6-.tar.gz 中的 ooxml-lib 目录下可以找到)。
11. 2.换行符号12. 硬换行:文件中换行,如果是键盘中使用了"enter"的换行。
13. 软换行:文件中一行的字符数容量有限,当字符数量超过一定值时,会自动切到下行显示。
14. 对程序来说,硬换行才是可以识别的、确定的换行,软换行与字体大小、缩进有关。
15. 3.读取的注意事项16. 值得注意的是: POI 在读取不会读取 word 文件中的图片信息; 还有就是对于 2007 版的 word(.docx), 如果 word 文件中有表格,所有表格中的数据都会在读取出来的字符串的最后。
17. 4.读取word文本内容代码1 import java.io.File;2 import java.io.FileInputStream;3 import java.io.InputStream;4 5 import org.apache.poi.POIXMLDocument;6 import org.apache.poi.POIXMLTextExtractor;7 import org.apache.poi.hwpf.extractor.WordExtractor;8 import org.apache.poi.openxml4j.opc.OPCPackage;9 import org.apache.poi.xwpf.extractor.XWPFWordExtractor;10 11 public class Test {12 public static void main(String[] args) {13 try {14 InputStream is = new FileInputStream(new File("2003.doc"));15 WordExtractor ex = new WordExtractor(is);16 String text2003 = ex.getText();17 System.out.println(text2003);18 19 OPCPackage opcPackage = POIXMLDocument.openPackage("2007.docx");20 POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);21 String text2007 = extractor.getText();22 System.out.println(text2007);23 24 } catch (Exception e) {25 e.printStackTrace();26 }27 }28 }
poi word 图片 设置位置
写 public static void main(String args[]) throws Exception { XWPFDocument doc = new XWPFDocument(); XWPFParagraph p1 = doc.createParagraph(); p1.setAlignment(ParagraphAlignment.CENTER); p1.setBorderBottom(Borders.DOUBLE); p1.setBorderTop(Borders.DOUBLE); p1.setBorderRight(Borders.DOUBLE); p1.setBorderLeft(Borders.DOUBLE); p1.setBorderBetween(Borders.SINGLE); p1.setVerticalAlignment(TextAlignment.TOP); XWPFRun r1 = p1.createRun(); r1.setBold(true); r1.setText("The quick brown fox"); r1.setBold(true); r1.setFontFamily("Courier"); r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH); r1.setTextPosition(100); XWPFParagraph p2 = doc.createParagraph(); p2.setAlignment(ParagraphAlignment.RIGHT); p2.setBorderBottom(Borders.DOUBLE); p2.setBorderTop(Borders.DOUBLE); p2.setBorderRight(Borders.DOUBLE); p2.setBorderLeft(Borders.DOUBLE); p2.setBorderBetween(Borders.SINGLE); XWPFRun r2 = p2.createRun(); r2.setText("jumped over the lazy dog"); r2.setStrike(true); r2.setFontSize(20); XWPFRun r3 = p2.createRun(); r3.setText("and went away"); r3.setStrike(true); r3.setFontSize(20); r3.setSubscript(VerticalAlign.SUPERSCRIPT); XWPFParagraph p3 = doc.createParagraph(); p3.setWordWrap(true); p3.setPageBreak(true); p3.setAlignment(ParagraphAlignment.BOTH); p3.setSpacingLineRule(LineSpacingRule.EXACT); p3.setIndentationFirstLine(600); XWPFRun r4 = p3.createRun(); r4.setTextPosition(20); r4.setText("To be, or not to be: that is the question: Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune, Or to take arms against a sea of troubles, And by opposing end them? To die: to sleep; "); r4.addBreak(BreakType.PAGE); r4.setText("No more; and by a sleep to say we end The heart-ache and the thousand natural shocks That flesh is heir to, 'tis a consummation Devoutly to be wish'd. To die, to sleep; To sleep: perchance to dream: ay, there's the rub; ......."); r4.setItalic(true); XWPFRun r5 = p3.createRun(); r5.setTextPosition(-10); r5.setText("For in that sleep of death what dreams may come"); r5.addCarriageReturn(); r5.setText("When we have shuffled off this mortal coil,Must give us pause: there's the respectThat makes calamity of so long life;"); r5.addBreak(); r5.setText("For who would bear the whips and scorns of time,The oppressor's wrong, the proud man's contumely,"); r5.addBreak(BreakClear.ALL); r5.setText("The pangs of despised love, the law's delay,The insolence of office and the spurns......."); FileOutputStream out = new FileOutputStream("simple.docx"); doc.write(out); out.close(); }
Apache的POI,可以利用POI(java)生成word中柱状图、饼图之类的图表...
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
这里的方法支持导出excel至项目所在服务器,或导出至客户端浏览器供用户下载,下面我把两个实例都放出来。
1.下载所需POI的jar包,并导入项目。
2.添加一个User类,用于存放用户实体,类中内容如下:1 package com.mvc.po;23 public class User {4 private int id;5 private String name;6 private String password;7 private int age;89 public User() {10 11 }12 13 public User(int id, String name, String password, int age) {14 this.id = id;15 this.name = name;16 this.password = password;17 this.age = age;18 }19 public int getId() {20 return id;21 }22 public void setId(int id) {23 this.id = id;24 }25 public String getName() {26 return name;27 }28 public void setName(String name) {29 this.name = name;30 }31 public String getPassword() {32 return password;33 }34 public void setPassword(String password) {35 this.password = password;36 }37 public int getAge() {38 return age;39 }40 public void setAge(int age) {41 this.age = age;42 }43 }3.添加一个UserController类,类中内容如下:1 package com.mvc.controller;23 import java.text.SimpleDateFormat;4 import java.util.Date;56 import javax.servlet.ServletOutputStream;7 import javax.servlet.http.HttpServletResponse;89 import org.springframework.stereotype.Controller;10 import org.springframework.beans.factory.annotation.Autowired;11 import org.springframework.web.bind.annotation.RequestMapping;12 import org.springframework.web.bind.annotation.ResponseBody;13 14 import com.mvc.po.User;15 import com.mvc.service.UserService;16 17 @Controller18 public class UserController {19 20 @Autowired21 private UserService userService;22 23 @RequestMapping("/export.do")24 public @ResponseBody String export(HttpServletResponse response){ 25 response.setContentType("application/binary;charset=utf-8");26 try{27 ServletOutputStream out=response.getOutputStream();28 String fileName=new String(("UserInfo "+ new SimpleDateFormat("yyyy-MM-dd").format(new Date())).getBytes(),"UTF-8");29 response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xls");30 String[] titles = { "用户编号", "用户姓名", "用户密码", "用户年龄" }; 31 userService.export(titles, out);32 return "success";33 } catch(Exception e){34 e.printStackTrace();35 return "导出信息失败";36 }37 }38 }4.添加一个接口类UserService和实现类UserServiceImpl,类中内容如下:1 package com.mvc.service;2 3 import javax.servlet.ServletOutputStream;4 import com.mvc.po.User;5 6 public interface UserService {7 public void export(String[] titles, ServletOutputStream out);8 }1 package com.mvc.service.impl;23 import java.text.SimpleDateFormat;4 import java.util.List;56 import javax.servlet.ServletOutputStream;78 import com.mvc.dao.UserDAO;9 import com.mvc.po.User;10 import com.mvc.service.UserService;11 12 import org.apache.poi.hssf.usermodel.HSSFCell;13 import org.apache.poi.hssf.usermodel.HSSFCellStyle;14 import org.apache.poi.hssf.usermodel.HSSFRow;15 import org.apache.poi.hssf.usermodel.HSSFSheet;16 import org.apache.poi.hssf.usermodel.HSSFWorkbook;17 import org.springframework.beans.factory.annotation.Autowired;18 import org.springframework.stereotype.Service;19 20 @Service21 public class UserServiceImpl implements UserService {22 23 @Autowired24 private UserDAO userDAO;25 26 @Override27 public void export(String[] titles, ServletOutputStream out) { 28 try{29 // 第一步,创建一个workbook,对应一个Excel文件30 HSSFWorkbook workbook = new HSSFWorkbook();31 // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet32 HSSFSheet hssfSheet = workbook.createSheet("sheet1");33 // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short34 HSSFRow hssfRow = hssfSheet.createRow(0);35 // 第四步,创建单元格,并设置值表头 设置表头居中36 HSSFCellStyle hssfCellStyle = workbook.createCellStyle();37 //居中样式38 hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);39 40 HSSFCell hssfCell = null;41 for (int i = 0; i 42 hssfCell = hssfRow.createCell(i);//列索引从0开始43 hssfCell.setCellValue(titles[i]);//列名144 hssfCell.setCellStyle(hssfCellStyle);//列居中显示 45 }46 47 // 第五步,写入实体数据 48 List users = userDAO.query(); 49 50 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");51 if(users != null && !users.isEmpty()){52 for (int i = 0; i 53 hssfRow = hssfSheet.createRow(i+1); 54 User user = users.get(i);55 56 // 第六步...
java poi XWPFTable操作word表格的问题?
1.下载 下载3.8beta4版本,请记得一定要下载该版本,其他版本读取word模板并改写内容生成新的文件后,打开新文件时会提示“word无法读取文档,文档可能损坏。
” 2.集成到项目 这一步很简单,只要把下载后解压得到的poi-3.8-beta4-20110826.jar和poi-scratchpad-3.8-beta4-20110826.jar两个文件复制到java web项目的lib目录下就行了 3.制作word模板 把需要变动的值全部用代码来代替,例如你需要改变名称的值,则可以在模板中用name来表示。
详细见附件中的doc文件。
4.调用接口方法实现对word的读写操作 整个过程就是先读取模板,然后修改内容,再重新生成新的文档保存到本地或者输出文件流提供下载,下面分别是生成新文档和输出文件流两种方式的代码片断,详细的代码请见下列代码中的readwriteWord()两个重载方法。
...
poi-Java导出word时,能实现在word正文内“插入附件”的效果吗
关键代码如下:FileInputStream fileInputStream = new FileInputStream( soureFile);POIFSFileSystem pfs = new POIFSFileSystem( fileInputStream );HWPFDocument hwpf = new HWPFDocument(pfs);// make a HWPFDocument objectOutputStream output = new FileOutputStream( targetFile );hwpf.write(output);// write to the target fileoutput.close();(2)再word中插入表格。
HWPF的情况:Table tcDataTable = range.insertTableBefore( (short)column , row);//column and row列数和行数tcDataTable.getRow(i).getCell(j).getParagraph(0).getCharacterRun(0).insertBefore("插入i行j列的内容" );XWPF的情况:String outputFile = "D:\\test.doc";XWPFDocument document = new XWPFDocument();XWPFTable tableOne = document.createTable();XWPFTableRow tableOneRowOne = tableOne.getRow(0);tableOneRowOne.getCell(0).setText("11");XWPFTableCell cell12 = tableOneRowOne.createCell();cell12.setText("12");// tableOneRowOne.addNewTableCell().setText("第1行第2列");// tableOneRowOne.addNewTableCell().setText("第1行第3列");// tableOneRowOne.addNewTableCell().setText("第1行第4列");XWPFTableRow tableOneRowTwo = tableOne.createRow();tableOneRowTwo.getCell(0).setText("21");tableOneRowTwo.getCell(1).setText("22");// tableOneRowTwo.getCell(2).setText("第2行第3列");XWPFTableRow tableOneRow3 = tableOne.createRow();tableOneRow3.addNewTableCell().setText("31");tableOneRow3.addNewTableCell().setText("32");FileOutputStream fOut;try {fOut = new FileOutputStream(outputFile);document.write(fOut); fOut.flush();// 操作结束,关闭文件fOut.close();} catch (Exception e) {e.printStackTrace();}
转载请注明出处51数据库 » poi word bookmark