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. 【luogu P3388 割点(割顶)】 模板

    题目链接:https://www.luogu.org/problemnew/show/P3388 #include <cstdio> #include <cstring> #i ...

  2. BP神经网络—java实现(转载)

    神经网络的结构 神经网络的网络结构由输入层,隐含层,输出层组成.隐含层的个数+输出层的个数=神经网络的层数,也就是说神经网络的层数不包括输入层.下面是一个三层的神经网络,包含了两层隐含层,一个输出层. ...

  3. 互联网高级Java面试总结

    前不久刚换了单位,这段时间抽出时间来总结一下. 本人渣本毕业四年,无大厂工作经验,出来面高级Java. 上家单位是一个知名互联网平台,但是体量不大的小公司(5线互联网公司),但就是出名(职场人都知道~ ...

  4. android 省市区三级联动

    最近项目,需要用到三级联动,在网上找了一些例子,进行了修改,实现,提炼出来了给大家分享 实现思路是在三个wheelview 进行联动.选择了省,马上就关联到市和区,选择了市 ,马上就可以关联到区. 效 ...

  5. 重写equals方法(未完)

    equals方法是我们日常编程中很常见的方法,Object中对这个方法的解释如下: boolean equals(Object obj) 指示其他某个对象是否与此对象“相等”. 查看该方法的底层代码如 ...

  6. oracle官网下载教程

    1.百度搜索oracle   也可以直接点击进入   oracle官网   或直接进入   下载页面 2.选择中文,看的更容易些 3.拉到最下面,选择所有下载和试用 4.选择数据库下载 5.点击下载对 ...

  7. 不再手写import - VSCode自动引入Vue组件和Js模块

    :first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdow ...

  8. [JSOI2010]Group 部落划分 Group

    Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 3661  Solved: 1755[Submit][Status][Discuss] Descripti ...

  9. 修改zabbix字体格式

    环境: centos7 zabbix3.2 1.获取喜欢的字体格式文件(喜欢别的字体也可以去网上下载) 通常都是ttf格式,可直接在windows下获取C:\Windows\Fonts 2.配置zab ...

  10. 让微信内置浏览器兼容clipboard.js 复制粘贴 ios 安卓

    <!--js copy事件--><script type="text/javascript" src="/static/js/clipboard.min ...