pom.xml
org.apache.poi
poi
3.14
org.apache.poi
poi-ooxml
3.14
org.apache.poi
poi-ooxml-schemas
3.14
org.apache.poi
poi-scratchpad
3.14
Controller
/**
* 导出解译标志Word
* @param id
* @return
* @author ygc
* @throws IOException
*/
@Transactional(propagation=Propagation.SUPPORTS)
@ResponseBody
@RequestMapping("/ExportInterpretationSignsWord")
public ResponseEntity ExportInterpretationSignsWord(@RequestParam String token,HttpServletRequest request,
HttpServletResponse response,
@RequestParam int id) throws IOException{
response.setContentType("text/html;charset=UTF-8");
response.setHeader("Content-type", "application/json;charset=UTF-8");
if(RedisPool.checkToken(token)==false) {
return null;
}
// JsonResultInfo info=new JsonResultInfo();
ResponseEntity temp=buildService.ExportInterpretationSignsWordById(id);
// info.setCode(200);
// info.setData(temp);
// info.setResult("成功");
// info.setMessage("导出解译标志Word");
// String r = JSON.toJSONString(info,SerializerFeature.WriteMapNullValue);
// response.setContentType("text/json");
// response.getWriter().print(r);
return temp;
}
Service
public ResponseEntity ExportInterpretationSignsWordById(int id) throws IOException {
LtinterpretationsignsjbxxExtends temp = buildMapper.findInterpretationSignsWordInfoById(id);
XWPFDocument doc = new XWPFDocument();
XWPFParagraph para;
XWPFRun run;
//创建表格
XWPFTable table = doc.createTable(8,4);
table.setCellMargins(3, 5, 3, 5);
// table.addNewCol();//添加新列
// table.createRow();//添加新行
String[] title1 = new String[]{"编号"," ","项目类型"," "};
String[] title2 = new String[]{"调查日期"," ","建设状态"," "};
XWPFTableRow row;
XWPFTableCell cell;
CTTcPr cellPr;
for(int j=0;j<8;j++){
row = table.getRow(j);
row.setHeight(600);
//第一行
for(int i=0;i paragraphs = cell.getParagraphs();
XWPFParagraph newPara = paragraphs.get(0);
XWPFRun imageCellRunn = newPara.createRun();
System.out.println(temp.getXmimgpath()+File.separator+temp.getXmimgname()+"图片路径");
File image = new File(temp.getXmimgpath()+File.separator+temp.getXmimgname());
if(!image.exists()){
continue;
}
try (FileInputStream is = new FileInputStream(temp.getXmimgpath()+File.separator+temp.getXmimgname())) {
imageCellRunn.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, image.getName(), Units.toEMU(160), Units.toEMU(160)); // 200x200 pixels
}catch (Exception e){
e.printStackTrace();
}
}
if(y==1) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
if(y==2) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
cellPr = cell.getCTTc().addNewTcPr();
//设置宽度
cellPr.addNewTcW().setW(BigInteger.valueOf(5000));
//第一个段落
para = cell.getParagraphs().get(0);
//设置对齐方式
para.setAlignment(ParagraphAlignment.LEFT);
//居左
para.setFontAlignment(1);
run = para.createRun();
run.addBreak();
run.addBreak();
cell.setText("(1)照片拍摄位置经纬度:");
run = para.createRun();
run.addBreak();
run.addBreak();
cell.setText(" 经度:"+temp.getXmlngdu()+"°"+temp.getXmlngfen()+"′"+temp.getXmlngmiao()+"″");
run = para.createRun();
run.addBreak();
run.addBreak();
cell.setText(" 纬度:"+temp.getXmlatdu()+"°"+temp.getXmlatfen()+"′"+temp.getXmlatmiao()+"″");
run = para.createRun();
run.addBreak();
run.addBreak();
cell.setText("(2)照片拍摄时间:"+CommonUtil.transformDate(CommonUtil.getSysDate(temp.getXmpictime())));
run = para.createRun();
run.addBreak();
run.addBreak();
cell.setText(null);
}
if(y==3) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
}
}
if(j==6){
//String imgPath="E:\\design_file_info_path\\设计文件\\3.jpg";
for(int y=0;y<4;y++){
if(y==0) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
List paragraphs = cell.getParagraphs();
XWPFParagraph newPara = paragraphs.get(0);
XWPFRun imageCellRunn = newPara.createRun();
File image = new File(temp.getXmmiddleaddress()+File.separator+temp.getXmmiddlepicname());
if(!image.exists()){
continue;
}
try (FileInputStream is = new FileInputStream(temp.getXmmiddleaddress()+File.separator+temp.getXmmiddlepicname())) {
imageCellRunn.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, image.getName(), Units.toEMU(160), Units.toEMU(160)); // 200x200 pixels
}catch (Exception e){
e.printStackTrace();
}
}
if(y==1) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
if(y==2) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
cellPr = cell.getCTTc().addNewTcPr();
//设置宽度
cellPr.addNewTcW().setW(BigInteger.valueOf(5000));
//第一个段落
para = cell.getParagraphs().get(0);
//设置对齐方式
para.setAlignment(ParagraphAlignment.LEFT);
//居左
para.setFontAlignment(1);
run = para.createRun();
run.addBreak();
run.addBreak();
cell.setText("(1)中分辨率影像类型:"+temp.getXmmiddletype());
run = para.createRun();
run.addBreak();
run.addBreak();
cell.setText("(2)中分辨率影像成像时间:"+CommonUtil.transformDate(CommonUtil.getSysDate(temp.getXmmiddletime())));
run = para.createRun();
run.addBreak();
run.addBreak();
cell.setText("(3)中分辨率影像空间分辨率:"+temp.getXmmiddleresolution());
run = para.createRun();
run.addBreak();
run.addBreak();
cell.setText("(4)中分辨率影像特征:"+temp.getXmmiddletype());
run = para.createRun();
run.addBreak();
run.addBreak();
cell.setText(null);
}
if(y==3) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
}
}
if(j==7){
// String imgPath="E:\\design_file_info_path\\设计文件\\4.jpg";
for(int y=0;y<4;y++){
if(y==0) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
List paragraphs = cell.getParagraphs();
XWPFParagraph newPara = paragraphs.get(0);
XWPFRun imageCellRunn = newPara.createRun();
System.out.println(temp.getXmhighaddress()+File.separator+temp.getXmhighpicname());
File image = new File(temp.getXmhighaddress(),temp.getXmhighpicname());
System.out.println(image.getAbsolutePath());
if(!image.exists()){
continue;
}
try (FileInputStream is = new FileInputStream(image)) {
imageCellRunn.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, image.getName(), Units.toEMU(160), Units.toEMU(160)); // 200x200 pixels
}catch (Exception e){
e.printStackTrace();
}
}
if(y==1) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
if(y==2) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
cellPr = cell.getCTTc().addNewTcPr();
//设置宽度
cellPr.addNewTcW().setW(BigInteger.valueOf(5000));
//第一个段落
para = cell.getParagraphs().get(0);
//设置对齐方式
para.setAlignment(ParagraphAlignment.LEFT);
//居左
para.setFontAlignment(1);
run = para.createRun();
run.addBreak();
run.addBreak();
cell.setText("(1)高分辨率影像类型:"+temp.getXmhightype());
run = para.createRun();
run.addBreak();
run.addBreak();
cell.setText("(2)高分辨率影像成像时间:"+CommonUtil.transformDate(CommonUtil.getSysDate(temp.getXmhightime())));
run = para.createRun();
run.addBreak();
run.addBreak();
cell.setText("(3)高分辨率影像空间分辨率:"+temp.getXmhighresolution());
run = para.createRun();
run.addBreak();
run.addBreak();
cell.setText("(4)高分辨率影像特征:"+temp.getXmhighfeature());
run = para.createRun();
run.addBreak();
run.addBreak();
cell.setText(null);
}
if(y==3) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
}
}
}
String fileName="解析标志基本信息.doc";
String path=Commons.INTERPRETATION_SIGNS_WORD+File.separator+fileName;
File storeDirectory = new File(Commons.INTERPRETATION_SIGNS_WORD);
if (!storeDirectory.exists() &&!storeDirectory.isDirectory()) {
storeDirectory.mkdirs();
}
File file = new File(path);
FileOutputStream os = new FileOutputStream(file);
doc.write(os);
if(os!=null){
try{
os.close();
System.out.println("文件已输出!");
HttpHeaders headers = new HttpHeaders();
// 下载显示的文件名,解决中文名称乱码问题
String downloadFielName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
// 通知浏览器以attachment(下载方式)打开
headers.setContentDispositionFormData("attachment", downloadFielName);
// application/octet-stream : 二进制流数据(最常见的文件下载)。
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
}
catch(IOException e){
e.printStackTrace();
}
}
return null;
}
package com.kero99.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.List;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
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.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
/**
* 导出word
* @author ygc
*/
public class ExportWord {
public static void main(String[] args) throws IOException, InvalidFormatException {
XWPFDocument doc = new XWPFDocument();
XWPFParagraph para;
XWPFRun run;
//创建表格
XWPFTable table = doc.createTable(8,4);
table.setCellMargins(3, 5, 3, 5);
// table.addNewCol();//添加新列
// table.createRow();//添加新行
String[] title1 = new String[]{"编号"," ","项目类型"," "};
String[] title2 = new String[]{"调查日期"," ","建设状态"," "};
XWPFTableRow row;
XWPFTableCell cell;
CTTcPr cellPr;
for(int j=0;j<8;j++){
row = table.getRow(j);
row.setHeight(600);
//第一行
for(int i=0;i paragraphs = cell.getParagraphs();
XWPFParagraph newPara = paragraphs.get(0);
XWPFRun imageCellRunn = newPara.createRun();
File image = new File(imgPath);
if(!image.exists()){
continue;
}
try (FileInputStream is = new FileInputStream(imgPath)) {
imageCellRunn.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, image.getName(), Units.toEMU(180), Units.toEMU(180)); // 200x200 pixels
}catch (Exception e){
e.printStackTrace();
}
}
if(y==1) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
if(y==2) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
cell.setText("(1)照片拍摄位置经纬度:");
cellPr = cell.getCTTc().addNewTcPr();
//设置宽度
cellPr.addNewTcW().setW(BigInteger.valueOf(5000));
//第一个段落
para = cell.getParagraphs().get(0);
//设置对齐方式
para.setAlignment(ParagraphAlignment.LEFT);
//居左
para.setFontAlignment(1);
run = para.createRun();
run.addBreak();
run.addBreak();
cell.setText(" 经度:");
// run.addBreak();
// run.addBreak();
run = para.createRun();
run.addBreak();
run.addBreak();
cell.setText(" 经度:");
run = para.createRun();
run.addBreak();
run.addBreak();
cell.setText("(2)照片拍摄时间:");
// run.addBreak();
// run.addBreak();
// cell.setText("(2)照片拍摄时间:");
// run.addBreak();
// cell.setText("纬度:");
// run.setText("(1)照片拍摄位置经纬度: 经度:");
// run.addCarriageReturn();
// run.addBreak();//换行
}
if(y==3) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
}
}
if(j==6){
String imgPath="E:\\design_file_info_path\\设计文件\\3.jpg";
for(int y=0;y<4;y++){
if(y==0) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
List paragraphs = cell.getParagraphs();
XWPFParagraph newPara = paragraphs.get(0);
XWPFRun imageCellRunn = newPara.createRun();
File image = new File(imgPath);
if(!image.exists()){
continue;
}
try (FileInputStream is = new FileInputStream(imgPath)) {
imageCellRunn.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, image.getName(), Units.toEMU(180), Units.toEMU(180)); // 200x200 pixels
}catch (Exception e){
e.printStackTrace();
}
}
if(y==1) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
if(y==2) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
}
if(y==3) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
}
}
if(j==7){
String imgPath="E:\\design_file_info_path\\设计文件\\4.jpg";
for(int y=0;y<4;y++){
if(y==0) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
List paragraphs = cell.getParagraphs();
XWPFParagraph newPara = paragraphs.get(0);
XWPFRun imageCellRunn = newPara.createRun();
File image = new File(imgPath);
if(!image.exists()){
continue;
}
try (FileInputStream is = new FileInputStream(imgPath)) {
imageCellRunn.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, image.getName(), Units.toEMU(180), Units.toEMU(180)); // 200x200 pixels
}catch (Exception e){
e.printStackTrace();
}
}
if(y==1) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
if(y==2) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
}
if(y==3) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
}
}
}
OutputStream os = new FileOutputStream("E:\\wordTest3.doc");
doc.write(os);
if(os!=null){
try{
os.close();
System.out.println("文件已输出!");
}
catch(IOException e){
e.printStackTrace();
}
}
}
}
测试版-导出样式
1.合并单元格
if(y==1) {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
}else {
cell = row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
2.单元格回车
run = para.createRun();
run.addBreak();
run.addBreak();
cell.setText(" 经度:");
run = para.createRun();
run.addBreak();
run.addBreak();
cell.setText(" 经度:");
3.调整字体大小
if(i==1) {
//这块查询的日期
title2[i]=CommonUtil.transformDate(
CommonUtil.getSysDate(temp.getXminvestigationdate())) ;
run = para.createRun();
run.setFontSize(11);
run.setText(title2[i]);
}
if(i==3){
//查询的状态
title2[i]=temp.getXmstate();
run = para.createRun();
run.setFontSize(11);
run.setText(title2[i]);
}
if(i==2) {
run.setText(title2[i]);
}
if(i==0) {
run.setText(title2[i]);
}