java操作office和pdf文件页面列表导出cvs,excel、pdf报表.
在平常的开发中我们常常遇到不仅仅只是导出excel报表的情况。有时候也需要导出pdf或者CSV报 表。其实原理都差不多。刚开始本来不打算也这篇博客介绍这个的。感觉这篇博客和前面的博客有点雷同。原理基本都一样。但想了想。有时候可能有些童鞋遇到这 样的需求会无从下手。所以还是记录下来。帮助一下那些需要这个需求的童鞋。如果你对前面几篇博客的原理都搞明白了。这篇博客你完全可以不看了。仅仅只是代 码的实现不同而已。好了。下面我们来看一下需求吧。
这个图就是我们的需求
就像你看到的一样。我们的需求就是列表内容是从数据库中读出来的。而我们想把从数据库得到的这个列表导出pdf、csv、excel报表。也不多说了。看代码吧:

1 package com.bzu.csh;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.File;
5 import java.io.FileOutputStream;
6 import java.io.OutputStream;
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12
13 import jxl.Workbook;
14 import jxl.write.Label;
15 import jxl.write.WritableFont;
16 import jxl.write.WritableSheet;
17 import jxl.write.WritableWorkbook;
18
19 import org.apache.struts2.ServletActionContext;
20
21 import com.lowagie.text.Document;
22 import com.lowagie.text.Element;
23 import com.lowagie.text.Font;
24 import com.lowagie.text.PageSize;
25 import com.lowagie.text.Paragraph;
26 import com.lowagie.text.pdf.PdfPTable;
27 import com.lowagie.text.pdf.PdfWriter;
28 import com.opensymphony.xwork2.Action;
29
30 public class downloadAction implements Action {
31
32 private String downType;
33
34 public String getDownType() {
35 return downType;
36 }
37
38 public void setDownType(String downType) {
39 this.downType = downType;
40 }
41
42 public String execute() {
43 // TODO Auto-generated method stub
44 HttpServletRequest request = ServletActionContext.getRequest();
45 //HttpServletResponse response = ServletActionContext.getResponse();
46 //此处模拟数据库读出的数据。在真正的项目中。我们可以通过在session中保存的前端数据集合替换这里
47 List<Person> list = new ArrayList<Person>();
48 for (int i = 1; i < 6; i++) {
49 Person person = new Person();
50 person.setId(String.valueOf(i));
51 person.setName(String.valueOf((char) (i + 64)));
52 person.setAge(i + 20);
53 person.setSex("man");
54 list.add(person);
55 }
56 OutputStream os = null;
57 String fname = "personlist";
58 if ("PDF".equals(downType)) {
59 try {
60 // response.reset();
61 // os = response.getOutputStream();
62 FileOutputStream out = new FileOutputStream("d://a.pdf");
63 Document document = new Document(PageSize.A4, 50, 50, 50, 50);
64 // response.setContentType("application/pdf");
65 // response.setHeader("Content-disposition",
66 // "attachment;filename=" + fname + ".pdf");
67 ByteArrayOutputStream baos = new ByteArrayOutputStream();
68
69 PdfWriter.getInstance(document, out);
70 document.open();
71 int cols = list.size();
72 // 创建PDF表格
73 PdfPTable table = new PdfPTable(4);
74 // 设置pdf表格的宽度
75 table.setTotalWidth(500);
76 // 设置是否要固定其宽度
77 table.setLockedWidth(true);
78 // 表头字体
79 Font thfont = new Font();
80 // 设置表头字体的大小
81 thfont.setSize(7);
82 // 设置表头字体的样式
83 thfont.setStyle(Font.BOLD);
84 Font tdfont = new Font();
85 tdfont.setSize(7);
86 tdfont.setStyle(Font.NORMAL);
87 // 设置水平对齐方式
88 table.setHorizontalAlignment(Element.ALIGN_MIDDLE);
89 // 设置table的header
90 table.addCell(new Paragraph("id", thfont));
91 table.addCell(new Paragraph("name", thfont));
92 table.addCell(new Paragraph("sex", thfont));
93 table.addCell(new Paragraph("age", thfont));
94 // 循环设置table的每一行
95 for (int i = 0; i < list.size(); i++) {
96 Person p = (Person) list.get(i);
97 table.addCell(new Paragraph(p.getId(), tdfont));
98 table.addCell(new Paragraph(p.getName(), tdfont));
99 table.addCell(new Paragraph(p.getSex(), tdfont));
100 table.addCell(new Paragraph(String.valueOf(p.getAge()),
101 tdfont));
102 }
103 document.add(table);
104 document.close();
105 // baos.writeTo(response.getOutputStream());
106 baos.close();
107 } catch (Exception e) {
108 e.printStackTrace();
109 }
110 } else if ("CSV".equals(downType)) {
111 // response.reset();
112 // 生成csv文件
113 //response.setHeader("Content-disposition", "attachment;filename="
114 // + fname + ".csv");
115 //response.setContentType("text/csv");
116 //response.setCharacterEncoding("UTF-8");
117 FileOutputStream out ;
118 String sep = ",";
119 try {
120 out = new FileOutputStream(new File("d://a.cvs"));
121 //out = response.getOutputStream();
122 out.write("id".getBytes());
123 out.write(sep.getBytes());
124 out.write("name".getBytes());
125 out.write(sep.getBytes());
126 out.write("sex".getBytes());
127 out.write(sep.getBytes());
128 out.write("age".getBytes());
129 out.write(sep.getBytes());
130 out.write(System.getProperty("line.separator").getBytes());
131 for (int i = 0; i < list.size(); i++) {
132 Person p = (Person) list.get(i);
133 out.write(p.getId().getBytes());
134 out.write((sep + "/t").getBytes());
135 out.write(p.getName().getBytes());
136 out.write((sep + "/t").getBytes());
137 out.write(p.getSex().getBytes());
138 out.write((sep + "/t").getBytes());
139 out.write(String.valueOf(p.getAge()).getBytes());
140 out.write((sep + "/t").getBytes());
141 out.write(sep.getBytes());
142 out.write(System.getProperty("line.separator").getBytes());
143 }
144 out.flush();
145 //out.cloute();
146 } catch (Exception e) {
147 e.printStackTrace();
148 }
149 } else if (downType.equals("Excel")) {
150 //response.reset();
151 // 生成xls文件
152 //response.setContentType("application/vnd.ms-excel");
153 //response.setHeader("Content-disposition", "attachment;filename="
154 // + fname + ".xls");
155 try {
156 //os = response.getOutputStream();
157 Label l = null;
158 WritableWorkbook wbook = Workbook.createWorkbook(new File(
159 "d://a.xls"));
160 // 写sheet名称
161 WritableSheet sheet = wbook.createSheet("my excel file", 0);
162 jxl.write.WritableFont wfc4 = new jxl.write.WritableFont(
163 WritableFont.ARIAL, 9, WritableFont.NO_BOLD, false,
164 jxl.format.UnderlineStyle.NO_UNDERLINE,
165 jxl.format.Colour.BLACK);
166 jxl.write.WritableCellFormat wcfFC4 = new jxl.write.WritableCellFormat(
167 wfc4);
168 wcfFC4.setBackground(jxl.format.Colour.LIGHT_GREEN);
169 int col = 0;
170 sheet.setColumnView(col, 12);
171 l = new Label(col, 0, "id", wcfFC4);
172 sheet.addCell(l);
173 col++;
174 sheet.setColumnView(col, 12);
175 l = new Label(col, 0, "name", wcfFC4);
176 sheet.addCell(l);
177 col++;
178 sheet.setColumnView(col, 12);
179 l = new Label(col, 0, "sex", wcfFC4);
180 sheet.addCell(l);
181 col++;
182 sheet.setColumnView(col, 12);
183 l = new Label(col, 0, "age", wcfFC4);
184 sheet.addCell(l);
185
186 // 设置字体样式
187 jxl.write.WritableFont wfc5 = new jxl.write.WritableFont(
188 WritableFont.ARIAL, 9, WritableFont.NO_BOLD, false,
189 jxl.format.UnderlineStyle.NO_UNDERLINE,
190 jxl.format.Colour.BLACK);
191 jxl.write.WritableCellFormat wcfFC5 = new jxl.write.WritableCellFormat(
192 wfc5);
193 for (int i = 0; i < list.size(); i++) {
194 Person p = (Person) list.get(i);
195 int j = 0;
196 l = new Label(j, i + 1, p.getId(), wcfFC5);
197 sheet.addCell(l);
198 j++;
199 l = new Label(j, i + 1, p.getName(), wcfFC5);
200 sheet.addCell(l);
201 j++;
202 l = new Label(j, i + 1, p.getSex(), wcfFC5);
203 sheet.addCell(l);
204 j++;
205 l = new Label(j, i + 1, String.valueOf(p.getAge()), wcfFC5);
206 sheet.addCell(l);
207 j++;
208 }
209 // 写入流中
210 wbook.write();
211 wbook.close();
212
213 } catch (Exception e) {
214 e.printStackTrace();
215 }
216 }
217 return SUCCESS;
218 }
219 }
220

操作很简单。选择好要导出的报表格式。点击导出按钮。我们在d盘相应的位置就会生成相应的报表。
java操作office和pdf文件页面列表导出cvs,excel、pdf报表.的更多相关文章
- 在线打开,浏览PDF文件的各种方式及各种pdf插件------(MS OneDrive/google drive & google doc/ github ?raw=true)
在线打开,浏览PDF文件的各种方式: 1 Google drive&doc (国内不好使,you know GFW=Great Firewall) 1. google drive: 直接分 ...
- PDF文件如何标注,怎么使用PDF标注工具
我们在使用文件的时候需要给文件的部分添加标注,能够更加直观的了解文件,但是有很多小伙伴们对于PDF文件怎么添加标注都不知道,也不知道PDF标注工具要怎么使用,那么下面就跟大家分享一下怎么使用PDF标注 ...
- python将字典列表导出为Excel文件的方法
将如下的字典列表内容导出为Excel表格文件形式: 关于上图字典列表的写入,请参考文章:https://blog.csdn.net/weixin_39082390/article/details/ ...
- java操作office和pdf文件java读取word,excel和pdf文档内容
在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...
- 如何在 网站页面中插入ppt/pdf 文件,使用插件,Native pdf 支持,chrome,Edge,Firefox,
1 经过测试:在网页中插入 ppt 不好使:可能是浏览器=>同源策略 error? pdf 可以正常使用: <前提:一定要放在服务器端才行!> 2 经过e ...
- pdf文件的导入导出
下面的代码自己本人没有尝试过,只是用来做记录,用到时候再说! 最近碰见个需求需要实现导出pdf文件,上网查了下代码资料总结了以下代码.可以成功的实现导出pdf文件. 在编码前需要在网上下载个itext ...
- 在网页中显示PDF文件及vue项目中弹出PDF
1.<embed width="800" height="600" src="test_pdf.pdf"> </embed ...
- 【Java EE 学习 17 下】【数据库导出到Excel】【多条件查询方法】
一.导出到Excel 1.使用DatabaseMetaData分析数据库的数据结构和相关信息. (1)测试得到所有数据库名: private static DataSource ds=DataSour ...
- .Net直接将Web页面table导出到Excel
项目管理系统有个统计表需要导出到Excel表中.常用的方法是在后台C#代码查询数据再写入Excel表中最后保存在目标路径. 为减轻数据库服务器的压力和保持页面的样式,能否直接将页面的表格直接导出到Ex ...
随机推荐
- Virtualbox网络设置和无UI启动
因工作需要,在Macbook上安装Ubuntu 14.04.2虚拟机,需要ssh连接操作. 一番查找资料,实践后可以正常工作了,记录一些信息以备用 无UI启动虚拟机,可使用以下命令: VBoxMana ...
- IOS Crash捕获
IOS Crash ,就两种情况:一种是异常,另一种是中断[信号量]. #include <libkern/OSAtomic.h> #include <execinfo.h> ...
- application/x-www-form-urlencoded multipart/form-data text/plain 的区别和作用
我们知道在通过POST方式向服务器发送AJAX请求时最好要通过设置请求头来指定为application/x-www-form-urlencoded编码类型.知道通过表单上传文件时必须指定编码类型为&q ...
- web访问速度优化分析
请求从发出到接收完成一共经历了DNS Lookup.Connecting.Blocking.Sending.Waiting和Receiving六个阶段,时间共计38ms.请求完成之后是DOM加载和页面 ...
- 解决iptables和vsftpd设置的问题
解决iptables和vsftpd设置的问题 博客分类: linux/centos/ubuntu 防火墙J#工作 解决iptables和vsftpd设置的问题 修改 vi /etc/sysconfig ...
- HDU4718 The LCIS on the Tree(LCT)
又是一枚LCT,写一发加深一下对LCT的理解.本题的坑爹之处就在于,它实在是太坑爹了.询问的是树路径上的最长连续上升的子串,考验的是怎么样去维护.一开始的想法是维护三个变量 ls,rs,mxl,分别表 ...
- uva 10817
Problem D: Headmaster's Headache Time limit: 2 seconds The headmaster of Spring Field School is cons ...
- LA 2038
Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solu ...
- hdu 2639 Bone Collector II (01背包,求第k优解)
这题和典型的01背包求最优解不同,是要求第k优解,所以,最直观的想法就是在01背包的基础上再增加一维表示第k大时的价值.具体思路见下面的参考链接,说的很详细 参考连接:http://laiba2004 ...
- LINUX输入输出与文件
1 文件描述符 内核为每个进程维护一个已打开文件的记录表(实现为结构体数组),文件描述符是一个较小的正整数(0-1023)(结构体数组下标),它代表记录表的一项,通过文件描述符和一组基于文件描述符的文 ...