java结果弹出表格_Java POI导出Word表格并使用输出流下载文件弹出打开保存框

/**

* 导出Word表格

* @author Saffi

*/

@RequestMapping("exportWord.action")

public void exportWord(HttpServletResponse response) throws IOException {

try {

List docuList =entityService.listDocumentaryForProduct();

// 获取当前时间

Date date = new Date();

// 将时间格式定义为年-月-日

DateFormat format = new SimpleDateFormat("yyyy-MM-dd");

// 将时间格式转换为String类型

String createtime = format.format(date);

//创建一个Word文件

XWPFDocument xdoc = new XWPFDocument();

XWPFParagraph xp = xdoc.createParagraph();

XWPFRun r1 = xp.createRun();

//设置表格标题

r1.setText("发货核对表");

r1.setFontFamily("微软雅黑");  //设置字体

r1.setFontSize(12);  //设置字体大小

r1.setTextPosition(10);

r1.setBold(true);

xp.setAlignment(ParagraphAlignment.CENTER);

//创建一个表格

XWPFTable xTable = xdoc.createTable(docuList.size()+1, 8);

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("11000"));

tblWidth.setType(STTblWidth.DXA);

//创建标题

exportWordUtil wutil = new exportWordUtil();

int i = 0;

xTable.getRow(i).setHeight(380);

wutil.setCellText(xdoc, xTable.getRow(i).getCell(0), "日期", "FFFFFF ",

wutil.getCellWidth(0));

wutil.setCellText(xdoc, xTable.getRow(i).getCell(1), "姓名", "FFFFFF",

wutil.getCellWidth(1));

wutil.setCellText(xdoc, xTable.getRow(i).getCell(2), "单号", "FFFFFF",

wutil.getCellWidth(2));

i++;

//创建行

for (int i2 = i; i2 <= docuList.size(); i2++) {

int i1=i2-1;

wutil.setCellText(xdoc, xTable.getRow(i2).getCell(0), docuList.get(i1).getRecorddate(), "FFFFFF",wutil.getCellWidth(i));

wutil.setCellText(xdoc, xTable.getRow(i2).getCell(1), docuList.get(i1).getName(),"FFFFFF", wutil.getCellWidth(i));

wutil.setCellText(xdoc, xTable.getRow(i2).getCell(2), docuList.get(i1).getNumber(), "FFFFFF",wutil.getCellWidth(i));

}

// 设置Word文件名,并以中文进行编码

String codedFileName = new String("Word文件名".getBytes("gbk"), "iso-8859-1");

response.setHeader("Content-Disposition", "attachment;filename=" + codedFileName + createtime+".docx");

// 响应类型,编码

response.setContentType("application/octet-stream;charset=UTF-8");

// 形成输出流

OutputStream osOut = response.getOutputStream();

xdoc.write(osOut);

// 刷新此输出流并强制将所有缓冲的输出字节被写出

osOut.flush();

// 关闭流

osOut.close();

} catch (Exception e) {

e.printStackTrace();

response.sendRedirect("error.action");

log.error("系统错误", e.fillInStackTrace());

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值