这本来是几年前写的代码了,不过给忘了。最近重新做了个这个功能,又给拾起来了。
使用的是jxl 当时用这个的原因是因为业务需要:导出图片到表格。当时没查到poi的导出图片就用的jxl。poi技术我会在整理一下。在后续发出来。后面会加个链接吧!导出图片的代码后面在写,前面写不是导出图片的代码。文笔不好,敬请谅解。排版请原谅我真的不会(会也不弄排版)
maven依赖
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
直接上代码(有注释):
解释一下传入的参数。
String[] title: 是我的标题。Excel表格的第一行,也就是标题。有多少个标题,在下面代码中可以清晰看到。
List<List<String>> context: 这一个是我们要写入到Excel表格的数据。后面我会贴出相关代码。
public static boolean makeExcel(HttpServletRequest request, String[] title,List<List<String>> context) {
boolean res = true;
WritableWorkbook wk = null;
try {
String path = request.getSession().getServletContext().getRealPath(""); // 获取项目的路径
path = path + "\\upload\\" + "a.xls"; // 只能是.xls 后缀名,.xlsx不行,我office2016打不开。不知道低版本的可不可以打开
// System.out.println(path);
File file = new File(path);
if (file.exists()) { // 由于我是写在服务器的项目里面的 , 所以我判断的是如果有这个文件就删除。再重新建一个
file.delete();
}
// 创建可写入的Excel工作薄 t.xls为要新建的文件名
wk = Workbook.createWorkbook(new File(path));
// 创建可写入的Excel工作表 生成名为“中药材统计”的工作表 0表示这是第一页
WritableSheet sheet = wk.createSheet("中药材统计", 0);
// 表头
for (int i = 0; i < title.length; i++) {
// 添加Label对象,参数依次表示在第一列,第一行,内容, 使用的格式
Label labTitle = new Label(i, 1, title[i]);
sheet.addCell(labTitle);
}
for (int i = 0; i < context.size(); i++) {
for (int j = 0; j < context.get(i).size(); j++) {
List<String> fileName = context.get(i);
Label labTitle = new Label(j, i + 2, context.get(i).get(j));
sheet.addCell(labTitle);
}
}
wk.write();
// System.out.println("服务器端数据生成完毕!!!");
} catch (IOException e) {
res = false;
} catch (RowsExceededException e) {
res = false;
} catch (WriteException e) {
res = false;
} finally {
try {
wk.close();
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return res;
}
// List<List<String>> context 把我们要导出到Excel表格的数据存入到List中
public static List<List<String>> createStatsticContent(List<Medicine> statisticsList) throws IOException {
List<List<String>> contextList = new ArrayList<List<String>>();
for (int i = 0; i < statisticsList.size(); i++) {
List<String> context = new ArrayList<String>();
String id = statisticsList.get(i).getMnum();
String name = statisticsList.get(i).getMname();
String msdate = statisticsList.get(i).getMsdate();
String msdates = statisticsList.get(i).getMsdates();
String mregion = statisticsList.get(i).getMregion();
int mstatuses = statisticsList.get(i).getMstatuses();
String mstatusesdes = (mstatuses == 0) ? "未出" : "已出";
int mreport = statisticsList.get(i).getMreport();
String mreportdes = (mreport == 0) ? "不合格" : "合格";
String mremark = statisticsList.get(i).getMremark();
context.add(String.valueOf(i + 1));
context.add(id);
context.add(name);
context.add(String.valueOf(msdate));
context.add(String.valueOf(msdates));
context.add(mregion);
context.add(mstatusesdes);
context.add(mreportdes);
context.add(mremark);
contextList.add(context);
}
// System.out.println("表格内容生成完毕!!!");
return contextList;
}
// Medicine 这是个实体类,这个数据我是从库里面查询出来的。
// 最后,让我们调用下载功能。由于我的Excel是生成到服务器下面的。所以需要下载功能。我不知道怎么直接生成到用户的机器上。原谅我不会。如果有哪个网友可以指点我一下。感激不尽
public static void down(HttpServletRequest request,HttpServletResponse response) {
InputStream fis = null;
OutputStream toClient = null;
try {
// 文件默认生成在项目根目录upload下
String path = request.getSession().getServletContext().getRealPath("");
path = path + "\\upload\\" + "a.xls" ;
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 以流的形式下载文件。
fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename="+ new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
} catch (IOException ex) {
} finally {
try {
fis.close();
} catch (IOException e) {
}
try {
toClient.close();
} catch (IOException e) {
}
}
}
下载的功能就不多做叙述。String[] title的这个值随便写几个就行,就不贴出来了,已经涉及到机密性的了。(当时做的这个系统,上线了一周就下线了。因为涉及数据泄露。ヾ(@⌒ー⌒@)ノ)
一个简单的Excel导出功能这样就可以实现了。如果有什么不对的地方,请大家指出来。
这是原创文章。若转载,请注明出处。
老板来了,不写了。导出图片的代码我会挑个时间补上。若急需的,可以私聊我。我要是可以看到。可以优先发给你。