`
ntwjf
  • 浏览: 1741 次
  • 性别: Icon_minigender_1
  • 来自: 南京
文章分类
社区版块
存档分类
最新评论

java poi导出excel

    博客分类:
  • java
阅读更多
  <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;
	}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics