生成word文档-表格

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38795959/article/details/79241862

public void CreatWord(HttpServletResponse response)throws Exception{
//浏览器下载
OutputStream os=null;
try {
os = response.getOutputStream();// 取得输出流
response.reset();// 清空输出流
response.setHeader(“Content-disposition”, “attachment; filename=” + new String( “word.doc”.getBytes(“UTF-8”), “ISO8859-1”));
response.setContentType(“text/html;charset=UTF-8”);
response.setContentType(“application/x-excel”);
//创建word文档,并设置纸张大小
Document document = new Document(PageSize.A4,72, 72, 36, 36);//第一个参数是页面大小,接下来的参数分别是左、右、上和下页边距
//建立一个书写器,与document对象关联
// RtfWriter2.getInstance(document,new FileOutputStream(“e:/word.doc”));
RtfWriter2.getInstance(document,os);
//打开文本对象
document.open();
//设置字体–宋体
BaseFont bfChinese = BaseFont.createFont(“C:/Windows/Fonts/simfang.ttf”, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//字体样式
Font font = new Font(bfChinese,20.0f,1);
Font contentFont = new Font(bfChinese,12.0f,0);
//字样–附件
Paragraph annex = new Paragraph(“附件”,font);//段落
annex.setAlignment(0);//段落内容左对齐( 0:左对齐,1:居中,2:右对齐 )
annex.setSpacingBefore(30f);
System.out.println(annex);
document.add(annex);
//主标题
Paragraph title = new Paragraph(“价格监测质量考核工作季度报告表”,font);
title.setAlignment(1);//段落内容居中
document.add(title);
//创建一个4 列表格
Table table = new Table(4,9);//定义表格的4列8行
int width[] ={23,31,23,23};//定义表格宽度
table.setWidths(width);//设置每列宽度的比例
table.setWidth(100);//表格占页面宽度的比例
table.setBorderWidth(1);//表格边框的宽度
table.setAlignment(1);//表格居中(1为居中对齐,2为右对齐,3为左对齐)
table.setPadding(12F);//设置单元格之间的间距
table.setAutoFillEmptyCells(true);//自动填满
//创建单元格
Phrase agency = new Phrase(“机构名称:”,contentFont);
Cell cell1_1 = new Cell();
cell1_1.setColspan(2);
cell1_1.addElement(agency);
cell1_1.setBorder(0);
table.addCell(cell1_1);
Phrase data = new Phrase(“填报日期:”,contentFont);
Cell cell1_2 = new Cell();
cell1_2.setColspan(2);
cell1_2.addElement(data);
cell1_2.setBorder(0);
table.addCell(cell1_2);
Phrase cellContent2_1 = new Phrase(“本地执行全国价格监测报告制度监测点总数”,contentFont);
Cell cell2_1 = new Cell();
cell2_1.setRowspan(2);//合并单元格
cell2_1.addElement(cellContent2_1);
cell2_1.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
cell2_1.setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.addCell(cell2_1);
Phrase cellContent2_2 = new Phrase(“”,contentFont);
Cell cell2_2 = new Cell();
cell2_2.setRowspan(2);//合并单元格
cell2_2.addElement(cellContent2_2);
cell2_2.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
cell2_2.setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.addCell(cell2_2);
Phrase cellContent2_3 = new Phrase(“季度內监测点调整数(另附增减监测点具体信息)”,contentFont);
Cell cell2_3 = new Cell();
cell2_3.setRowspan(2);//合并单元格
cell2_3.addElement(cellContent2_3);
cell2_3.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
cell2_3.setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.addCell(cell2_3);
Phrase cellContent2_4 = new Phrase(“增:”,contentFont);
Cell cell2_4 = new Cell();
cell2_4.addElement(cellContent2_4);
cell2_4.setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.addCell(cell2_4);
Phrase cellContent2_5 = new Phrase(“减:”,contentFont);
Cell cell2_5 = new Cell();
cell2_5.addElement(cellContent2_5);
cell2_5.setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.addCell(cell2_5);
Phrase cellContent3_1 = new Phrase(“价格监测点现场检查数量及企业名称”,contentFont);
Cell cell3_1 = new Cell();
cell3_1.addElement(cellContent3_1);
cell3_1.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
cell3_1.setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
cell3_1.setMaxLines(30);
table.addCell(cell3_1);
Phrase cellContent3_2 = new Phrase(“”,contentFont);
Cell cell3_2 = new Cell();
cell3_2.setColspan(3);
cell3_2.addElement(cellContent3_2);
cell3_2.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
cell2_2.setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.addCell(cell3_2);
Phrase cellContent4_1 = new Phrase(“向国家发展改革委价格监测中心报送信息篇数”,contentFont);
Cell cell4_1 = new Cell();
cell4_1.addElement(cellContent4_1);
cell4_1.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
cell4_1.setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.addCell(cell4_1);
Phrase cellContent4_2 = new Phrase(“总 篇,其中:动态信息 篇,综合调研报告 篇,价格分析报告 篇”,contentFont);
Cell cell4_2 = new Cell();
cell4_2.addElement(cellContent4_2);
// cell4_2.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
cell4_2.setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.addCell(cell4_2);
Phrase cellContent4_3 = new Phrase(“被省(区、市)和大中城市党委、政府采用信息篇数”,contentFont);
Cell cell4_3 = new Cell();
cell4_3.addElement(cellContent4_3);
cell4_3.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
cell4_3.setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.addCell(cell4_3);
Phrase cellContent4_4 = new Phrase(“”,contentFont);
Cell cell4_4 = new Cell();
cell4_4.addElement(cellContent4_4);
cell4_4.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
cell4_4.setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.addCell(cell4_4);
Phrase cellContent5_1 = new Phrase(“得到省(区、市)和大中城市党委、政府领导批示信息篇数”,contentFont);
Cell cell5_1 = new Cell();
cell5_1.addElement(cellContent5_1);
cell5_1.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
cell5_1.setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.addCell(cell5_1);
Phrase cellContent5_2 = new Phrase(“”,contentFont);
Cell cell5_2 = new Cell();
cell5_2.addElement(cellContent5_2);
cell5_2.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
cell5_2.setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.addCell(cell5_2);
Phrase cellContent5_3 = new Phrase(“价格监测信息发布条数”,contentFont);
Cell cell5_3 = new Cell();
cell5_3.addElement(cellContent5_3);
cell5_3.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
cell5_3.setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.addCell(cell5_3);
Phrase cellContent5_4 = new Phrase(“”,contentFont);
Cell cell5_4 = new Cell();
cell5_4.addElement(cellContent5_4);
cell5_4.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
cell5_4.setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.addCell(cell5_4);
Phrase cellContent6_1 = new Phrase(“被省(区、市)和大中城市党委、政府采用信息标题”,contentFont);
Cell cell6_1 = new Cell();
cell6_1.addElement(cellContent6_1);
cell6_1.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
cell6_1.setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.addCell(cell6_1);
Phrase cellContent6_2 = new Phrase(“”,contentFont);
Cell cell6_2 = new Cell();
cell6_2.setColspan(3);
cell6_2.addElement(cellContent6_2);
cell6_2.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
cell6_2.setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.addCell(cell6_2);
Phrase cellContent7_1 = new Phrase(“发布的价格监测信息标题和发布媒体”,contentFont);
Cell cell7_1 = new Cell();
cell7_1.addElement(cellContent7_1);
cell7_1.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
cell7_1.setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.addCell(cell7_1);
Phrase cellContent7_2 = new Phrase(“”,contentFont);
Cell cell7_2 = new Cell();
cell7_2.setColspan(3);
cell7_2.addElement(cellContent7_2);
cell7_2.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
cell7_2.setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.addCell(cell7_2);
Phrase responsible = new Phrase(“负责人(签字):”,contentFont);
Cell cell8_1 = new Cell();
cell8_1.setColspan(2);
cell8_1.addElement(responsible);
cell8_1.setBorder(0);
table.addCell(cell8_1);
Phrase fill = new Phrase(“填报人(签字):”,contentFont);
Cell cell8_2 = new Cell();
cell8_2.setColspan(2);
cell8_2.addElement(fill);
cell8_2.setBorder(0);
table.addCell(cell8_2);
document.add(table);//将表格添加到word中
Paragraph Explain = new Paragraph(“填报说明:”,contentFont);
Explain.setAlignment(0);
// Explain.setSpacingBefore(5F);//距离表格的间距
document.add(Explain);
Paragraph content1 = new Paragraph(“1.所附增减监测点的信息包括:监测点名称、地址,负责人及采报价人员姓名、联系电话,所属价格监测报告制度等。”,contentFont);
content1.setFirstLineIndent(30f);//首行缩进
document.add(content1);
Paragraph content2 = new Paragraph(“2.得到省(区、市)和大中城市党委、政府领导批示信息应传真上报批示页或佐证材料。”,contentFont);
content2.setFirstLineIndent(30f);//首行缩进
document.add(content2);
Paragraph content3 = new Paragraph(“3.本表为季报,分别于1月25日、4月25日、7月25日、10月25日前上报。内容较多时,可另附页。”,contentFont);
content3.setFirstLineIndent(30f);//首行缩进
document.add(content3);
document.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

猜你喜欢

转载自blog.csdn.net/qq_38795959/article/details/79241862