iText-1.3.1.jar iTextAsian-1.3.1.jar
这里是最简单的Word操作。基本没有任何样式,都是字符串的拼接
代码如下:
public void exportYdzycwzbkb_Word(OutputStream out,String dateTime) throws DocumentException, IOException{
Document document = new Document(PageSize.A4.rotate());
// 日期格式转换,目的是去零,例如2001-07,转为2001-7
String dateT = null;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
try {
dateT = new SimpleDateFormat("yyyy-M").format(format.parse(dateTime));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 报告月份
String month = dateT.substring(dateT.indexOf("-")+1);
List<Map<String,Object>> data = handlerYdzycwzbkb_Word_data(dateTime);
//合计数据
Map<String, Object> total = totalYdzycwzbkb_Word(dateTime);
// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
RtfWriter2.getInstance(document,out);
document.open();
// 设置中文字体
// BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
// "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// BaseFont bfChinese = BaseFont.createFont("C:\\WINDOWS\\Fonts\\SIMSUN.TTC,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
BaseFont bfChinese = BaseFont.createFont("/font/STKAITI.TTF",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// 标题字体风格
Font titleFont = new Font(bfChinese, 26, Font.BOLD);
//标签字体样式
Font labelFont = new Font(bfChinese,15,Font.BOLD);
// 正文字体风格
Font contextFont = new Font(bfChinese, 14, Font.NORMAL);
Font contextBoldFont = new Font(bfChinese, 14, Font.BOLD);
// 设置标题格式对齐方式
Paragraph title = new Paragraph("江苏中烟工业系统月度快报说明",titleFont);
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
// 设置日期
Paragraph title2 = new Paragraph("("+number2Chinese(dateTime)+")",new Font(bfChinese,16,Font.BOLD));
title2.setAlignment(Element.ALIGN_CENTER);
title2.setLeading(30);
document.add(title2);
//标签1
Paragraph label = new Paragraph(" 一、全系统实现税利情况",labelFont);
label.setLeading(30);
label.setAlignment(Element.ALIGN_LEFT);
document.add(label);
Chunk _label = new Chunk(" 工业公司本级:",contextBoldFont);
Chunk _context = new Chunk(month+"月份实现税利"+fmtBillion(data.get(0).get("BY"))+"亿元,同比"+fmtBillion(data.get(0).get("BY_TQ"))+"亿元"+judgeAddPer(data.get(0).get("BYZF"))+fmtPercent(data.get(0).get("BYZF")).replace("-", "")+"," +
"其中实现利润"+fmtBillion(data.get(1).get("BY"))+"亿元,同比"+fmtBillion(data.get(1).get("BY_TQ"))+"亿元"+judgeAddPer(data.get(1).get("BYZF"))+fmtPercent(data.get(1).get("BYZF")).replace("-", "")+"。",contextFont);
Chunk _scope = new Chunk("");
if(!"1".equals(dateT.substring(dateT.indexOf("-")+1))){
_scope = new Chunk("1-"+month+"月份累计实现税利"+fmtBillion(total.get("SHUILIBY"))+"亿元, 同比"+fmtBillion(total.get("SHUILIBYTQ"))+"亿元"+judgeAddPer(total.get("SHUILIBYZF"))+fmtPercent(total.get("SHUILIBYZF")).replace("-", "")+"," +
"其中累计实现利润"+fmtBillion(total.get("LIRUNBY"))+"亿元,同比"+fmtBillion(total.get("LIRUNBYTQ"))+"亿元"+judgeAddPer(total.get("LIRUNBYZF"))+fmtPercent(total.get("LIRUNBYZF")).replace("-", "")+"。",contextFont);
}
Paragraph _l_c = new Paragraph();
_l_c.setLeading(30);
_l_c.add(_label);
_l_c.add(_context);
_l_c.add(_scope);
document.add(_l_c);
_label = new Chunk(" 全系统:",contextBoldFont);
_context = new Chunk(month+"月份实现税利XXX亿元,同比XXX亿元增长XXX%,其中实现利润XXX亿元,同比XXX亿元增长XXX%。1-"+month+"月份累计实现税利XXX亿元," +
"同比XXX亿元增长XXX%,其中实现利润XXX亿元,同比XXX亿元增长XXX%。",contextFont);
_l_c = new Paragraph();
_l_c.setLeading(30);
_l_c.add(_label);
_l_c.add(_context);
document.add(_l_c);
//标签1
label = new Paragraph(" 二、卷烟工业企业产量、销量及实现税利情况",labelFont);
label.setLeading(30);
label.setAlignment(Element.ALIGN_LEFT);
document.add(label);
Chunk chun = null;
chun = new Chunk(" 1、"+month+"月份生产卷烟"+fmtMillionBox(data.get(4).get("BY"))+"万箱,同比"+fmtMillionBox(data.get(4).get("BY_TQ"))+"万箱"+judgeAdd(Double.parseDouble(data.get(4).get("BY").toString())-Double.parseDouble(data.get(4).get("BY_TQ").toString()))+fmtMillionBox(Math.abs(Double.parseDouble(data.get(4).get("BY").toString())-Double.parseDouble(data.get(4).get("BY_TQ").toString())))+"万箱,"+judgeAddPer(data.get(4).get("BYZF"))+fmtPercent(data.get(4).get("BYZF")).replace("-", "")+"。",contextFont);
_scope = new Chunk("");
if(!"1".equals(dateT.substring(dateT.indexOf("-")+1))){
_scope = new Chunk("1-"+month+"月份累计生产卷烟"+fmtMillionBox(total.get("SCJYBY"))+"万箱,同比"+fmtMillionBox(total.get("SCJYBYTQ"))+"万箱"+judgeAdd(fmtMillionBox(Double.parseDouble(total.get("SCJYBY").toString())-Double.parseDouble(total.get("SCJYBYTQ").toString())))+fmtMillionBox(Math.abs(Double.parseDouble(total.get("SCJYBY").toString())-Double.parseDouble(total.get("SCJYBYTQ").toString())))+"万箱,"+judgeAddPer(total.get("SCJYBYZF"))+fmtPercent(total.get("SCJYBYZF"))+"。",contextFont);
}
_l_c = new Paragraph();
_l_c.add(chun);
_l_c.add(_scope);
_l_c.setLeading(30);
document.add(_l_c);
chun = new Chunk(" 2、"+month+"月份自产卷烟销售"+fmtMillionBox(data.get(5).get("BY"))+"万箱,同比"+fmtMillionBox(data.get(5).get("BY_TQ"))+"万箱"+judgeAdd(Double.parseDouble(data.get(5).get("BY").toString())-Double.parseDouble(data.get(5).get("BY_TQ").toString()))+fmtMillionBox(Math.abs(Double.parseDouble(data.get(5).get("BY").toString())-Double.parseDouble(data.get(5).get("BY_TQ").toString())))+"万箱,"+judgeAddPer(data.get(5).get("BY_TQ"))+fmtPercent(data.get(5).get("BYZF")).replace("-", "")+"。",contextFont);
_scope = new Chunk("");
if(!"1".equals(dateT.substring(dateT.indexOf("-")+1))){
_scope = new Chunk("1-"+month+"月份累计自产卷烟销售"+fmtMillionBox(total.get("ZCJYXSBY"))+"万箱,同比"+fmtMillionBox(total.get("ZCJYXSBYTQ"))+"万箱"+judgeAdd(fmtMillionBox(Double.parseDouble(total.get("ZCJYXSBY").toString())-Double.parseDouble(total.get("ZCJYXSBYTQ").toString())))+fmtMillionBox(Math.abs(Double.parseDouble(total.get("ZCJYXSBY").toString())-Double.parseDouble(total.get("ZCJYXSBYTQ").toString())))+"万箱,"+judgeAddPer(total.get("ZCJYXSBYZF"))+fmtPercent(total.get("ZCJYXSBYZF"))+"。",contextFont);
}
_l_c = new Paragraph();
_l_c.add(chun);
_l_c.add(_scope);
_l_c.setLeading(30);
document.add(_l_c);
chun = new Chunk(" 3、"+month+"月份自产卷烟销售额"+fmtBillion(data.get(6).get("BY"))+"亿元,同比"+fmtBillion(data.get(6).get("BY_TQ"))+"亿元"+judgeAddPer(data.get(6).get("BYZF"))+fmtPercent(data.get(6).get("BYZF")).replace("-", "")+"。",contextFont);
_scope = new Chunk("");
if(!"1".equals(dateT.substring(dateT.indexOf("-")+1))){
_scope = new Chunk("1-"+month+"月份累计自产卷烟销售额"+fmtBillion(total.get("ZCJYXSEBY"))+"亿元,同比"+fmtBillion(total.get("ZCJYXSEBYTQ"))+"亿元"+judgeAddPer(total.get("ZCJYXSEBYZF"))+fmtPercent(total.get("ZCJYXSEBYZF"))+"。",contextFont);
}
_l_c = new Paragraph();
_l_c.add(chun);
_l_c.add(_scope);
_l_c.setLeading(30);
document.add(_l_c);
chun = new Chunk(" 4、"+month+"月份自产卷烟销售成本"+fmtBillion(data.get(7).get("BY"))+"亿元,同比"+fmtBillion(data.get(7).get("BY_TQ"))+"亿元"+judgeAddPer(data.get(7).get("BYZF"))+fmtPercent(data.get(7).get("BYZF")).replace("-", "")+"。",contextFont);
_scope = new Chunk("");
if(!"1".equals(dateT.substring(dateT.indexOf("-")+1))){
_scope = new Chunk("1-"+month+"月份累计自产卷烟销售成本"+fmtBillion(total.get("ZCJYXSCBBY"))+"亿元,同比"+fmtBillion(total.get("ZCJYXSCBBYTQ"))+"亿元"+judgeAddPer(total.get("ZCJYXSCBBYZF"))+fmtPercent(total.get("ZCJYXSCBBYZF"))+"。",contextFont);
}
_l_c = new Paragraph();
_l_c.add(chun);
_l_c.add(_scope);
_l_c.setLeading(30);
document.add(_l_c);
if(!"1".equals(dateT.substring(dateT.indexOf("-")+1))){
_l_c = new Paragraph(" 5、1-"+month+"月份合作生产卷烟累计销售XXX箱,同比XXX箱增加XXX箱,增长XXX%。",contextFont);
}else{
_l_c = new Paragraph(" 5、1月份合作生产卷烟累计销售XXX箱,同比XXX箱增加XXX箱,增长XXX%。",contextFont);
}
_l_c.setLeading(30);
document.add(_l_c);
if(!"1".equals(dateT.substring(dateT.indexOf("-")+1))){
_l_c = new Paragraph(" 6、1-"+month+"月份卷烟累计税利"+judgeAdd(Double.parseDouble(fmtBillion(total.get("SHUILIBY")))-Double.parseDouble(fmtBillion(total.get("SHUILIBYTQ"))))+fmtBillion(Math.abs(Double.parseDouble(total.get("SHUILIBY").toString())-Double.parseDouble(total.get("SHUILIBYTQ").toString())))+"亿元," +
"其中销量"+judgeAdd(Double.parseDouble(fmtMillionBox(total.get("ZCJYXSBY")))-Double.parseDouble(fmtMillionBox(total.get("ZCJYXSBYTQ"))))+fmtMillionBox(Math.abs(Double.parseDouble(total.get("ZCJYXSBY").toString())-Double.parseDouble(total.get("ZCJYXSBYTQ").toString())))+"万箱," +
"增加税利XXX亿元,单箱税利结构提升XXX元,增加税利XXX亿元。",contextFont);
}else{
_l_c = new Paragraph(" 6、1月份卷烟累计税利增加17.93亿元,其中销量增加3.81万箱,增加税利XXX亿元,单箱税利结构提升XXX元,增加税利XXX亿元。",contextFont);
}
_l_c.setLeading(30);
document.add(_l_c);
label = new Paragraph(" 三、工业公司本级成本费用情况",labelFont);
label.setLeading(30);
label.setAlignment(Element.ALIGN_LEFT);
document.add(label);
_l_c = new Paragraph();
_l_c.setLeading(30);
if(!"1".equals(dateT.substring(dateT.indexOf("-")+1))){
_context = new Chunk(" 1-"+month+"月份累计期间费用"+total.get("LJQJFYBY")+"万元,同比"+total.get("LJQJFYBYTQ")+"万元"+judgeAddPer(total.get("LJQJFYBYZF"))+fmtPercent(total.get("LJQJFYBYZF")).replace("-", "")+"。其中:",contextFont);
}else{
_context = new Chunk(" 1月份累计期间费用"+data.get(9).get("BY")+"万元,同比"+data.get(9).get("BY_TQ")+"万元"+judgeAddPer(data.get(9).get("BYZF"))+fmtPercent(data.get(9).get("BYZF")).replace("-", "")+"。其中:",contextFont);
}
_label = new Chunk("营业费用",contextBoldFont);
_l_c.add(_context);
_l_c.add(_label);
_context = new Chunk(total.get("YYFYBY")+"万元,同比"+total.get("YYFYBYTQ")+"万元"+judgeAddPer(total.get("YYFYBYZF"))+fmtPercent(total.get("YYFYBYZF")).replace("-", "")+",",contextFont);
_label = new Chunk("管理费用",contextBoldFont);
_l_c.add(_context);
_l_c.add(_label);
_context = new Chunk(total.get("GLFYBY")+"万元,同比"+total.get("GLFYBYTQ")+"万元"+judgeAddPer(total.get("GLFYBYZF"))+fmtPercent(total.get("GLFYBYZF")).replace("-", "")+"。" +
"综合两大费用来看总体减少XXX万,下降XXX%。其中由于工资费用增长XXX万,实则其他费用的下降影响在XXX多万。重点费用的使用情况:其中宣传促销费下降XXX%,车辆运行费用下降XXX%,业务招待费下降XXX%,本月由于派驻阿根廷人员差费报销,涉外费用是上升的。",contextFont);
_l_c.add(_context);
document.add(_l_c);
document.close();
}
Action的代码:
/**
* <h1>月度主要财务指标快报_Word_导出</h1>
* @author ruanhao
* @return
*/
private String initYdzycwzbkbWord() {
String dateTime = request.getParameter("dateTime");
response.reset();
response.setContentType("application/msword");
response.setCharacterEncoding("GBK");
try {
response.setHeader("Content-Disposition", "attachment; filename=" + dateTime + new String("月度主要财务指标快报".getBytes("GBK"), "ISO-8859-1") +".doc");
OutputStream os = response.getOutputStream();
this.weekService.exportYdzycwzbkb_Word(os,dateTime);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}