1.java 生成简单word(包含图片表格)

pom中加入itext 相关依赖

<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency> <dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext-rtf</artifactId>
<version>2.1.7</version>
</dependency>

2.java相关代码(数据与图片可以忽略)

/**
* 新建报告(报告名字。图片。新闻id)
*
* @throws JSONException
*/
@Override
public String addReport(String[] img, String[] newsId, String reportName, String loginId, String path,
String imgPath) { Long name = System.currentTimeMillis();
String filePath = path + File.separator + name + ".doc";
File file = new File(filePath); Report report = new Report(); report.setLoginId(loginId);
report.setReportName(reportName);
report.setDownUrl(filePath); fileDao.addReport(report); System.out.println("插入报告成功" + report.getReportNo());
    
Document document = new Document(PageSize.A4); try {
RtfWriter2.getInstance(document, new FileOutputStream(file));
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
document.open();
BaseFont bfChinese = null;
try {
bfChinese = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
} catch (DocumentException | IOException e1) {
e1.printStackTrace();
}
Font titleFont = new Font(bfChinese, 22, Font.BOLD);
Font contextFont = new Font(bfChinese, 10, Font.NORMAL);
Paragraph title = new Paragraph("统计报告", titleFont);
title.setAlignment(Element.ALIGN_CENTER); // 红色的线
RtfShapePosition position;
position = new RtfShapePosition(150, 0, 10400, 170);
position.setXRelativePos(RtfShapePosition.POSITION_X_RELATIVE_MARGIN);
position.setYRelativePos(RtfShapePosition.POSITION_Y_RELATIVE_PARAGRAPH);
RtfShape shape = new RtfShape(RtfShape.SHAPE_RECTANGLE, position);
RtfShapeProperty property = new RtfShapeProperty(RtfShapeProperty.PROPERTY_LINE_COLOR, Color.RED);
shape.setProperty(property); Paragraph p1 = new Paragraph(shape);
try {
// logo
Image imglogo = Image.getInstance(imgPath + File.separator + "dfgx.jpg");
imglogo.setAlignment(Image.LEFT);// 设置图片显示位置
imglogo.scaleAbsolute(80, 50);// 直接设定显示尺寸
document.add(imglogo);
document.add(title);
document.add(p1);
} catch (Exception e) {
e.printStackTrace();
} Table table = null;
try {
table = new Table(4);
int width[] = { 40, 30, 15, 15 };// 设置每列宽度比例
table.setWidths(width);
} catch (Exception e1) {
e1.printStackTrace();
} table.setBorderWidth(1);
table.setBorderColor(Color.BLACK);
table.setPadding(0);
table.setSpacing(0);
try {
table.addCell("标题");
table.addCell("链接");
table.addCell("渠道");
table.addCell("时间");
} catch (BadElementException e1) {
e1.printStackTrace();
}
List<Object> list = estoWeb.SelectId(newsId);
String time = "";
String titleinfo = "";
String url = "";
String channel = "";
try {
for (Object string : list) {
JSONObject json = null;
json = new JSONObject(String.valueOf(string));
channel = (String) json.get("webCell");
titleinfo = (String) json.get("titleCell");
url = (String) json.get("linkCell");
time = (String) json.get("dateCell");
table.addCell(new Paragraph(titleinfo));
table.addCell(new Paragraph(url));
table.addCell(new Paragraph(channel));
table.addCell(new Paragraph(time)); }
} catch (Exception e) {
e.printStackTrace();
} try {
document.add(table);
} catch (DocumentException e1) {
e1.printStackTrace();
} for (String string : img) {
Map<String, Object> map = fileDao.findReportPath(string);
System.out.println(map.get("PIC_PATH"));
Image img1 = null;
try {
img1 = Image.getInstance(imgPath + File.separator + String.valueOf(map.get("PIC_PATH")));
} catch (BadElementException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
img1.setAbsolutePosition(0, 0);
img1.setAlignment(Image.ALIGN_CENTER);// 设置图片显示位置
img1.scaleAbsolute(360, 180);// 直接设定显示尺寸
try {
document.add(img1);
} catch (DocumentException e) {
e.printStackTrace();
}
} document.close(); return "ok";
}

3.生成简单Excel(参考 :http://blog.csdn.net/yongqingmiao/article/details/7179452)

pom加入相关依赖

<dependency>
<groupId>jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6</version>
</dependency>

4.java代码

File file;
WritableWorkbook wwb;
try {
file = new File(imgpath + File.separator + "news.xls");
wwb = Workbook.createWorkbook(file);
WritableSheet ws = wwb.createSheet("页1", 0);
ws.addCell(new Label(0, 0, "时间"));
ws.addCell(new Label(1, 0, "标题"));
ws.addCell(new Label(2, 0, "链接"));
ws.addCell(new Label(3, 0, "渠道"));
List<Object> list = estoWeb.SelectId(newsId);
String time = "";
String titleinfo = "";
String url = "";
String channel = "";
try {
for (int i = 0; i < list.size(); i++) {
JSONObject json = null;
json = new JSONObject(String.valueOf(list.get(i)));
channel = (String) json.get("webCell");
titleinfo = (String) json.get("titleCell");
url = (String) json.get("linkCell");
time = (String) json.get("dateCell"); ws.addCell(new Label(0, i + 1, time));
ws.addCell(new Label(1, i + 1, titleinfo));
ws.addCell(new Label(2, i + 1, url));
ws.addCell(new Label(3, i + 1, channel)); } wwb.write();
wwb.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}

5.下载demo(相关文件名参数参考)

InputStream inputStream = null;
OutputStream os = null; res.setCharacterEncoding("utf-8");
res.setContentType("multipart/form-data");
res.setHeader("Content-Disposition", "attachment;filename=\"" + "news.xls" + "\""); try {
// 下载文件
inputStream = new FileInputStream(new File(imgpath + File.separator + "news.xls"));
os = res.getOutputStream(); byte[] b = new byte[4096];
int length;
while ((length = inputStream.read(b)) > 0) {
os.write(b, 0, length);
}
} catch (Exception e) {
e.printStackTrace();
return "false";
} finally {
try {
os.flush();
os.close();
inputStream.close();
if (new File(imgpath + File.separator + "news.xls").exists()) {
new File(imgpath + File.separator + "news.xls").delete();
}
} catch (Exception e) {
e.printStackTrace();
return "false";
} }

java 生成简单word(利用Itext工具),生成简单Excel,以及下载笔记的更多相关文章

  1. 利用代码生成工具生成基于ABP框架的代码

    在前面随笔,我介绍了整个ABP优化过框架的分层模型,包括尽量简化整个ABP框架的各个层的关系,以及纳入一些基类的辅助处理,使得我们对应业务分层类或者接口尽可能减少代码,并具有生产环境所需要的基类接口, ...

  2. 利用wsdl2java工具生成webservice的客户端代码

    1.JDK环境  2.下载apache-cxf发布包:http://cxf.apache.org/download.html 目前最新版本为3.2.6, 解压后如下: 解压发布包,设置CXF_HOME ...

  3. 利用keytool工具生成数字证书

    一.制作数字证书  因测试微信小程序, 腾讯要求使用 https协议,所以需要使用证书.使用jdk工具制作数字证书流程如下: 1.查看JDK是否安装,使用命令java -version 2.切换目录至 ...

  4. WebService -- Java 实现之 CXF ( 使用CXF工具生成client 程序)

    1. 下载CXF 工具解压到磁盘 2.添加工具bin目录到PATH环境变量 3.创建一个CXF client新项目 4. run -> cmd 到指定目录,并运行工具目录下的批处理 “wadl2 ...

  5. 利用git工具命令简单的从github上拷贝和上传代码

    第一:从github上拷贝项目到本地   1.在github上建立一个项目名为:MygitTest 2.在我们本地电脑上把这个项目拷贝下来:直接选择一个文件夹,右键选择git Bash here 直接 ...

  6. Java并发之Semaphore和Exchanger工具类简单介绍

    一.Semaphore介绍 Semaphore意思为信号量,是用来控制同时访问特定资源的线程数数量.它的本质上其实也是一个共享锁.Semaphore可以用于做流量控制,特别是公用资源有限的应用场景.例 ...

  7. 利用代码生成工具Database2Sharp生成ABP VNext框架项目代码

    我们在做某件事情的时候,一般需要详细了解它的特点,以及内在的逻辑关系,一旦我们详细了解了整个事物后,就可以通过一些辅助手段来提高我们的做事情的效率了.本篇随笔介绍ABP VNext框架各分层项目的规则 ...

  8. Java 动态实现word导出功能

    1.word模板:xx.ftl生成,ftl文件就是word的源代码,类似html一样是拥有标签和样式的代码. 把需要导出的doc文件模板用office版本的word工具打开. 把doc文件另存为xx. ...

  9. Web API应用架构在Winform混合框架中的应用(4)--利用代码生成工具快速开发整套应用

    前面几篇介绍了Web API的基础信息,以及如何基于混合框架的方式在WInform界面里面整合了Web API的接入方式,虽然我们看似调用过程比较复杂,但是基于整个框架的支持和考虑,我们提供了代码生成 ...

随机推荐

  1. 【题解】洛谷P1313 [NOIP2011TG]计算系数(组合+二次项展开)

    洛谷P1313:https://www.luogu.org/problemnew/show/P1313 思路 本题就是考查二次项展开 根据定理有:(ax+by)k=∑ki=0Cik*aibk-ixiy ...

  2. 前端html,css基础总结

    0.1.css引入界面的方式: 内联式:通过标签的style属性,在标签上直接写样式. <div style="width:100px; height:100px; backgroun ...

  3. GET&&POST请求编码过程

    编码.解码 我们在开发过程中不可避免的一个话题就是编码和解码,那么什么是编码什么是解码呢?为什么要进行编码和解码呢?下面我们一一分析! 编码和解码的概念 编码是信息从一种形式或格式转换为另一种形式的过 ...

  4. flexible.js在华某为手机上使用rem时,页面宽度超出手机屏幕宽度

    问题:手机端项目在华为的某款手机上显示时页面内容没有自适应手机宽度,出现横向滚动条 原因:手机获取手机屏幕宽度并计算出rem时出现偏差,明显宽余真实手机屏宽度 解决方案一:在页面里获取页面最外层dom ...

  5. 【TOJ 3305】Hero In Maze II

    描述 500年前,Jesse是我国最卓越的剑客.他英俊潇洒,而且机智过人^_^.突然有一天,Jesse心爱的公主被魔王困在了一个巨大的迷宫中.Jesse听说这个消息已经是两天以后了,他急忙赶到迷宫,开 ...

  6. linux系统之-vi编辑器

    在linux系统使用中,掌握熟练的vi编辑器,可以提高linux工作效率.那么vi编辑器的使用方法有哪些呢? vi编辑器可在绝大部分linux发行版中使用. Vi编辑器的作用:创建或修改文件:维护li ...

  7. js继承的几种方法和es6继承方法

        一.原型链继     1.基本思想     利用原型链来实现继承,超类的一个实例作为子类的原型     2.具体实现     function F() {}     //原型属性,原型方法: ...

  8. php 无限参数方法

    在很多项目开发中经常会用到共用方法但是参数不固定,每个参数都创建一遍阅读性不好,后期维护也麻烦,PHP有获取传入参数的方法,记录参考一下.这里有两个方法 <?php 方法一: #不指定参数个数方 ...

  9. Scala语法(二)

    (1)类,对象 //定义类(属性.方法),实例化对象 class counter{ *//主构造器 class counter(name:String,mode:Int){ ... } 实例化:val ...

  10. 网络编程之socket的运用

    一,socket用法 socket是什么 ? Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐 ...