import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.Reader; import java.math.BigInteger; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.apache.poi.xwpf.usermodel.BreakType; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import org.apache.poi.xwpf.usermodel.XWPFTableRow; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc; public class POI_创建Oracle表文档_S4_Test { public static void main(String[] args) throws Exception { POI_创建Oracle表文档_S4_Test t = new POI_创建Oracle表文档_S4_Test(); t.createSimpleTableDocx("f:/saveFile/temp/sys_table_" + System.currentTimeMillis() + ".docx"); } public void createSimpleTableDocx(String savePath) throws Exception { XWPFDocument xdoc = new XWPFDocument(); OracleTableInfo_S3 s3 = new OracleTableInfo_S3(); List<List<String>> tableList = s3.getUserAllTable(); for (int i = 0, len = tableList.size(); i < len; i++) { List<String> tableInfo = tableList.get(i); System.out.println("-----------=" + tableInfo.get(0)); List<List<String>> tableColumnList = s3 .getTableColumnInfo(tableInfo.get(0)); createOracleTableInfo(xdoc, tableInfo, tableColumnList); addNewPage(xdoc, BreakType.COLUMN); } saveDocument(xdoc, savePath); } public void createOracleTableInfo(XWPFDocument xdoc, List<String> tableInfo, List<List<String>> tableColumnList) { XWPFTable xTable = xdoc.createTable(3 + tableColumnList.size(), 6); setTableWidth(xTable, "8000"); int[] colWidthArr = new int[] { 1500, 3000, 1200, 850, 600, 850 }; String[] colInfo = new String[] { "列名", "注释", "类型", "默认值", "空值", "主键" }; XWPFTableRow row = xTable.getRow(2); row.setHeight(380); createColumnTitle(row, colInfo, colWidthArr); row = xTable.getRow(0); row.setHeight(380); setRowCellText(row.getCell(0), "中文名称", colWidthArr[0], true, 3, "BFBFBF", STVerticalJc.CENTER, STJc.CENTER); setRowCellText(row.getCell(1), "", colWidthArr[1], false, 0, null, STVerticalJc.CENTER, STJc.CENTER); setRowCellText(row.getCell(2), "英文名称", colWidthArr[2], true, 3, "BFBFBF", STVerticalJc.CENTER, STJc.CENTER); setRowCellText(row.getCell(3), tableInfo.get(0), colWidthArr[3], false, 0, null, STVerticalJc.CENTER, STJc.CENTER); mergeCellsHorizontal(xTable, 0, 3, 5); row = xTable.getRow(1); row.setHeight(380); setRowCellText(row.getCell(0), "功能描述", colWidthArr[0], true, 3, "BFBFBF", STVerticalJc.CENTER, STJc.CENTER); setRowCellText(row.getCell(1), tableInfo.get(1), colWidthArr[1], false, 0, null, STVerticalJc.CENTER, STJc.LEFT); mergeCellsHorizontal(xTable, 1, 1, 5); for (int i = 0, len = tableColumnList.size(); i < len; i++) { row = xTable.getRow(3 + i); row.setHeight(380); List<String> columnList = tableColumnList.get(i); for (int j = 0, jlen = columnList.size(); j < jlen; j++) { setRowCellText(row.getCell(j), columnList.get(j), colWidthArr[j], false, 0, null, STVerticalJc.CENTER, STJc.CENTER); } } } public void createColumnTitle(XWPFTableRow row, String[] colInfo, int[] colWidthArr) { for (int i = 0, len = colInfo.length; i < len; i++) { setRowCellText(row.getCell(i), colInfo[i], colWidthArr[i], true, 3, "BFBFBF", STVerticalJc.CENTER, STJc.CENTER); } } // 设置单元格文字 public void setRowCellText(XWPFTableCell cell, String text, int width, boolean isShd, int shdValue, String shdColor, STVerticalJc.Enum jc, STJc.Enum stJc) { CTTc cttc = cell.getCTTc(); CTTcPr ctPr = cttc.isSetTcPr() ? cttc.getTcPr() : cttc.addNewTcPr(); CTShd ctshd = ctPr.isSetShd() ? ctPr.getShd() : ctPr.addNewShd(); CTTblWidth cTblWidth = ctPr.addNewTcW(); cTblWidth.setW(BigInteger.valueOf(width)); cTblWidth.setType(STTblWidth.Enum.forString("dxa")); if (isShd) { if (shdValue > 0 && shdValue <= 38) { ctshd.setVal(STShd.Enum.forInt(shdValue)); } if (shdColor != null) { ctshd.setColor(shdColor); } } ctPr.addNewVAlign().setVal(jc); cttc.getPList().get(0).addNewPPr().addNewJc().setVal(stJc); cell.setText(text); } // 跨列合并单元格 public void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) { for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) { XWPFTableCell cell = table.getRow(row).getCell(cellIndex); if (cellIndex == fromCell) { // The first merged cell is set with RESTART merge value cell.getCTTc().addNewTcPr().addNewHMerge() .setVal(STMerge.RESTART); } else { // Cells which join (merge) the first one, are set with CONTINUE cell.getCTTc().addNewTcPr().addNewHMerge() .setVal(STMerge.CONTINUE); } } } public void setTableWidth(XWPFTable table, String width) { CTTbl ttbl = table.getCTTbl(); CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl .getTblPr(); CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr .addNewTblW(); CTJc cTJc = tblPr.addNewJc(); cTJc.setVal(STJc.Enum.forString("center")); tblWidth.setW(new BigInteger(width)); tblWidth.setType(STTblWidth.DXA); } public void addNewPage(XWPFDocument document, BreakType breakType) { XWPFParagraph xp = document.createParagraph(); xp.createRun().addBreak(breakType); } public void saveDocument(XWPFDocument document, String savePath) throws Exception { FileOutputStream fos = new FileOutputStream(savePath); document.write(fos); fos.close(); } } class OracleTableInfo_S3 { public List<List<String>> getUserAllTable() throws Exception { Connection conn = getOracleConnection(); String sql = "select table_name,comments from user_tab_comments"; PreparedStatement ps = null; ResultSet rs = null; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); List<List<String>> tableList = new ArrayList<List<String>>(); while (rs.next()) { List<String> resultList = new ArrayList<String>(); resultList.add(rs.getString(1)); if (StringUtils.isNotBlank(rs.getString(2))) { resultList.add(rs.getString(2)); } else { resultList.add(""); } tableList.add(resultList); } closeConnection(rs, null, conn); return tableList; } public List<List<String>> getTableColumnInfo(String tableName) throws Exception { Connection conn = getOracleConnection(); String sql = "select a.column_name as column_name, b.comments,a.data_type || '(' || nvl(a.data_precision, a.data_length) || (case when a.data_scale > 0 then ',' || a.data_scale end) || ')' as data_type, a.data_default, a.nullable, (select decode(d.constraint_type, 'P','Y','') from all_constraints d, all_cons_columns c where d.constraint_type in ('P') and d.status = 'ENABLED' and d.constraint_name = c.constraint_name and d.owner = c.owner and c.column_name = a.column_name and c.table_name = a.table_name) as key_type from user_tab_columns a, user_col_comments b where a.column_name = b.column_name and a.table_name = b.table_name and b.table_name = upper('" + tableName + "')"; PreparedStatement ps = null; ResultSet rs = null; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); Reader reader; List<List<String>> columnList = new ArrayList<List<String>>(); while (rs.next()) { List<String> resultList = new ArrayList<String>(); resultList.add(rs.getString(1)); resultList.add(rs.getString(2)); resultList.add(rs.getString(3)); reader = rs.getCharacterStream(4); if (reader != null) { BufferedReader br = new BufferedReader(reader); StringBuffer sb = new StringBuffer(); String temp; while ((temp = br.readLine()) != null) { sb.append(temp); } if (sb.length() > 0) { resultList.add(sb.toString()); } else { resultList.add(""); } } else { resultList.add(""); } resultList.add(rs.getString(5)); resultList.add(rs.getString(6)); columnList.add(resultList); } for (List<String> list : columnList) { System.out.println(list); } closeConnection(rs, null, conn); return columnList; } public void closeConnection(ResultSet rs, Statement statement, Connection conn) { try { if (rs != null) { rs.close(); } if (statement != null) { statement.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } public Connection getOracleConnection() { String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@localhost:1521:xe";// 要操作的数据库名称 String username = "admin";// 数据库用户名 String password = "123456";// 密码 return getConnection(driver, url, username, password); } public Connection getConnection(String driver, String url, String userName, String passwd) { Connection conn = null; try { Class.forName(driver); conn = DriverManager.getConnection(url, userName, passwd); } catch (Exception e) { e.printStackTrace(); } return conn; } }
结果为:
相关推荐
简单poi创建word文档源码\简单poi创建word文档源码\简单poi创建word文档源码
poi.word poi.word poi操作word表格
POI 替换word2007文本框的值POI,包含word表中的文本框的值,定义好word模板即可替换word中的值。
poi创建excel表格,导出excel
POI报表Word导出
值得注意的是 POI 在读取 word 文件的时候不会读取 word 文件中的图片信息 还有就是对于 2007 版的 word docx 如果 word 文件中有表格 所有表格中的数据都会在读取出来的字符串的最后 ">这是一个POI读取word 2003 和...
java使用poi操作.doc word模板替换,循环插入表格
一个使用Apache的poi使用word模板循环输出表格行并导出的例子
POI解析word2007文本及图片(已测试).doc
poi将word里面的内容转换成html、包括word里面字体的大小、表格的处理、还有图片、段落等等...
使用java Apache poi 根据word模板生成word报表 仅支持docx格式的word文件,大概是word2010及以后版本,doc格式不支持。 使用说明:https://blog.csdn.net/u012775558/article/details/79678701
Apache poi 根据word模板生成word报表 替换 循环列表 图片,代码调试过了,修改相应的图片位置,word模板路径即可运行
java Apache poi word模板 word表格模板 word报表.zip
NULL 博文链接:https://53873039oycg.iteye.com/blog/2187388
poi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成word
下载后,运行 可以 Test1中的main方法,注意里面的word模版地址要修改下。 test1.java主要是针对word的书签操作 MSWordTool.java主要是针对word的列表书签的操作,相对来说比较复杂,具体看个人需求。
POI导出Word包含页眉 页脚的设置,标题的设置以及显示显示设置,包括POI操作Word完整的jar包
附件包含一个web项目,是一个简单的解析excel和word的例子,项目中包含jar poi解析excel、word2007,2010等版本
使用poi替换word中的图片,无需加书签,doc/docx均可,亲测。
java运用poi操作word,替换word中的占位符填充数据,并将多个word合并成一个word,附件是java代码和相关jar包,以及测试模板,需要注意是,只适用2007以上的word