admin管理员组

文章数量:1122859

world

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:
一、POI 生成导出Word样式

二、详细代码

pom.xml

		<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.14</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.14</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>3.14</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>3.14</version></dependency>

Controller

	/*** 导出解译标志Word* @param id * @return * @author ygc * @throws IOException */@Transactional(propagation=Propagation.SUPPORTS)@ResponseBody@RequestMapping("/ExportInterpretationSignsWord")public  ResponseEntity<byte[]>  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<byte[]> 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<byte[]>  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<title1.length;i++){      //创建单元格cell = row.getCell(i);  //单元格属性cellPr = cell.getCTTc().addNewTcPr();  //设置宽度cellPr.addNewTcW().setW(BigInteger.valueOf(5000)); //垂直居中 cellPr.addNewVAlign().setVal(STVerticalJc.CENTER);//第一个段落para = cell.getParagraphs().get(0);  //设置对齐方式para.setAlignment(ParagraphAlignment.CENTER);  //段落的对齐方式 1左 2中 3右 4往上 左 不可写0和负数para.setFontAlignment(2);//一个XWPFRun代表具有相同属性的一个区域:一段文本run = para.createRun();//设置字体run.setFontFamily("仿宋"); //设置字体大小run.setFontSize(13);  if(j==0) {//字体宽
//	                    run.setBold(true);  if(i==1) {title1[i]=temp.getXmnumber();run = para.createRun();  run.setFontSize(11); run.setText(title1[i]);  }if(i==3) {title1[i]=temp.getXmtype();run = para.createRun();  run.setFontSize(11); run.setText(title1[i]);  }if(i==2) {run.setText(title1[i]); }if(i==0) {run.setText(title1[i]); }}}    //第二行if(j==1){  for(int y=0;y<4;y++){ if(y==0) {cell = row.getCell(0); cellPr = cell.getCTTc().addNewTcPr();  cellPr.addNewTcW().setW(BigInteger.valueOf(5000));  para = cell.getParagraphs().get(0);  para.setAlignment(ParagraphAlignment.CENTER);  para.setFontAlignment(2);run = para.createRun();             run.setFontFamily("仿宋");  run.setFontSize(13);  run.setText("扰动类型");    }if(y==1) {cell = row.getCell(y); cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART); cell.setText(temp.getXmexcitation());}else {cell = row.getCell(y); cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE); }}}if(j==2){ for(int y=0;y<4;y++){ if(y==0) {cell = row.getCell(0); cellPr = cell.getCTTc().addNewTcPr();  cellPr.addNewTcW().setW(BigInteger.valueOf(5000));  para = cell.getParagraphs().get(0);  para.setAlignment(ParagraphAlignment.CENTER);  para.setFontAlignment(2);run = para.createRun();             run.setFontFamily("仿宋");  run.setFontSize(13);  run.setText("项目名称");  }if(y==1) {cell = row.getCell(y); cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART); cell.setText(temp.getXmname());}else {cell = row.getCell(y); cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE); }}}  for(int i=0;i<title2.length;i++){               cell = row.getCell(i);  cellPr = cell.getCTTc().addNewTcPr();  cellPr.addNewTcW().setW(BigInteger.valueOf(5000));  para = cell.getParagraphs().get(0);  para.setAlignment(ParagraphAlignment.CENTER);  para.setFontAlignment(2);run = para.createRun();             run.setFontFamily("仿宋");  run.setFontSize(13);  if(j==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]); }}} if(j==4){ for(int y=0;y<4;y++){ if(y==0) {cell = row.getCell(0); //单元格属性cellPr = cell.getCTTc().addNewTcPr();  //设置宽度cellPr.addNewTcW().setW(BigInteger.valueOf(5000));  //第一个段落para = cell.getParagraphs().get(0);  //设置对齐方式para.setAlignment(ParagraphAlignment.CENTER);  //居中para.setFontAlignment(2);run = para.createRun();             run.setFontFamily("仿宋");  run.setFontSize(13);      run.setText("详细地址");  }if(y==1) {cell = row.getCell(y); 	 cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART); cell.setText(temp.getXmdetailaddress());}else {cell = row.getCell(y); cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE); }}}  if(j==5){ //String imgPath="E:\\design_file_info_path\\设计文件\\2.jpg";for(int y=0;y<4;y++){ if(y==0) {cell = row.getCell(y); cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);List<XWPFParagraph> 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<XWPFParagraph> 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<XWPFParagraph> 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<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);}  catch(IOException e){  e.printStackTrace();  }  }  return null;}

三、导出Word测试工具–测试版

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<title1.length;i++){      //创建单元格cell = row.getCell(i);  //单元格属性cellPr = cell.getCTTc().addNewTcPr();  //设置宽度cellPr.addNewTcW().setW(BigInteger.valueOf(5000)); //垂直居中 cellPr.addNewVAlign().setVal(STVerticalJc.CENTER);//第一个段落para = cell.getParagraphs().get(0);  //设置对齐方式para.setAlignment(ParagraphAlignment.CENTER);  //段落的对齐方式 1左 2中 3右 4往上 左 不可写0和负数para.setFontAlignment(2);//一个XWPFRun代表具有相同属性的一个区域:一段文本run = para.createRun();//设置字体run.setFontFamily("仿宋"); //设置字体大小run.setFontSize(11);  if(j==0) {//字体宽
//                    run.setBold(true);  run.setText(title1[i]);  }}    //第二行if(j==1){  for(int y=0;y<4;y++){ if(y==0) {cell = row.getCell(0); cellPr = cell.getCTTc().addNewTcPr();  cellPr.addNewTcW().setW(BigInteger.valueOf(5000));  para = cell.getParagraphs().get(0);  para.setAlignment(ParagraphAlignment.CENTER);  para.setFontAlignment(2);run = para.createRun();             run.setFontFamily("仿宋");  run.setFontSize(13);  run.setText("扰动类型");    }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); }}}if(j==2){ for(int y=0;y<4;y++){ if(y==0) {cell = row.getCell(0); cellPr = cell.getCTTc().addNewTcPr();  cellPr.addNewTcW().setW(BigInteger.valueOf(5000));  para = cell.getParagraphs().get(0);  para.setAlignment(ParagraphAlignment.CENTER);  para.setFontAlignment(2);run = para.createRun();             run.setFontFamily("仿宋");  run.setFontSize(13);  run.setText("项目名称");  }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); }}}  for(int i=0;i<title2.length;i++){               cell = row.getCell(i);  cellPr = cell.getCTTc().addNewTcPr();  cellPr.addNewTcW().setW(BigInteger.valueOf(5000));  para = cell.getParagraphs().get(0);  para.setAlignment(ParagraphAlignment.CENTER);  para.setFontAlignment(2);run = para.createRun();             run.setFontFamily("仿宋");  run.setFontSize(13);  if(j==3) {run.setText(title2[i]);  }} if(j==4){ for(int y=0;y<4;y++){ if(y==0) {cell = row.getCell(0); //单元格属性cellPr = cell.getCTTc().addNewTcPr();  //设置宽度cellPr.addNewTcW().setW(BigInteger.valueOf(5000));  //第一个段落para = cell.getParagraphs().get(0);  //设置对齐方式para.setAlignment(ParagraphAlignment.CENTER);  //居中para.setFontAlignment(2);run = para.createRun();             run.setFontFamily("仿宋");  run.setFontSize(13);      run.setText("详细地址");  }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); }}}  if(j==5){ String imgPath="E:\\design_file_info_path\\设计文件\\2.jpg";for(int y=0;y<4;y++){ if(y==0) {cell = row.getCell(y); cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);List<XWPFParagraph> 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<XWPFParagraph> 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<XWPFParagraph> 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]); }

————————————————
版权声明:本文为CSDN博主「南归北隐」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:

本文标签: World