需求场景:
最近在做一个手机借记管理平台(WEB),项目新功能要求能下载所有手机的信息(也就是存在数据库的信息)生成EXCEL表格。
用到的工具:
JAVA EXCEL(下载地址:http://nchc.dl.sourceforge.net/project/jexcelapi/jexcelapi/2.6.6/jexcelapi_2_6_6.zip 将 jxl.jar复制到WEB-INF/lib目录下面即可)
具体实现:
1.实现DownloadUTestPhonesExcel类,实现对OutputStream的数据写入:
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import com.utest.model.Phone;
import com.utest.service.PhoneSer;
import com.utest.service.ServiceManager;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class DownloadUTestPhonesExcel {
public void createExcel(OutputStream os) throws WriteException,IOException{
//创建工作薄
WritableWorkbook workbook = Workbook.createWorkbook(os);
//创建新的一页
WritableSheet sheet = workbook.createSheet("First Sheet",0);
//创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数为内容
//查询数据库
PhoneSer phoneSer = (PhoneSer) ServiceManager.getService(Constants.PHONE_SERVICE);
Phone phone = new Phone();
phone.setIsDeleted(0);
phone.setManager("max");
phone.setPageSize(65535);
ArrayList<Phone> phones = (ArrayList<Phone>) phoneSer.selectPhoneInfo(phone);
String[] tableheads={
"id","manufacturer","Model","manager","umanager"
};
//填写表头
Label tempdate;
for (int j = 0; j <tableheads.length ; j++) {
tempdate = new Label(j,0,tableheads[j]);
sheet.addCell(tempdate);
tempdate = null;
}
//填写数据
for(int i= 0;i<phones.size();i++){
//id
tempdate = new Label(0,i+1,phones.get(i).getId());
sheet.addCell(tempdate);
tempdate = null;
//manufacturer
tempdate = new Label(1,i+1,phones.get(i).getManufacturer());
sheet.addCell(tempdate);
tempdate = null;
//Model
tempdate = new Label(2,i+1,phones.get(i).getModel());
sheet.addCell(tempdate);
tempdate = null;
//manager
tempdate = new Label(3,i+1,phones.get(i).getManager());
sheet.addCell(tempdate);
tempdate = null;
//umanager
tempdate = new Label(4,i+1,phones.get(i).getUmanager());
sheet.addCell(tempdate);
tempdate = null;
}
//把创建的内容写入到输出流中,并关闭输出流
workbook.write();
workbook.close();
os.close();
}
}
2.实现global_download_utest_phones.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.io.*" %>
<%@ page import="com.utest.utils.*" %>
<%
String fname = "PhonesOfUTest";
OutputStream os = response.getOutputStream();//取得输出流
response.reset();//清空输出流
//下面是对中文文件名的处理
response.setCharacterEncoding("UTF-8");//设置相应内容的编码格式
fname = java.net.URLEncoder.encode(fname,"UTF-8");
response.setHeader("Content-Disposition","attachment;filename="+new String(fname.getBytes("UTF-8"),"GBK")+".xls");
response.setContentType("application/msexcel");//定义输出类型
DownloadUTestPhonesExcel sw = new DownloadUTestPhonesExcel();
sw.createExcel(os);
%>
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
3.结果:
当然要实现更细致的Excel,我们还需更多的了解JAVA EXCEL:
可以查看下载文件中的jexcelapi_2_6_6\jexcelapi\docs\index.html文件,了解更多实现方法(如高光,下划线,合并单元格等操作)
例如:
//浮点数据
Number number = new Number(1,1,3.1415926535);
//整形数据
Number ints = new Number(2,1,15042699);
//日期型数据
Calendar c = Calendar.getInstance();
Date date = c.getTime();
WritableCellFormat cf1 = new WritableCellFormat(DateFormats.FORMAT1);
DateTime dt = new DateTime(4,1,date,cf1);
sheet.addCell(dt);
// 如果觉得不错,记得顶我哦! 顶我!顶我!顶我!