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. PAT1064. Complete Binary Search Tree

    1064. Complete Binary Search Tree 题目大意 给定一个序列, 求其 生成Complete BST 的层序遍历. 思路 最开始把这个题想复杂了, 还想着建立结构体, 其实 ...

  2. 2018年暑假ACM个人训练题7 题解报告

    A:HDU 1060 Leftmost Digit(求N^N的第一位数字 log10的巧妙使用) B:(还需要研究一下.....) C:HDU 1071 The area(求三个点确定的抛物线的面积, ...

  3. 菜鸟笔记 -- Chapter 6.4.3 多态

    6.4.3  多态 多态就是指程序中定义的引用变量所指向的具体类型和通过该引用变量发出的方法调用在编程时并不确定,而是在程序运行期间才确定,即一个引用变量倒底会指向哪个类的实例对象,该引用变量发出的方 ...

  4. 分组函数group by和Oracle中分析函数partition by的用法以及区别

    1.分组函数group by和Oracle中分析函数partition by的用法以及区别 2.开窗函数.

  5. Redis面向java的Client

    一.概念简介: Redis: Redis是一款开源的Key-Value数据库,运行在内存中,由ANSI C编写,详细的信息在Redis官网上面有,因为我自己通过google等各种渠道去学习Redis, ...

  6. Linux基础 ppt pptx

    引言 以前写过一个讲 Linux 基础的ppt,琢磨着把它分享出来,有需要的请自取. 部分截图如下 下载地址 下载地址1

  7. (转)redis是什么

    1. 什么是Redis Redis是由意大利人Salvatore Sanfilippo(网名:antirez)开发的一款内存高速缓存数据库.Redis全称为:Remote Dictionary Ser ...

  8. 记js里codePointAt()方法返回的结果的含义。

    经过<字符串的扩展>和<字符编码的那些事>这两篇文章的阅读,大概了解js里codePointAt方法返回结果的含义. var str='

  9. Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO

    你有碰上过这样的提示吗? Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in t ...

  10. js bom和dom

    一, 前言 到目前为止,我们已经学过了JavaScript的一些简单的语法.但是这些简单的语法,并没有和浏览器有任何交互. 也就是我们还不能制作一些我们经常看到的网页的一些交互,我们需要继续学习BOM ...