个人尝试出的方法 如果有误差见谅
poi导出word文档
需要的jar包
poi-3.9-20180503.jar
poi-examples-3.9-20180503.jar
poi-excelant-3.9-20180503.jar
poi-ooxml-3.9-20180503.jar
poi-ooxml-schemas-3.9-20180503.jar
poi-scratchpad-3.9-20180503.jar
xmlbeans-3.1.0.jar
注:
创建表格的方法
/*
*
* 设置表格样式 — 格子 内容 颜色 宽度
*/
public void setCellText(XWPFTableCell cell, String text, String bgcolor, int width) {
CTTc cttc = cell.getCTTc();
CTTcPr cellPr = cttc.addNewTcPr();
cellPr.addNewTcW().setW(BigInteger.valueOf(width));
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);
}
/**
* 跨列合并
* @param table
* @param row 所合并的行
* @param fromCell 起始列
* @param toCell 终止列
*/
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);
}
}
}
/**
* 跨行合并
* @param table
* @param col 合并的列
* @param fromRow 起始行
* @param toRow 终止行
*/
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);
}
}
}
操作步骤:
一;创建文档对象 大体段落位置要实现安排布置好
XWPFDocument xdoc =new XWPFDocument();//创建文档对象
//标题
XWPFParagraph xp0 =xdoc.createParagraph();//段落
xp0.setAlignment(ParagraphAlignment.CENTER);//段落对齐中央
//表格
创建表格之前确认表格 列/行数
Integer col_total_count =10;//列数(实际的对象属性和数据库字段数)
Integer data_count =list.size()+6;//行数(根据实际提取的数据记录数)
XWPFTable table =xdoc.createTable(data_count, col_total_count);
//结尾
XWPFParagraph xp1 =xdoc.createParagraph();//段落
xp1.setAlignment(ParagraphAlignment.LEFT);//段落对齐左
二;段落加入内容
//标题
XWPFRun run0 =xp0.createRun();//创建行
run0.setText("标题内容");
run0.setFontFamily("黑体");//字体类型
run0.setFontSize(15);//字体大小
run0.setTextPosition(10);//行间距
run0.setBold(true);//加粗
//表格
XWPFTableCell cell =null;//创建空格
XWPFTableRow row0 =table.getRow(0);//创建表格表头—行
row0.setHeight(400); //行—高
cell =row0.getCell(0);//确定表行和格子的位置从0~n
setCellText(cell, "月 份", "FFFFFF", 1000);
cell =row0.getCell(1);
setCellText(cell, "电", "FFFFFF", 1000);
cell =row0.getCell(2);
setCellText(cell, "", "FFFFFF", 1000);
cell =row0.getCell(3);
setCellText(cell, "", "FFFFFF", 1000);
cell =row0.getCell(4);
setCellText(cell, "", "FFFFFF", 1000);
cell =row0.getCell(5);
setCellText(cell, "", "FFFFFF", 1000);
cell =row0.getCell(6);
setCellText(cell, "水", "FFFFFF", 1000);
cell =row0.getCell(7);
setCellText(cell, "空格8", "FFFFFF", 1000);
cell =row0.getCell(8);
setCellText(cell, "蒸汽", "FFFFFF", 1000);
cell =row0.getCell(9);
setCellText(cell, "空格10", "FFFFFF", 1000);
cell =row0.getCell(10);
setCellText(cell, "天然气", "FFFFFF", 1000);
cell =row0.getCell(11);
setCellText(cell, "空格12", "FFFFFF", 1000);
//跨行 跨列 (跨行 跨列 需要在创建表行添加内容之后 否则格式会混乱达不到预期效果)
mergeCellsVertically(table, 0, 0, 1);
mergeCellsHorizontal(table, 0, 1, 5);//跨列空格2到空格6
mergeCellsHorizontal(table, 0, 6, 7);//跨2列
mergeCellsHorizontal(table, 0, 8, 9);//跨2列
mergeCellsHorizontal(table, 0, 10, 11);//跨2列
//创建表格表头第2行
XWPFTableRow row1 =table.getRow(1);//第二行
row1.setHeight(400);
cell =row1.getCell(0);
setCellText(cell, "第一行第一列跨过来了", "FFFFFF", 1500);//显示的内容是第一行第一格跨的内容
cell =row1.getCell(1);
setCellText(cell, "照明(度)", "FFFFFF", 1000);
cell =row1.getCell(2);
setCellText(cell, "空调(度)", "FFFFFF", 1000);
cell =row1.getCell(3);
setCellText(cell, "动力(度", "FFFFFF", 1000);
cell =row1.getCell(4);
setCellText(cell, "总电量(度)", "FFFFFF", 1000);
cell =row1.getCell(5);
setCellText(cell, "月电费(元)", "FFFFFF", 1000);
cell =row1.getCell(6);
setCellText(cell, "水量(吨)", "FFFFFF", 1000);
cell =row1.getCell(7);
setCellText(cell, "水费(元)", "FFFFFF", 1000);
cell =row1.getCell(8);
setCellText(cell, "气量(m3)", "FFFFFF", 1000);
cell =row1.getCell(9);
setCellText(cell, "气费(元)", "FFFFFF", 1000);
cell =row1.getCell(10);
setCellText(cell, "气量(m3)", "FFFFFF", 1000);
cell =row1.getCell(11);
setCellText(cell, "气费(元)", "FFFFFF", 1000);
//表格表体的内容 取出的数据在list集合中
int h =2;//表格已经创建过了两行表头
for(int i =0;i
rowi.setHeight(400);
cell = rowi.getCell(0);
setCellText(cell, Integer.toString(list.get(i).getYue())+"月", "FFFFFF", 1000);//表格内容转化为字符串输出
cell = rowi.getCell(1);
setCellText(cell, list.get(i).getDzhao().toString(), "FFFFFF", 1000);//单元格样式
cell = rowi.getCell(2);
setCellText(cell, list.get(i).getDkong().toString(), "FFFFFF", 1000);
cell = rowi.getCell(3);
setCellText(cell, list.get(i).getDdong().toString(), "FFFFFF", 1000);
cell = rowi.getCell(4);
setCellText(cell, list.get(i).getDzong().toString(), "FFFFFF", 1000);
cell = rowi.getCell(5);
setCellText(cell, list.get(i).getDyue().toString(), "FFFFFF", 1000);
cell = rowi.getCell(6);
setCellText(cell, list.get(i).getSliang().toString(), "FFFFFF", 1000);
cell = rowi.getCell(7);
setCellText(cell, list.get(i).getSfei().toString(), "FFFFFF", 1000);
cell = rowi.getCell(8);
setCellText(cell, list.get(i).getTliang().toString(), "FFFFFF", 1000);
cell = rowi.getCell(9);
setCellText(cell, list.get(i).getTfei().toString(), "FFFFFF", 1000);
cell = rowi.getCell(10);
setCellText(cell, list.get(i).getZliang().toString(), "FFFFFF", 1000);
cell = rowi.getCell(11);
setCellText(cell, list.get(i).getZfei().toString(), "FFFFFF", 1000);
}
//结尾
XWPFRun run1 =xp1.createRun();
runf1.setText("能源价格:(注:需提供近3年的能源账单。)");
runf1.addBreak();//换行
runf1.setText("电:"+fu.getDiandj()+"元/度");
runf1.addBreak();
runf1.setText("水:"+fu.getShuidj()+"元/度");
runf1.addBreak();
runf1.setText("蒸汽:"+fu.getZqidj()+"元/吨");
runf1.addBreak();
runf1.setText("天然气:"+fu.getTrqidj()+"元/立方");
runf1.setFontFamily("宋体");
runf1.setTextPosition(27);
runf1.setColor("000000");
runf1.setFontSize(12);
一般属性设置:(网上找的)
//默认:宋体(wps)/等线(office2016) 5号 两端对齐 单倍间距
runX.setText("舜发于畎亩之中, 傅说举于版筑之间, 胶鬲举于鱼盐之中, 管夷吾举于士...");
runX.setBold(false);//加粗
runX.setCapitalized(false);//我也不知道这个属性做啥的
//runX.setCharacterSpacing(5);//这个属性报错
runX.setColor("BED4F1");//设置颜色--十六进制
runX.setDoubleStrikethrough(false);//双删除线
runX.setEmbossed(false);//浮雕字体----效果和印记(悬浮阴影)类似
//runX.setFontFamily("宋体");//字体
runX.setFontFamily("华文新魏", FontCharRange.cs);//字体,范围----效果不详
runX.setFontSize(14);//字体大小
runX.setImprinted(false);//印迹(悬浮阴影)---效果和浮雕类似
runX.setItalic(false);//斜体(字体倾斜)
//runX.setKerning(1);//字距调整----这个好像没有效果
runX.setShadow(true);//阴影---稍微有点效果(阴影不明显)
//runX.setSmallCaps(true);//小型股------效果不清楚
//runX.setStrike(true);//单删除线(废弃)
runX.setStrikeThrough(false);//单删除线(新的替换Strike)
//runX.setSubscript(VerticalAlign.SUBSCRIPT);//下标(吧当前这个run变成下标)---枚举
//runX.setTextPosition(20);//设置两行之间的行间距//runX.setUnderline(UnderlinePatterns.DASH_LONG);//各种类型的下划线(枚举)//runX0.addBreak();//类似换行的操作(html的 br标签)runX0.addTab();//tab键runX0.addCarriageReturn();//回车键注意:addTab()和addCarriageReturn() 对setText()的使用先后顺序有关:比如先执行addTab,再写Text这是对当前这个Text的Table,反之是对下一个run的Text的Tab效果
word文档添加图片的方法
//工具类
public class CustomXWPFDocument extends XWPFDocument{
/*
* 导出现场勘查表添加图片 的工具类
*
*/
public CustomXWPFDocument(InputStream in) throws IOException {
super(in);
}
public CustomXWPFDocument() {
super();
}
public CustomXWPFDocument(OPCPackage pkg) throws IOException {
super(pkg);
}
/**
* @param id
* @param width
* 宽
* @param height
* 高
* @param paragraph
* 段落
*/
public void createPicture(int id, int width, int height,
XWPFParagraph paragraph) {
final int EMU = 9525;
width *= EMU;
height *= EMU;
String blipId = getAllPictures().get(id).getPackageRelationship()
.getId();
CTInline inline = paragraph.createRun().getCTR().addNewDrawing()
.addNewInline();
String picXml = ""
+ "
+ "
+ "
+ "
+ "\" name=\"Generated\"/>"
+ "
+ "
+ "
+ "
+ "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>"
+ "
+ "
+ "
+ "
+ "
+ "
+ "
+ "
+ "\" cy=\""
+ height
+ "\"/>"
+ "
+ "
+ "
+ "
+ "
+ "
+ "
inline.addNewGraphic().addNewGraphicData();
XmlToken xmlToken = null;
try {
xmlToken = XmlToken.Factory.parse(picXml);
} catch (XmlException xe) {
xe.printStackTrace();
}
inline.set(xmlToken);
inline.setDistT(0);
inline.setDistB(0);
inline.setDistL(0);
inline.setDistR(0);
CTPositiveSize2D extent = inline.addNewExtent();
extent.setCx(width);
extent.setCy(height);
CTNonVisualDrawingProps docPr = inline.addNewDocPr();
docPr.setId(id);
docPr.setName("图片名称");
docPr.setDescr("描述信息");
}
}
//操作步骤
一;创建文档固定图片添加位置
CustomXWPFDocument xdoc = new CustomXWPFDocument();//创建word文档
//插入图片位置1
XWPFParagraph xpPic1 =xdoc.createParagraph();
//插入图片位置2
XWPFParagraph xpPic2 =xdoc.createParagraph();
//文件图片最后追加到word
//图片1
for(int i =0;i
String uploadFilePathz =request.getSession().getServletContext().getRealPath("imgzong");//获取服务器中的位置
String pic1Path =listpic1s.get(i).getPicture();//获取数据库中的路经位置数据
buiwaimPath =pic1Path.substring(pic1Path.lastIndexOf("/"));//截取第二个/之后的文件名称
FileInputStream in1 =new FileInputStream(uploadFilePathz+pic1Path);
byte[] ba1 =new byte[in1.available()];
inz.read(ba1);
ByteArrayInputStream btye1InputStream =new ByteArrayInputStream(ba1);
//添加图片
try {
xdoc.addPictureData(btye1InputStream,CustomXWPFDocument.PICTURE_TYPE_JPEG);
//图片大小、位置
xdoc.createPicture(xdoc.getAllPictures().size() - 1, 300, 150, xpPic1 );
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
btye1InputStream.close();
in1.close();
}
}
//图片2亦同
在浏览器窗口下载
String filename="测试.docx”;
response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(filename, "utf-8"));
OutputStream os = response.getOutputStream();
xdoc.write(os);
os.close();