欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POI操作xls表格文件

程序员文章站 2022-07-13 13:11:06
...
private void printExcelList(String templateFile, String writeFile,
		int beginRowIndex, String[] columns, boolean needIndex,
		List<Map<String, Object>> objectList) throws Exception {
	if(objectList != null && objectList.size() > 0
			&& columns != null && columns.length > 0
			&& templateFile != null && !templateFile.equals("")
			&& writeFile != null && !writeFile.equals("")){
			
			FileInputStream in = null;
		//先做输入文件的格式检查
		try{
			in = new FileInputStream(new File(templateFile));
		}catch(Exception e){
			throw new Exception("模板文件“" + templateFile + "”找不到!");
		}
		//做输出文件的格式检查
		String path = "";
		if(writeFile.lastIndexOf("\\") >= 0){
			path = writeFile.substring(0,writeFile.lastIndexOf("\\"));
		}else if(writeFile.lastIndexOf("/") >= 0){
			path = writeFile.substring(0,writeFile.lastIndexOf("/"));
		}else{
			throw new Exception("输出文件“" + writeFile + "”格式不正确!");
		}
		File pathFile = new File(path);
		if(!pathFile.exists()){
			pathFile.mkdirs();
		}
		FileOutputStream fileOut = new FileOutputStream(writeFile);			
		
		HSSFWorkbook hwb = new HSSFWorkbook(in);	
		HSSFCellStyle style1 = hwb.createCellStyle();
		style1.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
		style1.setFillPattern(CellStyle.SOLID_FOREGROUND);
		style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直    
        style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
		style1.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框    
		style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框    
		style1.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框    
		style1.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 
		
		HSSFCellStyle style2 = hwb.createCellStyle();
		HSSFFont font=hwb.createFont();
		font.setFontHeightInPoints((short)12);
		font.setFontName("宋体");
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		style2.setFont(font);
		
		style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直    
        style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
		style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框    
		style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框    
		style2.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框    
		style2.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
		
		HSSFCellStyle style3 = hwb.createCellStyle();
		style3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直    
        style3.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
		style3.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框    
		style3.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框    
		style3.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框    
		style3.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
		
		HSSFSheet sheet = hwb.getSheetAt(0);
		sheet.setDefaultRowHeight((short)540);
		int sn = 0;
		for(int i = 0; i < objectList.size(); i++) {
			Map<String,Object> map = objectList.get(sn);
			
			HSSFRow row;
			row = sheet.createRow(i + beginRowIndex);
			row.setHeight((short)540);
			HSSFCell cell = null;
			int startCellNum = 0;
			//如果需要添加序号一列,则将序号一列置于第一列,后面的内容值依次往后面移一个单元格
			if(needIndex){
				startCellNum = 1;
				cell = row.createCell(0);
				cell.setCellValue(i+1);
			}
			for(int index = 0; index < columns.length; index++){
				cell = row.createCell(index + startCellNum);
				if(index==0){		
					cell.setCellStyle(style1);
				}else if(index==2){
					cell.setCellStyle(style2);
				}else{
					cell.setCellStyle(style3);
				}
				cell.setCellValue(map.get(columns[index]) == null ? "" : map.get(columns[index]).toString());
			}
			sn ++;
		}
		
		hwb.write(fileOut);
		fileOut.flush();
		fileOut.close();
		in.close();
		
	}else{
		throw new Exception("传入主要参数不能有空");
	}