java导出word

java实现导出word操作

实现代码
···
package cn.ttitcn.xj.controller;

import java.io.FileOutputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.XWPFTableCell.XWPFVertAlign;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
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.CTTcBorders;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
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 org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.STBorderImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.hutool.core.convert.Convert;
import cn.ttitcn.common.annotation.Log;
import cn.ttitcn.common.constant.Constants;
import cn.ttitcn.common.core.domain.AjaxResult;
import cn.ttitcn.common.enums.BusinessType;
import cn.ttitcn.common.util.ExcelUtil;
import cn.ttitcn.common.util.StringUtils;
import cn.ttitcn.framework.web.BaseController;

@RestController
@RequestMapping(“xj/studentroll”)
public class StudentrollController extends BaseController {

/**
 * 导出excel
 * 
 * @param studentroll
 * @return
 */
@Log(title = XJConstant.XJ_STUDENTROLLE, businessType = BusinessType.EXPORT)
@PreAuthorize("@ss.hasPermi('xj:studentroll:export')")
@PostMapping("export")
public AjaxResult export(@RequestBody Studentroll studentroll) {
	List<Studentroll> list = studentrollService.list(studentroll);
	ExcelUtil<Studentroll> util = new ExcelUtil<Studentroll>(Studentroll.class);
	return util.exportExcel(list, "学籍表");
}


/**
 * 导入
 * 
 * @param studentroll
 * @return
 */
@Log(title = XJConstant.XJ_STUDENTROLLE, businessType = BusinessType.IMPORT)
@PreAuthorize("@ss.hasPermi('xj:studentroll:import')")
@PostMapping("import")
public AjaxResult import1(@RequestBody Studentroll studentroll) {
	return success();
}



/**
 * 导出word(一个学生占一页word)
 * 
 * @param studentroll
 * @return
 */
@Log(title = XJConstant.XJ_STUDENTROLLE, businessType = BusinessType.EXPORT)
@PreAuthorize("@ss.hasPermi('xj:studentroll:export')")
@PostMapping("exportWord")
public AjaxResult exportWord(@RequestBody(required = false) Studentroll studentroll) {
    try {
		createSimpleTable("D:/"+ System.currentTimeMillis() + ".docx");
	} catch (Exception e) {
		e.printStackTrace();
	}
	return success();
}

public static void main(String[] args) {
	 try {
			createSimpleTable("D:/"+ System.currentTimeMillis() + ".docx");
		} catch (Exception e) {
			e.printStackTrace();
		}
}

public static void createSimpleTable(String savePath) throws Exception {
	XWPFDocument xdoc = new XWPFDocument();
	XWPFParagraph xp = xdoc.createParagraph();
	xp.setSpacingBefore(0);
	XWPFRun r1 = xp.createRun();
	r1.setText("学生个人学籍信息");
	r1.addBreak(); // 换行
	r1.setFontFamily("宋体");
	r1.setFontSize(16);
	r1.setTextPosition(10);
	r1.setBold(true);
	xp.setAlignment(ParagraphAlignment.CENTER);
	
	Integer col_total_count = 4; // 表格最多的列数
	Integer data_count = 10; // 需要创建的总条数

	XWPFTable xTable = xdoc.createTable(1, col_total_count);

	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("8600"));
	tblWidth.setType(STTblWidth.DXA);

	/**创建表头数据*/
	int i = 0;
	xTable.getRow(i).setHeight(500);
	setCellText(xdoc, xTable.getRow(i).getCell(0), "姓名:", "FFFFFF", getCellWidth(0));
	setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
	setCellText(xdoc, xTable.getRow(i).getCell(2), "学号:", "FFFFFF", getCellWidth(2));
	setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));	
	
	// 创建表格内容
	i++;
	
	/**向表中添加新行*/
	XWPFTableRow rw8	= xTable.insertNewTableRow(i);
	for (int j = 0; j < 4; j++) {
		rw8.addNewTableCell();
	}
	rw8.setHeight(500);
	setCellText(xdoc, xTable.getRow(i).getCell(0), "家庭成员联系电话:", "FFFFFF", getCellWidth(0));
	setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
	setCellText(xdoc, xTable.getRow(i).getCell(2), "家庭成员工作单位:", "FFFFFF", getCellWidth(2));
	setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
	
	
	/**向表中添加新行*/
	XWPFTableRow rw7	= xTable.insertNewTableRow(i);
	for (int j = 0; j < 4; j++) {
		rw7.addNewTableCell();
	}
	rw7.setHeight(500);
	setCellText(xdoc, xTable.getRow(i).getCell(0), "家庭成员姓名:", "FFFFFF", getCellWidth(0));
	setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
	setCellText(xdoc, xTable.getRow(i).getCell(2), "家庭成员关系:", "FFFFFF", getCellWidth(2));
	setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
	
	
	/**向表中添加新行*/
	XWPFTableRow rw6	= xTable.insertNewTableRow(i);
	for (int j = 0; j < 4; j++) {
		rw6.addNewTableCell();
	}
	rw6.setHeight(500);
	setCellText(xdoc, xTable.getRow(i).getCell(0), "是否建档立卡贫困户:", "FFFFFF", getCellWidth(0));
	setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
	setCellText(xdoc, xTable.getRow(i).getCell(2), "健康状况:", "FFFFFF", getCellWidth(2));
	setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
	
	
	/**向表中添加新行*/
	XWPFTableRow rw5	= xTable.insertNewTableRow(i);
	for (int j = 0; j < 4; j++) {
		rw5.addNewTableCell();
	}
	rw5.setHeight(500);
	setCellText(xdoc, xTable.getRow(i).getCell(0), "户口详细地址:", "FFFFFF", getCellWidth(0));
	mergeCellsHorizontal(xTable,i, 1, 3);

	/**向表中添加新行*/
	XWPFTableRow rw15 = xTable.insertNewTableRow(i);
	for (int j = 0; j < 4; j++) {
		rw15.addNewTableCell();
	}
	rw15.setHeight(500);
	setCellText(xdoc, xTable.getRow(i).getCell(0), "家庭现住址:", "FFFFFF", getCellWidth(0));
	mergeCellsHorizontal(xTable,i, 1, 3);
	
	/**向表中添加新行*/
	XWPFTableRow rw14	= xTable.insertNewTableRow(i);
	for (int j = 0; j < 4; j++) {
		rw14.addNewTableCell();
	}
	rw14.setHeight(500);
	setCellText(xdoc, xTable.getRow(i).getCell(0), "寄信地址:", "FFFFFF", getCellWidth(0));
	mergeCellsHorizontal(xTable,i, 1, 3);

// /*测试------寄信地址添加单独一行/
// XWPFTableRow rw14 = xTable.insertNewTableRow(i);
// for (int j = 0; j < 4; j++) {
// rw14.addNewTableCell();
// }
// rw14.setHeight(500);
// setCellText(xdoc, xTable.getRow(i).getCell(0), “寄信地址:”, “FFFFFF”, getCellWidth(0));
// //mergeCellsVertically(xTable, 1, 3, 5);
// mergeCellsHorizontal(xTable,i, 1, 3);
// //mergeCellsHorizontal(xTable, i, i, i);

	/**向表中添加新行*/
	XWPFTableRow rw9	= xTable.insertNewTableRow(i);
	for (int j = 0; j < 4; j++) {
		rw9.addNewTableCell();
	}
	rw9.setHeight(500);
	setCellText(xdoc, xTable.getRow(i).getCell(0), "出生地:", "FFFFFF", getCellWidth(0));
	setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
	setCellText(xdoc, xTable.getRow(i).getCell(2), "就读学校类型:", "FFFFFF", getCellWidth(2));
	setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
	
	/**向表中添加新行*/
	XWPFTableRow rw13 = xTable.insertNewTableRow(i);
	for (int j = 0; j < 4; j++) {
		rw13.addNewTableCell();
	}
	rw13.setHeight(500);
	setCellText(xdoc, xTable.getRow(i).getCell(0), "学生手机号码:", "FFFFFF", getCellWidth(0));
	setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
	setCellText(xdoc, xTable.getRow(i).getCell(2), "户口性质:", "FFFFFF", getCellWidth(2));
	setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
	

	/**向表中添加新行*/
	XWPFTableRow rw12 = xTable.insertNewTableRow(i);
	for (int j = 0; j < 4; j++) {
		rw12.addNewTableCell();
	}
	rw12.setHeight(500);
	setCellText(xdoc, xTable.getRow(i).getCell(0), "籍贯:", "FFFFFF", getCellWidth(0));
	setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
	setCellText(xdoc, xTable.getRow(i).getCell(2), "邮政编码:", "FFFFFF", getCellWidth(2));
	setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
	

	/**向表中添加新行*/
	XWPFTableRow rw10 = xTable.insertNewTableRow(i);
	for (int j = 0; j < 4; j++) {
		rw10.addNewTableCell();
	}
	rw10.setHeight(500);
	setCellText(xdoc, xTable.getRow(i).getCell(0), "入学方式:", "FFFFFF", getCellWidth(0));
	setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
	setCellText(xdoc, xTable.getRow(i).getCell(2), "就读方式:", "FFFFFF", getCellWidth(2));
	setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
	
	
	/**向表中添加新行*/
	XWPFTableRow rw2	= xTable.insertNewTableRow(i);
	for (int j = 0; j < 4; j++) {
		rw2.addNewTableCell();
	}
	rw2.setHeight(500);
	setCellText(xdoc, xTable.getRow(i).getCell(0), "身份证号:", "FFFFFF", getCellWidth(0));
	setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
	setCellText(xdoc, xTable.getRow(i).getCell(2), "出生日期:", "FFFFFF", getCellWidth(2));
	setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
	
	
	/**向表中添加新行*/
	XWPFTableRow rw4	= xTable.insertNewTableRow(i);
	for (int j = 0; j < 4; j++) {
		rw4.addNewTableCell();
	}
	rw4.setHeight(500);
	setCellText(xdoc, xTable.getRow(i).getCell(0), "政治面貌:", "FFFFFF", getCellWidth(0));
	setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
	setCellText(xdoc, xTable.getRow(i).getCell(2), "学籍号:", "FFFFFF", getCellWidth(2));
	setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
	
	
	/**向表中添加新行*/
	XWPFTableRow rw11 = xTable.insertNewTableRow(i);
	for (int j = 0; j < 4; j++) {
		rw11.addNewTableCell();
	}
	rw11.setHeight(500);
	setCellText(xdoc, xTable.getRow(i).getCell(0), "专业:", "FFFFFF", getCellWidth(0));
	setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
	setCellText(xdoc, xTable.getRow(i).getCell(2), "班级:", "FFFFFF", getCellWidth(2));
	setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
	
	
	/**向表中添加新行*/
	XWPFTableRow rw	= xTable.insertNewTableRow(i);
	for (int j = 0; j < 4; j++) {
		rw.addNewTableCell();
	}
	rw.setHeight(500);
	setCellText(xdoc, xTable.getRow(i).getCell(0), "性别:", "FFFFFF", getCellWidth(0));
	setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
	setCellText(xdoc, xTable.getRow(i).getCell(2), "证件类型:", "FFFFFF", getCellWidth(2));
	setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
	
	
	/**向表中添加新行*/
	XWPFTableRow rw3	= xTable.insertNewTableRow(i);
	for (int j = 0; j < 4; j++) {
		rw3.addNewTableCell();
	}
	rw3.setHeight(500);
	setCellText(xdoc, xTable.getRow(i).getCell(0), "民族:", "FFFFFF", getCellWidth(0));
	setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
	setCellText(xdoc, xTable.getRow(i).getCell(2), "国籍/地区:", "FFFFFF", getCellWidth(2));
	setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
	

	FileOutputStream fos = new FileOutputStream(savePath);
	xdoc.write(fos);
	fos.close();
}

/**
 * 设置表头内容
 * @param xDocument
 * @param cell
 * @param text
 * @param bgcolor
 * @param width
 */
private static void setCellText(XWPFDocument xDocument, 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);
	cell.setVerticalAlignment(XWPFVertAlign.CENTER);
	CTTcPr ctPr = cttc.addNewTcPr();
	ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
	cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
	//cell.getCTTc().getTcPr().getTcBorders().addNewRight().setVal();
	cell.setText(text);
}

/**
 * 设置列宽
 * 
 * @param index
 * @return
 */
private static int getCellWidth(int index) {
	int cwidth = 1000;
	if (index == 0) {
		cwidth = 1800;
	} else if (index == 1) {
		cwidth = 1800;
	} else if (index == 2) {
		cwidth = 1800;
	} else if (index == 3) {
		cwidth = 1800;
	}
	return cwidth;
}

/***
*  跨行合并  
* @param table
* @param col  合并列
* @param fromRow 起始行
* @param toRow   终止行
*/
private 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 ) {  
               cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);  
           } else {  
               cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);  
           }  
       }  
   }


/***
* 跨列合并 
* @param table
* @param row 所合并的行
* @param fromCell  起始列
* @param toCell   终止列
*/
private 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 ) {  
               cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);  
           } else {  
               cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);  
           }  
       }  
   }  

}

···

效果图:

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: @preauthorize(@ss.haspermi) 是Spring Security中的注解,用于在方法执行前进行权限检查。其中 @ss.haspermi 是自定义的权限表达式,表示当前用户是否拥有某个权限。 具体来说,当一个方法被 @preauthorize(@ss.haspermi) 注解修饰时,Spring Security会在方法执行前调用 SecurityExpressionRoot 类的 hasPermission 方法,该方法会根据 @ss.haspermi 中定义的权限表达式,判断当前用户是否有权限执行该方法。 例如,@preauthorize("@ss.haspermi('user:add')") 表示只有拥有 "user:add" 权限的用户才能执行该方法。如果当前用户没有该权限,则会抛出 AccessDeniedException 异常,方法不会被执行。 总之,@preauthorize(@ss.haspermi) 注解是 Spring Security 中非常重要的权限控制注解,可以帮助我们实现精细化的权限控制。 ### 回答2: @preauthorize(@ss.haspermi 这是一个Spring Security的注解,它用来控制方法或类是否被授权访问。@preauthorize表示在方法调用前先进行权限检查,如果用户没有相应权限,则不允许访问该方法。 @ss.haspermi表示只有拥有指定权限的用户才能访问该方法或类。 在Spring Security中,可以通过配置SecurityConfig来设置各种权限配置。在SecurityConfig中可以设置用户、角色、资源以及访问控制等信息,通过这些配置可以控制Spring应用程序中的访问控制。@preauthorize和@ss.haspermi就是Spring Security控制访问的重要注解之一。 使用这些注解,首先需要在pom.xml中引入spring-security-core和spring-security-config两个jar包,然后在配置类中使用@EnableGlobalMethodSecurity注解启用Spring Security方法级别的安全功能。接着,在方法或类中使用@preauthorize和@ss.haspermi注解来控制访问授权。这样,当用户访问需要授权的方法时,Spring Security会先验证用户是否有相应的权限,如果有,则允许访问;否则,则拒绝访问。 @preauthorize和@ss.haspermi注解可以帮助开发者在Spring应用程序中实现精细化的访问控制,提高系统的安全性和可靠性。因此,在开发过程中,合理地使用这些注解可以使开发者的工作变得更加高效和智能。 ### 回答3: @preauthorize(@ss.haspermi是Spring Security框架中的一项功能,在应用程序设计中起到了安全保障的作用。@preauthorize是一个注解,用于在代码级别上对用户权限进行判断,并决定是否允许用户执行某些操作。 @ss.hasPermi是Spring Security框架中的一个方法,它可以查询当前用户是否具有某种权限。通常情况下,@preauthorize注解与@ss.hasPermi方法是一起使用的,通过配置注解的参数来实现对当前用户是否具备某些操作权限的判断。 在实际应用中,@preauthorize注解通常是用在Controller层的方法中。例如,在一个Java Web应用中,对于某个需要权限才能访问的URL,可以在Controller方法上添加@preauthorize注解,并且指定该URL所需要的权限,比如: @PreAuthorize("@ss.hasPermi('sys:user:add')") 在上述代码中,@preauthorize注解中的@ss.hasPermi('sys:user:add')就是使用了@ss.hasPermi方法查询当前用户是否具有sys:user:add权限。如果用户具有该权限,则该Controller方法将被允许访问,否则会被拒绝访问。 总之,@preauthorize(@ss.hasPermi的使用可以在代码级别上实现对用户权限的授权和访问控制,提高应用程序的安全性和可靠性,是Java Web应用开发中不可或缺的一项技术。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值