package com.test.utils;
import java.io.FileOutputStream; import java.io.IOException; import java.math.BigInteger;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment; import org.apache.poi.xwpf.usermodel.TextAlignment; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import org.apache.poi.xwpf.usermodel.XWPFTableRow; import org.apache.poi.xwpf.usermodel.XWPFTableCell.XWPFVertAlign; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHMerge; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; 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.CTVMerge; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
import com.alibaba.fastjson.JSONObject; /******************************************************************************* * @程序名称 * @处理 * @功能 * @版本 V1.0 * * @修改履历 wps打开可能存在不兼容 * * ********************************************************************************/ public class exportWord { public static void main(String[] args) { try { getWord(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void getWord() throws Exception { XWPFDocument xdoc = new XWPFDocument(); //标题 XWPFParagraph titleMes = xdoc.createParagraph(); titleMes.setAlignment(ParagraphAlignment.CENTER); XWPFRun r1 = titleMes.createRun(); r1.setFontFamily("微软雅黑"); r1.setText("材质报告");//活动名称 r1.setFontSize(16); r1.setColor("333333"); r1.setBold(false); //表格 XWPFTable xTable = xdoc.createTable(12, 5); mergeCellsVertically(xTable,4,2,9); createSimpleTable(xTable, xdoc); //setEmptyRow(xdoc, r1); // 在服务器端生成 FileOutputStream fos = new FileOutputStream( "d:\\种类报告.docx"); xdoc.write(fos); fos.close(); } //设置表格高度 private static XWPFTableCell getCellHight(XWPFTable xTable,int rowNomber,int cellNumber,Boolean b){ XWPFTableRow row = null; row = xTable.getRow(rowNomber); if(b){ row.setHeight(100); }else{ row.setHeight(200); } XWPFTableCell cell = null; cell = row.getCell(cellNumber); return cell; } /** * * @param xDocument * @param cell * @param text * @param bgcolor * @param width */ private static void setCellText(XWPFDocument xDocument, XWPFTableCell cell,String text,Boolean b,int fontSize) { CTP ctp = CTP.Factory.newInstance(); XWPFParagraph p = new XWPFParagraph(ctp, cell); if(!b){ p.setAlignment(ParagraphAlignment.CENTER); }else{ p.setAlignment(ParagraphAlignment.LEFT); } XWPFRun run = p.createRun(); run.setColor("000000"); run.setFontSize(fontSize); run.setText(text); run.setBold(false); CTRPr rpr = run.getCTR().isSetRPr() ? run.getCTR().getRPr() : run.getCTR().addNewRPr(); CTFonts fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr.addNewRFonts(); fonts.setAscii("微软雅黑"); fonts.setEastAsia("微软雅黑"); fonts.setHAnsi("微软雅黑"); cell.setParagraph(p); } public static void fillTable(XWPFTable table) { for (int rowIndex = 0; rowIndex < table.getNumberOfRows(); rowIndex++) { XWPFTableRow row = table.getRow(rowIndex); /* row.setHeight(380); */ for (int colIndex = 0; colIndex < row.getTableCells().size(); colIndex++) { XWPFTableCell cell = row.getCell(colIndex); if(rowIndex == 0 || rowIndex == 10){ setCellText(cell, "", "777777", 1000); //设置颜色 } } } } public static void setCellText(XWPFTableCell cell,String text, String bgcolor, int width) { CTTc cttc = cell.getCTTc(); CTTcPr cellPr = cttc.addNewTcPr(); cellPr.addNewTcW().setW(BigInteger.valueOf(width)); //cell.setColor(bgcolor); CTTcPr ctPr = cttc.addNewTcPr(); CTShd ctshd = ctPr.addNewShd(); ctshd.setFill(bgcolor); ctPr.addNewVAlign().setVal(STVerticalJc.CENTER); cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER); cell.setText(text); } //设置表格间的空行 public static void setEmptyRow(XWPFDocument xdoc,XWPFRun r1){ XWPFParagraph p1 = xdoc.createParagraph(); p1.setAlignment(ParagraphAlignment.CENTER); p1.setVerticalAlignment(TextAlignment.CENTER); r1 = p1.createRun(); } /** * 创建计划明细表 * @param task * @param xTable * @param xdoc * @throws IOException */ public static void createSimpleTable(XWPFTable xTable, XWPFDocument xdoc) throws IOException { String bgColor="FFFFFF"; CTTbl ttbl = xTable.getCTTbl(); CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr(); CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW(); tblWidth.setW(new BigInteger("8000")); tblWidth.setType(STTblWidth.DXA); setCellText(xdoc, getCellHight(xTable, 1, 4,true), "-- 图片 --",false,8); String[] arrayString = new String[]{"材料编号","材料代号","材料名称","规格","表面处理","厂家品牌","厂家联系方式","使用位置","数量"} ; for (int i = 1; i <= 9; i++) { setCellText(xdoc, getCellHight(xTable, i, 0,true), arrayString[i-1],true,8); mergeCellsHorizontal(xTable, i, 1, 3); setCellText(xdoc, getCellHight(xTable, i, 1,true), "参数值内容",false,8); } fillTable(xTable); mergeCellsHorizontal(xTable, 0, 0, 4); mergeCellsHorizontal(xTable, 10, 0, 4); mergeCellsHorizontal(xTable, 11, 0, 4); setCellText(xdoc, getCellHight(xTable, 0, 0,true), "地砖",true,13); setCellText(xdoc, getCellHight(xTable, 10, 0,true), "说明",true,8); setCellText(xdoc, getCellHight(xTable, 11, 0,false), "无",true,8); } // word跨列合并单元格 public static 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); } } } // word跨行并单元格 public static void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) { for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) { XWPFTableCell cell = table.getRow(rowIndex).getCell(col); if ( rowIndex == fromRow ) { // The first merged cell is set with RESTART merge value cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART); } else { // Cells which join (merge) the first one, are set with CONTINUE cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE); } } } }
=============================================================================================word 2
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="java.io.File" %> <%@ page import="java.io.FileInputStream" %> <%@ page import="java.io.FileOutputStream" %> <%@ page import="java.io.IOException" %> <%@ page import="java.math.BigInteger" %> <%@ page import="org.apache.poi.xwpf.usermodel.ParagraphAlignment" %> <%@ page import="org.apache.poi.xwpf.usermodel.XWPFParagraph" %> <%@ page import="org.apache.poi.xwpf.usermodel.XWPFRun" %> <%@ page import="org.apache.poi.xwpf.usermodel.XWPFTable" %> <%@ page import="org.apache.poi.xwpf.usermodel.XWPFTableCell" %> <%@ page import="org.apache.poi.xwpf.usermodel.XWPFTableRow" %> <%@ page import="org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts" %> <%@ page import="org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHMerge" %> <%@ page import="org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP" %> <%@ page import="org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr" %> <%@ page import="org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd" %> <%@ page import="org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl" %> <%@ page import="org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr" %> <%@ page import="org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth" %> <%@ page import="org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc" %> <%@ page import="org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr" %> <%@ page import="org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge" %> <%@ page import="org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge" %> <%@ page import="org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth" %> <%@ page import="com.test.utils.*" %> <%@ page import=" java.io.FileInputStream"%> <%@ page import=" java.io.InputStream"%>
<% getWord(); %>
<%! public static void getWord() throws Exception { CustomXWPFDocument xdoc = new CustomXWPFDocument(); //表格 XWPFTable xTable = xdoc.createTable(12, 3); createSimpleTable(xTable, xdoc);
// 在服务器端生成 FileOutputStream fos = new FileOutputStream("d:\\种类报告4.docx"); xdoc.write(fos); fos.close(); }
/** * 设置表格高度 * xTable:表格对象 * rowNomber:行下标值 * cellNumber:列下标值 * height:高度 */ private static XWPFTableCell getCellHight(XWPFTable xTable, int rowNomber, int cellNumber, int height){ XWPFTableRow row = null; row = xTable.getRow(rowNomber); row.setHeight(height); XWPFTableCell cell = null; cell = row.getCell(cellNumber); //设置单元格垂直居中 cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); return cell; }
/** * 设置单元格 * cell:单元格 * text:内容 * bgColor:背景色 * align:水平位置 * fontSize:字体大小 * isBold:是否加粗 */ private static void setCellText(XWPFTableCell cell, String text, String bgColor, ParagraphAlignment align, int fontSize, Boolean isBold) { //设置单元格背景色 if(bgColor != "" || !"".equals(bgColor)){ CTTc cttc = cell.getCTTc(); CTTcPr ctPr = cttc.addNewTcPr(); CTShd ctshd = ctPr.addNewShd(); ctshd.setFill(bgColor); } CTP ctp = CTP.Factory.newInstance(); XWPFParagraph p = new XWPFParagraph(ctp, cell); //设置单元格水平位置 p.setAlignment(align); XWPFRun run = p.createRun(); run.setColor("000000"); run.setFontSize(fontSize); run.setText(text); run.setBold(isBold); CTRPr rpr = run.getCTR().isSetRPr() ? run.getCTR().getRPr() : run.getCTR().addNewRPr(); CTFonts fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr.addNewRFonts(); fonts.setAscii("宋体"); fonts.setEastAsia("宋体"); fonts.setHAnsi("宋体"); cell.setParagraph(p); }
/** * 设置单元格图片信息 * doc:文件对象 * cell:单元格对象 * picUrl:图片访问地址 * width:图片宽度 * height:图片高度 */ private static void setCellImage(CustomXWPFDocument doc, XWPFTableCell cell, String picUrl, int width, int height) { try { //根据图片路径,生成数据流 File pic = new File(picUrl); FileInputStream is = new FileInputStream(pic); //给段落重新赋值 cell.removeParagraph(0); //调整图片为水平居中位置 XWPFParagraph pargraph = cell.addParagraph(); pargraph.setAlignment(ParagraphAlignment.CENTER); //将图片数据流写进word文档 doc.addPictureData(is, getPictureType(picUrl)); doc.createPicture(doc.getAllPictures().size()-1, width, height, pargraph); if(is != null){ is.close(); } } catch (Exception e) { e.printStackTrace(); } }
/** * 创建计划明细表 */ public static void createSimpleTable(XWPFTable xTable, CustomXWPFDocument xdoc) throws IOException { //设置表格边距 xTable.setCellMargins(100, 100, 100, 100);
CTTbl ttbl = xTable.getCTTbl(); CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr(); CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW(); tblWidth.setW(new BigInteger("8000")); tblWidth.setType(STTblWidth.DXA);
mergeCellHorizontally(xTable, 0, 0, 2); setCellText(getCellHight(xTable, 0, 0, 300), "地砖", "A9A9A9", ParagraphAlignment.LEFT, 12, true); setCellText(getCellHight(xTable, 1, 2, 500), "图片", "", ParagraphAlignment.CENTER, 12, true);
String[] arrayString = {"材料编号","材料代号","材料名称","规格","表面处理","厂家品牌","厂家联系方式","使用位置","数量"}; for (int i = 1; i < arrayString.length + 1; i++) { setCellText(getCellHight(xTable, i, 0, 500), arrayString[i-1], "", ParagraphAlignment.LEFT, 12, true); setCellText(getCellHight(xTable, i, 1, 500), "参数值内容", "", ParagraphAlignment.CENTER, 12, false); }
setCellImage(xdoc, getCellHight(xTable, 2, 2, 500), "D:\\879x707.png", 200, 200); mergeCellVertically(xTable, 2, 2, arrayString.length);
mergeCellHorizontally(xTable, 10, 0, 2); mergeCellHorizontally(xTable, 11, 0, 2); setCellText(getCellHight(xTable, 10, 0, 300), "说明", "A9A9A9", ParagraphAlignment.LEFT, 12, true); setCellText(getCellHight(xTable, 11, 0, 500), "无", "", ParagraphAlignment.LEFT, 12, false); }
//列合并 static void mergeCellVertically(XWPFTable table, int col, int fromRow, int toRow) { for(int rowIndex = fromRow; rowIndex <= toRow; rowIndex++){ CTVMerge vmerge = CTVMerge.Factory.newInstance(); if(rowIndex == fromRow){ // The first merged cell is set with RESTART merge value vmerge.setVal(STMerge.RESTART); } else { // Cells which join (merge) the first one, are set with CONTINUE vmerge.setVal(STMerge.CONTINUE); } XWPFTableCell cell = table.getRow(rowIndex).getCell(col); // Try getting the TcPr. Not simply setting an new one every time. CTTcPr tcPr = cell.getCTTc().getTcPr(); if (tcPr != null) { tcPr.setVMerge(vmerge); } else { // only set an new TcPr if there is not one already tcPr = CTTcPr.Factory.newInstance(); tcPr.setVMerge(vmerge); cell.getCTTc().setTcPr(tcPr); } } } //行合并 static void mergeCellHorizontally(XWPFTable table, int row, int fromCol, int toCol) { for(int colIndex = fromCol; colIndex <= toCol; colIndex++){ CTHMerge hmerge = CTHMerge.Factory.newInstance(); if(colIndex == fromCol){ // The first merged cell is set with RESTART merge value hmerge.setVal(STMerge.RESTART); } else { // Cells which join (merge) the first one, are set with CONTINUE hmerge.setVal(STMerge.CONTINUE); } XWPFTableCell cell = table.getRow(row).getCell(colIndex); // Try getting the TcPr. Not simply setting an new one every time. CTTcPr tcPr = cell.getCTTc().getTcPr(); if (tcPr != null) { tcPr.setHMerge(hmerge); } else { // only set an new TcPr if there is not one already tcPr = CTTcPr.Factory.newInstance(); tcPr.setHMerge(hmerge); cell.getCTTc().setTcPr(tcPr); } } }
//判断并返回图片类型对应的数字 public static int getPictureType(String picUrl){ int res = CustomXWPFDocument.PICTURE_TYPE_PICT; if(picUrl!=null){ if(picUrl.contains(".png") || picUrl.contains(".PNG")){ res=CustomXWPFDocument.PICTURE_TYPE_PNG; }else if(picUrl.contains(".dib") || picUrl.contains(".DIB")){ res = CustomXWPFDocument.PICTURE_TYPE_DIB; }else if(picUrl.contains(".emf") || picUrl.contains(".EMF")){ res = CustomXWPFDocument.PICTURE_TYPE_EMF; }else if(picUrl.contains(".jpg") || picUrl.contains(".JPG") || picUrl.contains(".jpeg") || picUrl.contains(".JPEG")){ res = CustomXWPFDocument.PICTURE_TYPE_JPEG; }else if(picUrl.contains(".wmf") || picUrl.contains(".WMF")){ res = CustomXWPFDocument.PICTURE_TYPE_WMF; } } return res; } %>
==============================================================================================
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="java.io.File" %> <%@ page import="jxl.*" %> <%@ page import="jxl.write.Label" %> <%@ page import="jxl.write.WritableSheet" %> <%@ page import="jxl.write.WritableWorkbook" %>
<% //标题行 String title[] = {"通用名称","商品名称","英文名称","拼音全码","主要成份","成份","性状","适应症/功能主治","规格型号","用法用量","不良反应","禁忌","注意事项","儿童用药","老年患者用药","孕妇及哺乳期妇女用药","药物相互作用","药物过量","药理毒理","药代动力学","贮藏","包装","有效期","执行标准","批准文号","生产企业"}; //操作执行 try { //t.xls为要新建的文件名 WritableWorkbook book= Workbook.createWorkbook(new File("/home/data/web/templates/merchant/template_13/doTest/testV2.xls")); //生成名为“第一页”的工作表,参数0表示这是第一页 WritableSheet sheet=book.createSheet("第一页",0);
//写入内容 for(int i=0; i<title.length; i++) { sheet.addCell(new Label(i,0,title[i])); }
for(int i=0; i<title.length; i++) { for(int j=1; j<4; j++) { sheet.addCell(new Label(i, j, "test" + j + "-" + i)); } } //写入数据 book.write(); //关闭文件 book.close(); } catch(Exception e) { } %>
int i=0; i<title.length; i++) { sheet.addCell(new Label(i,0,title[i])); } for(int i=0; i<title.length; i++) { for(int j=1; j<4; j++) { sheet.addCell(new Label(i, j, "test" + j + "-" + i)); } } //写入数据 book.write(); //关闭文件 book.close(); } catch(Exception e) { }%>
int i=0; i<title.length; i++) { sheet.addCell(new Label(i,0,title[i])); } for(int i=0; i<title.length; i++) { for(int j=1; j<4; j++) { sheet.addCell(new Label(i, j, "test" + j + "-" + i)); } } //写入数据 book.write(); //关闭文件 book.close(); } catch(Exception e) { }%>
|
|