java 生成简单word(利用Itext工具),生成简单Excel,以及下载笔记
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,以及下载笔记的更多相关文章
- 利用代码生成工具生成基于ABP框架的代码
在前面随笔,我介绍了整个ABP优化过框架的分层模型,包括尽量简化整个ABP框架的各个层的关系,以及纳入一些基类的辅助处理,使得我们对应业务分层类或者接口尽可能减少代码,并具有生产环境所需要的基类接口, ...
- 利用wsdl2java工具生成webservice的客户端代码
1.JDK环境 2.下载apache-cxf发布包:http://cxf.apache.org/download.html 目前最新版本为3.2.6, 解压后如下: 解压发布包,设置CXF_HOME ...
- 利用keytool工具生成数字证书
一.制作数字证书 因测试微信小程序, 腾讯要求使用 https协议,所以需要使用证书.使用jdk工具制作数字证书流程如下: 1.查看JDK是否安装,使用命令java -version 2.切换目录至 ...
- WebService -- Java 实现之 CXF ( 使用CXF工具生成client 程序)
1. 下载CXF 工具解压到磁盘 2.添加工具bin目录到PATH环境变量 3.创建一个CXF client新项目 4. run -> cmd 到指定目录,并运行工具目录下的批处理 “wadl2 ...
- 利用git工具命令简单的从github上拷贝和上传代码
第一:从github上拷贝项目到本地 1.在github上建立一个项目名为:MygitTest 2.在我们本地电脑上把这个项目拷贝下来:直接选择一个文件夹,右键选择git Bash here 直接 ...
- Java并发之Semaphore和Exchanger工具类简单介绍
一.Semaphore介绍 Semaphore意思为信号量,是用来控制同时访问特定资源的线程数数量.它的本质上其实也是一个共享锁.Semaphore可以用于做流量控制,特别是公用资源有限的应用场景.例 ...
- 利用代码生成工具Database2Sharp生成ABP VNext框架项目代码
我们在做某件事情的时候,一般需要详细了解它的特点,以及内在的逻辑关系,一旦我们详细了解了整个事物后,就可以通过一些辅助手段来提高我们的做事情的效率了.本篇随笔介绍ABP VNext框架各分层项目的规则 ...
- Java 动态实现word导出功能
1.word模板:xx.ftl生成,ftl文件就是word的源代码,类似html一样是拥有标签和样式的代码. 把需要导出的doc文件模板用office版本的word工具打开. 把doc文件另存为xx. ...
- Web API应用架构在Winform混合框架中的应用(4)--利用代码生成工具快速开发整套应用
前面几篇介绍了Web API的基础信息,以及如何基于混合框架的方式在WInform界面里面整合了Web API的接入方式,虽然我们看似调用过程比较复杂,但是基于整个框架的支持和考虑,我们提供了代码生成 ...
随机推荐
- 【luogu P3128 [USACO15DEC]最大流Max Flow】 题解
题目链接:https://www.luogu.org/problemnew/show/P3128 菜 #include <cstdio> #include <cstring> ...
- UVA - 136 Ugly Numbers(丑数,STL优先队列+set)
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9 ...
- 分享一个关于pthread线程栈在mm_struct里面的分布问题
大家好,本人被下面这个问题困扰了一段时间,最近似乎找到了答案. 这里和大家分享一下,可能对有相同困惑的同学有点帮助,同时也请各位帮忙看看错漏的地方. 1================问题: 在使用p ...
- 匿名union
#include <stdio.h> enum node_type{ t_int,t_double}; struct node{ enum node_type type; ...
- detection工作
今天看到YOLO2的工作还是很不错的,效果好,关键是速度也快,已经完胜SSD了感觉. 虽然faster rcnn各方面效果都不错,但是从简单粗暴的角度考虑,SSD和YOLO真的深得我心啊. 检测模型, ...
- c语言描述的双向链表的基本操作
#include<stdio.h> #include<stdlib.h> #define ok 1 #define error 0 typedef int Status; ty ...
- Redis分布式锁的正确实现方式(Java版)
前言 分布式锁一般有三种实现方式:1. 数据库乐观锁:2. 基于Redis的分布式锁:3. 基于ZooKeeper的分布式锁.本篇博客将介绍第二种方式,基于Redis实现分布式锁.虽然网上已经有各种介 ...
- Python常用模块之re
1.正则表达式规则 2.Python正则常用模块 2.1.re.match与re.search 函数说明:re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match ...
- ABAP术语-Business Framework Architecture
Business Framework Architecture 原文:http://www.cnblogs.com/qiangsheng/archive/2007/12/29/1019277.html ...
- Centos在虚拟机VMware12上的安装
欢迎评论和更正 虚拟机12的安装 看教程https://blog.csdn.net/yhj19920417/article/details/72891766 centos6.5镜像下载(选minima ...