<body>
<a href="servlet/DownLoadExcelServlet">下载表格</a> <br>
</body>
public class DownLoadExcelServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// response.setContentType("text/html");
// PrintWriter out = response.getWriter();
// out
// .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
// out.println("<HTML>");
// out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
// out.println(" <BODY>");
// out.print(" This is ");
// out.print(this.getClass());
// out.println(", using the POST method");
// out.println(" </BODY>");
// out.println("</HTML>");
// out.flush();
// out.close();
String fileName = "收费报表";
if(request.getHeader("User-Agent").toUpperCase().indexOf("FIREFOX") > 0){
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1"); //火狐
}else if(request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0){
fileName = URLEncoder.encode(fileName, "UTF-8"); // IE
}
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition","attachment; filename=\"" + fileName + ".xls" + "\"");
OutputStream os = response.getOutputStream();
downLoadXLS(os);
}
public void downLoadXLS(OutputStream os){
try {
// String path = "f:\\app\\iotest\\n.xls";
// String postfix = (path.trim().lastIndexOf(".") == -1)?"":path.substring(path.trim().lastIndexOf(".") + 1);
// if("xls".equalsIgnoreCase(postfix) || "xlsx".equalsIgnoreCase(postfix)){
HSSFWorkbook wb = new HSSFWorkbook();
//设置居中
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFCellStyle cellStyle2 = wb.createCellStyle();
cellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//设置字体大小
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 10);//字号
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗
cellStyle.setFont(font);
HSSFSheet sheet = wb.createSheet();
wb.setSheetName(0, "feeUser");
//设置列宽80个字符的宽度 ; 80 * 256 / 256;
sheet.setColumnWidth(0, 15 * 256);
sheet.setColumnWidth(1, 25 * 256);
sheet.setColumnWidth(2, 15 * 256);
sheet.setColumnWidth(3, 25 * 256);
for(int i=4;i<=11;i++){
sheet.setColumnWidth(i, 13 * 256);
}
//表头
HSSFRow row = sheet.createRow(0);
//设置行高
row.setHeightInPoints(25);
HSSFCell cell0 = row.createCell(0);
cell0.setCellValue("序号");
cell0.setCellStyle(cellStyle);
HSSFCell cell1 = row.createCell(1);
cell1.setCellValue("户名(共125户)");
cell1.setCellStyle(cellStyle);
HSSFCell cell2 = row.createCell(2);
cell2.setCellValue("缴费日");
cell2.setCellStyle(cellStyle);
HSSFCell cell3 = row.createCell(3);
cell3.setCellValue("表计");
cell3.setCellStyle(cellStyle);
row = sheet.createRow(1);
row.setHeightInPoints(30);
HSSFCell cell = row.createCell(3);
cell.setCellValue("表计");
cell.setCellStyle(cellStyle);
cell = row.createCell(4);
cell.setCellValue("通讯地址");
cell.setCellStyle(cellStyle);
cell = row.createCell(5);
cell.setCellValue("起始时间");
cell.setCellStyle(cellStyle);
cell = row.createCell(6);
cell.setCellValue("起始表底");
cell.setCellStyle(cellStyle);
cell = row.createCell(7);
cell.setCellValue("结束时间");
cell.setCellStyle(cellStyle);
cell = row.createCell(8);
cell.setCellValue("结束表底");
cell.setCellStyle(cellStyle);
cell = row.createCell(9);
cell.setCellValue("差值");
cell.setCellStyle(cellStyle);
cell = row.createCell(10);
cell.setCellValue("倍率");
cell.setCellStyle(cellStyle);
cell = row.createCell(11);
cell.setCellValue("电量");
cell.setCellStyle(cellStyle);
// sheet.addMergedRegion(new Region(0,(short)0,1,(short)0));
// sheet.addMergedRegion(new Region(0,(short)1,1,(short)1));
// sheet.addMergedRegion(new Region(0,(short)2,1,(short)2));
// sheet.addMergedRegion(new Region(0,(short)3,0,(short)11));
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1));
sheet.addMergedRegion(new CellRangeAddress(0, 1, 2, 2));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 3, 11));
List<UserFee> userFeeLis = this.getUserFeeList();
int i = 2;
int index = 1;
for(UserFee uf: userFeeLis){
HSSFRow hsRow = sheet.createRow(i++);
for(int j=0;j<=11;j++){
HSSFCell hsCell = hsRow.createCell(j);
hsCell.setCellStyle(cellStyle2);
switch (j) {
case 0:
hsCell.setCellValue(index++);
break;
case 1:
hsCell.setCellValue(uf.getUnitName());
break;
case 2:
hsCell.setCellValue(Integer.valueOf(uf.getDay()));
break;
case 3:
hsCell.setCellValue(uf.getDeviceName());
break;
case 4:
hsCell.setCellValue(Double.valueOf(uf.getAddress()));
break;
case 5:
hsCell.setCellValue(uf.getStartDate());
break;
case 6:
hsCell.setCellValue(Double.valueOf(uf.getStartValue()));
break;
case 7:
hsCell.setCellValue(uf.getEndDate());
break;
case 8:
hsCell.setCellValue(Double.valueOf(uf.getEndValue()));
break;
case 9:
hsCell.setCellValue(Double.valueOf(uf.getChazhi()));
break;
case 10:
hsCell.setCellValue(Double.valueOf(uf.getBeilv()));
break;
case 11:
hsCell.setCellValue(Double.valueOf(uf.getElec()));
break;
default:
break;
}
}
}
wb.write(os);
os.flush();
os.close();
/*}else{
System.out.println("不支持的文件类型");
}*/
} catch (IOException e) {
e.printStackTrace();
}
}
private List<UserFee> getUserFeeList(){
List<UserFee> userFeeList = new ArrayList<UserFee>();
UserFee uf1 = new UserFee();
uf1.setUnitName("爱慕1");
uf1.setDay("25");
uf1.setDeviceName("爱慕1(面1)(店内)");
uf1.setAddress("41508348705");
uf1.setStartDate("2015-11-24");
uf1.setStartValue("953.11");
uf1.setEndDate("2015-12-24");
uf1.setEndValue("1451.88");
uf1.setChazhi("498.77");
uf1.setBeilv("1");
uf1.setElec("498.77");
UserFee uf2 = new UserFee();
uf2.setUnitName("安世宁4");
uf2.setDay("25");
uf2.setDeviceName("安世宁4(201)(室内)");
uf2.setAddress("041508348593");
uf2.setStartDate("2015-11-24");
uf2.setStartValue("42.32");
uf2.setEndDate("2015-12-24");
uf2.setEndValue("70.09");
uf2.setChazhi("27.77");
uf2.setBeilv("1");
uf2.setElec("27.77");
UserFee uf3 = new UserFee();
uf3.setUnitName("百圣百");
uf3.setDay("25");
uf3.setDeviceName("百圣百(212)(室内)");
uf3.setAddress("41508642495");
uf3.setStartDate("2015-11-24");
uf3.setStartValue("459.83");
uf3.setEndDate("2015-12-24");
uf3.setEndValue("1046.06");
uf3.setChazhi("586.23");
uf3.setBeilv("1");
uf3.setElec("586.23");
userFeeList.add(uf1);
userFeeList.add(uf2);
userFeeList.add(uf3);
return userFeeList;
}
}
public class UserFee {
private String unitName;
private String day;
private String deviceName;
private String address;
private String startDate;
private String startValue;
private String endDate;
private String endValue;
private String chazhi;
private String beilv;
private String elec;
public String getUnitName() {
return unitName;
}
public void setUnitName(String unitName) {
this.unitName = unitName;
}
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
public String getDeviceName() {
return deviceName;
}
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getStartValue() {
return startValue;
}
public void setStartValue(String startValue) {
this.startValue = startValue;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public String getEndValue() {
return endValue;
}
public void setEndValue(String endValue) {
this.endValue = endValue;
}
public String getChazhi() {
return chazhi;
}
public void setChazhi(String chazhi) {
this.chazhi = chazhi;
}
public String getBeilv() {
return beilv;
}
public void setBeilv(String beilv) {
this.beilv = beilv;
}
public String getElec() {
return elec;
}
public void setElec(String elec) {
this.elec = elec;
}
}
分享到:
相关推荐
使用apach poi 导出Excel,可直接在项目中使用使用apach poi 导出Excel,可直接在项目中使用使用apach poi 导出Excel,可直接在项目中使用
Java Poi 导出excel(支持各种设置字体、颜色、垂直居中)
JAVA POI导出EXCEL代码
通用类的Java导出Excel方法,导出内容是使用json传递,字段描述使用ArrayList数组传递,
java使用POI导出 Excel+图片工具类 ,里面含有poi jar包,只调用接口即可直接保存Excel。使用的时候需先把数据封装,具体包装需根据实际导出数据进行处理。文件demo中只提供包装格式。
Java POI 导出Excel 2007简单实例源代码 该源代码的jar包,Java POI导出EXCEL经典实现
这个是利用POI导出excel的源码,需要导入poi包,然后把jsp和action的代码拷贝进去就可以直接运行,很基础的代码,很适合初学者学习。 POI可以自己定义导出excel格式的数据,实例就是这样弄的,希望对初学者有帮助。
Java实现POI导出Excel 博文参考:http://blog.csdn.net/itmyhome1990/article/details/49818045
需要使用JAVA 做EXCEL导出的同学,有问题尽管私信
java poi 导出excel jar包
这个是java用poi操作Excel进行导出,并且可以自动换行
该实例集成maven开发环境,里面讲解了POI导入和导出Excel表格的基本用法
java poi excel数据的导出
String outputFile = "D:\\excel\\excel.xlsx"; OutputStream outputStream = new FileOutputStream(outputFile); UtilExcel utilExcel = new UtilExcel(); String titles = "所属区域,所属车间,当前处理人,描述...
Java POI 生成Excel时显示货币格式
利用poi技术导出EXCEL表格,文件里还包含部分关键代码,使用方便
java实现excel的导入导出(poi详解),经过测试poi效率要比jxl要高很多,特别是数据量大的时候jxl根本无法用肉眼入目,本资源是个可运行项目demo,很有参考价值!