Java 如何将表格数据导入word文档中
作者:George.Liu73
这篇文章主要介绍了Java将表格数据导入word文档中的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
Java 表格数据导入word文档中
个人觉得这个功能实在搞笑,没什么意义,没办法提了需求就要实现,(太好说话了把我)
我的实现是再word中生成一个与 excel行,列 一样的一个表格,然后把从excel拿到的数据(exList参数)依次放到word表格中
public static void createFile(HttpServletResponse response, String fileName, List<List<String>> exList) { try { setResponseHeader(response, fileName); //生成一个word模版文件 XWPFDocument document = new XWPFDocument(); XWPFTable table = document.createTable(exList.size(), exList.get(0).size()); XWPFTableRow row; for (int i = 0; i < exList.size(); i++) { List<String> sdf = exList.get(i); row = table.getRow(i); for (int j = 0; j < exList.get(i).size(); j++) { String s =sdf.get(j); row.getCell(j).setText(s); row.getCell(j).setWidthType(TableWidthType.AUTO); } //将数据插入表格中 pos:0 表示 第一个表格 document.setTable(0,table); } ServletOutputStream outputStream = response.getOutputStream(); BufferedOutputStream bufferStream = new BufferedOutputStream(outputStream, 1024); document.write(bufferStream); document.close(); bufferStream.close();; } catch (IOException e) { e.printStackTrace(); } } public static void setResponseHeader(HttpServletResponse response, String name) { try { name = new String(name.getBytes(), "ISO8859-1"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } response.setContentType("multipart/form-data"); //要保存的文件名 response.setHeader("Content-Disposition", "attachment;filename=" + name + ".docx"); response.addHeader("Pargam", "no-cache"); response.addHeader("Cache-Control", "no-cache"); }
Java poi导入word表格数据的经过
一、过程及遇到的问题和解决思路
需要导入的是一个word文档,内容是以表格的形式保存在word中
1、poi对word表格的空格处可以自动识别出来并赋值为 " ",这一点比poi导入excel人性化(excel默认是跳过这个空格)
2、对于某些情况下,肉眼无法看出表格格式问题,但是程序可以识别出来,怀疑是表格后期人工修改过,导致表格外观没问题但是行列属性不一致,导致读取时遇到这些地方报错,解决思路:可以在读取每一行之前先判断列数是否正确,poi中可以获取每行的列数,不正确的证明此列有问题,舍弃跳过。
二、代码
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.17</version> </dependency>
package com.example.importtomysql; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.usermodel.*; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; public class ImportWord { public List<TableColumn> testReadByDoc(String path) throws Exception { File f = new File(path); InputStream is = new FileInputStream(f); HWPFDocument doc = new HWPFDocument(is); //输出书签信息 // this.printInfo(doc.getBookmarks()); //输出文本 // System.out.println(doc.getDocumentText()); Range range = doc.getRange(); // this.printInfo(range); //读表格 List<TableColumn> tableColumns = this.readTable(range); //读列表 // this.readList(range); //把当前HWPFDocument写到输出流中 // doc.write(new FileOutputStream("D:\\temp\\test.doc")); is.close(); return tableColumns; } /** * 输出书签信息 * @param bookmarks */ private void printInfo(Bookmarks bookmarks) { int count = bookmarks.getBookmarksCount(); System.out.println("书签数量:" + count); Bookmark bookmark; for (int i=0; i<count; i++) { bookmark = bookmarks.getBookmark(i); System.out.println("书签" + (i+1) + "的名称是:" + bookmark.getName()); System.out.println("开始位置:" + bookmark.getStart()); System.out.println("结束位置:" + bookmark.getEnd()); } } /** * 读表格 * 每一个回车符代表一个段落,所以对于表格而言,每一个单元格至少包含一个段落,每行结束都是一个段落。 * @param range */ private List<TableColumn> readTable(Range range) { List<TableColumn> tableColumns = new ArrayList<>(); //遍历range范围内的table。 TableIterator tableIter = new TableIterator(range); Table table; TableRow row; TableCell cell; int i=0; int k=0; while (tableIter.hasNext()&&i<=1) { table = tableIter.next(); int rowNum = table.numRows(); for (int j=0; j<rowNum; j++) { TableColumn tableColumn = new TableColumn(); row = table.getRow(j); int cellNum = row.numCells(); // for (int k=0; k<cellNum; k++) { // cell = row.getCell(k, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); // // //输出单元格的文本 // System.out.println(cell.text().trim()); // } k++; if(12==cellNum){ tableColumn.setId(row.getCell(0).text().trim()); tableColumn.setSscj(row.getCell(1).text().trim()); tableColumn.setQlfl(row.getCell(2).text().trim()); tableColumn.setXmmc(row.getCell(3).text().trim()); tableColumn.setZx(row.getCell(4).text().trim()); tableColumn.setBlx(row.getCell(5).text().trim()); tableColumn.setSsyj(row.getCell(6).text().trim()); tableColumn.setCbjg(row.getCell(7).text().trim()); tableColumn.setZrsx(row.getCell(8).text().trim()); tableColumn.setSxyj(row.getCell(9).text().trim()); tableColumn.setZzqx(row.getCell(10).text().trim()); tableColumn.setZzyj(row.getCell(11).text().trim()); // tableColumn.setBz(row.getCell(12).text().trim()); tableColumns.add(tableColumn); if(679==k){ System.out.println(k +" " +row.getCell(0).text().trim()+" " +row.getCell(3).text().trim()); } // System.out.println(k +" " +row.getCell(0).text().trim()+" "+row.getCell(3).text().trim()); }else { System.out.println(k); } } i++; } return tableColumns; } /** * 读列表 * @param range */ private void readList(Range range) { int num = range.numParagraphs(); Paragraph para; for (int i=0; i<num; i++) { para = range.getParagraph(i); if (para.isInList()) { System.out.println("list: " + para.text()); } } } /** * 输出Range * @param range */ private void printInfo(Range range) { //获取段落数 int paraNum = range.numParagraphs(); System.out.println(paraNum); for (int i=0; i<paraNum; i++) { System.out.println("段落" + (i+1) + ":" + range.getParagraph(i).text()); } int secNum = range.numSections(); System.out.println(secNum); Section section; for (int i=0; i<secNum; i++) { section = range.getSection(i); System.out.println(section.getMarginLeft()); System.out.println(section.getMarginRight()); System.out.println(section.getMarginTop()); System.out.println(section.getMarginBottom()); System.out.println(section.getPageHeight()); System.out.println(section.text()); } } }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。