poi之Excel下载之详细设置

1、设置标题格式

       /**
	 * HEAD样式
	 *
	 * @param workbook
	 * @param sheet
	 */
	public void setHeadCellStyles(HSSFWorkbook workbook, HSSFSheet sheet) {
		headStyle = workbook.createCellStyle();
		headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		HSSFFont font = workbook.createFont();
		headStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		font.setFontName("宋体");
		font.setFontHeightInPoints((short) 16);// 设置字体大小
		headStyle.setFont(font);
	}

  

2、设置列头样式

     /**
	 * 列头样式
	 * @param workbook
	 * @param sheet
	 */
	public void setTitleCellStyles(HSSFWorkbook workbook, HSSFSheet sheet)
        {
		titleStyle = workbook.createCellStyle();

		// 设置边框
		titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		// 设置背景色
		titleStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
		titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		// 设置居中
		titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		// 设置字体
		HSSFFont font = workbook.createFont();
		font.setFontName("宋体");
		font.setFontHeightInPoints((short) 11); // 设置字体大小
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 粗体显示
		titleStyle.setFont(font);// 选择需要用到的字体格式
		// 设置自动换行
		titleStyle.setWrapText(true);
		// 设置列宽 ,第一个参数代表列id(从0开始),第2个参数代表宽度值
		sheet.setColumnWidth(0, 7000);//
//		sheet.setColumnWidth(1, 7000);//
		sheet.setColumnWidth(1, 4000);//
		sheet.setColumnWidth(2, 4000);//
		sheet.setColumnWidth(3, 4000);//
		sheet.setColumnWidth(4, 7000);//
		sheet.setColumnWidth(5, 7000);//
		sheet.setColumnWidth(6, 4000);//
		sheet.setColumnWidth(7, 4000);//
		sheet.setColumnWidth(8, 4000);//
		sheet.setColumnWidth(9, 4000);//
		sheet.setColumnWidth(10, 4000);//
}

  

3、设置数据样式

       /**
	 * 数据样式
	 *
	 * @param workbook
	 * @param sheet
	 */
	public void setDataCellStyles(HSSFWorkbook workbook, HSSFSheet sheet) {
		dataStyle = workbook.createCellStyle();

		// 设置边框
		dataStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		dataStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		dataStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		dataStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		// 设置背景色
		dataStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
		dataStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		// 设置居中
		dataStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
		// 设置字体
		HSSFFont font = workbook.createFont();
		font.setFontName("宋体");
		font.setFontHeightInPoints((short) 11); // 设置字体大小
		dataStyle.setFont(font);// 选择需要用到的字体格式
		// 设置自动换行
		dataStyle.setWrapText(true);
	}

  

4、创建隐藏页和数据域(省市区三级联动下拉框数据隐藏域设置)

        /**
	 * 创建隐藏页和数据域(省市区三级联动下拉框数据隐藏域设置)
	 * @param workbook
	 * @param hideSheetName
	 */
	public void creatHideSheet(HSSFWorkbook workbook) {
		/****************************************************** 创建省市区 ***************************************************/
		HSSFSheet factoryAndModelSheet = workbook.createSheet("factoryAndModelSheet");// 隐藏一些信息
		/*************************************************** 省-市 start ***************************************************/
		//省份
		List<Area> proviceList = 获得省份列表;
		List<String> rowList = null;
		String provinceid = "";
		String provinceCity = "";
		Name name;
		for(int i=0;i<proviceList.size();i++){
			HSSFRow pfModelRow = factoryAndModelSheet.createRow(i);
			rowList = new ArrayList<String>();
			provinceid = proviceList.get(i).getId().toString();
			provinceCity = proviceList.get(i).getProvinceCity();
			rowList.add(provinceCity + "_" + provinceid);
			List<Area> cityList = 根据省份获得城市列表;
			// 添加“省--->市” 名称
			name = workbook.createName();
			name.setNameName( provinceCity+ "_" + provinceid);
			for(int j=0; j<cityList.size(); j++){
				Area area = cityList.get(j);
				rowList.add(area.getProvinceCity()+"_"+area.getId().toString());
			}
			this.creatRow(pfModelRow, rowList);
			name.setRefersToFormula("factoryAndModelSheet!$B$" + (i + 1) + ":$"
					+ this.getcellColumnFlag(cityList.size() + 1) + "$"
					+ (i + 1));
		}
		name = workbook.createName();
		name.setNameName("provice");
		name.setRefersToFormula("factoryAndModelSheet!$A$1:$A$"+ proviceList.size());
		/*************************************************** 省-市 end ***************************************************/
		/*************************************************** 市-区 start ***************************************************/
		//市
		int sm = proviceList.size()+1;
		List<String> cityNList = null;
		List<Area> cityareaList = new LinkedList<Area>();
		String cityid = "";
		String cityCity = "";
		//将所有的市加之cityareaList
		for(int i=0;i<proviceList.size();i++){
			provinceid = proviceList.get(i).getId().toString();
			List<Area> cityList = 获得城市列表;
			cityareaList.addAll(cityList);//加至List
		}
		for(int i=0;i<cityareaList.size();i++){
			HSSFRow pfModelRow = factoryAndModelSheet.createRow(sm+i);
			cityNList = new ArrayList<String>();
			cityid = cityareaList.get(i).getId().toString();
			cityCity = cityareaList.get(i).getProvinceCity();
			cityNList.add(cityCity + "_" + cityid);
			List<Area> cityList = 根据城市查询区域列表;
			// 添加“市--->区” 名称
			name = workbook.createName();
			name.setNameName( cityCity+ "_" + cityid);
			for(int j=0; j<cityList.size(); j++){
				Area area = cityList.get(j);
				cityNList.add(area.getProvinceCity()+"_"+area.getId().toString());
			}
			this.creatRow(pfModelRow, cityNList);
			name.setRefersToFormula("factoryAndModelSheet!$B$" + (sm+i + 1) + ":$"
					+ this.getcellColumnFlag(cityList.size() + 1) + "$"
					+ (sm+i + 1));
		}
		name = workbook.createName();
		name.setNameName("citycityname");
		name.setRefersToFormula("factoryAndModelSheet!$A$"+sm+":$A$"+ cityareaList.size());
		/*************************************************** 市-区 end ***************************************************/
		// 设置隐藏页标志
		workbook.setSheetHidden(workbook.getSheetIndex("factoryAndModelSheet"),true);
}

  

5、创建标题和列头数据

/**
	 * 创建标题应用列头
	 * @param userinfosheet1
	 * @param userName
	 */
	public void creatAppRowHead(HSSFSheet userinfosheet1, String headName) {
		// 设置标题
		HSSFRow rowHead = userinfosheet1.createRow(0);
		userinfosheet1.addMergedRegion(new Region(0, (short) 0, 0, (short) 21));
		POIUtils.createCell(rowHead, (short) 0, headName, headStyle);
		rowHead.setHeight((short) (27 * 20));

		// 设置表头
		HSSFRow row = userinfosheet1.createRow(1);

		HSSFCell merchantIdCell = row.createCell(0);
		merchantIdCell.setCellValue("一");
		merchantIdCell.setCellStyle(titleStyle);

//		HSSFCell mrchtNameCell = row.createCell(1);
//		mrchtNameCell.setCellValue("二");
//		mrchtNameCell.setCellStyle(titleStyle);

		HSSFCell merchantEnameCell = row.createCell(1);
		merchantEnameCell.setCellValue("二");
		merchantEnameCell.setCellStyle(titleStyle);

		HSSFCell abbrCnameCell = row.createCell(2);
		abbrCnameCell.setCellValue("三");
		abbrCnameCell.setCellStyle(titleStyle);

		HSSFCell abbrEnameCell = row.createCell(3);
		abbrEnameCell.setCellValue("四");
		abbrEnameCell.setCellStyle(titleStyle);

		HSSFCell mccIdCell = row.createCell(4);
		mccIdCell.setCellValue("五");
		mccIdCell.setCellStyle(titleStyle);

		HSSFCell addressCell = row.createCell(5);
		addressCell.setCellValue("六");
		addressCell.setCellStyle(titleStyle);

		HSSFCell provinceCell = row.createCell(6);
		provinceCell.setCellValue("七");
		provinceCell.setCellStyle(titleStyle);

		HSSFCell cityNoCell = row.createCell(7);
		cityNoCell.setCellValue("八");
		cityNoCell.setCellStyle(titleStyle);

		HSSFCell zoneCell = row.createCell(8);
		zoneCell.setCellValue("九");
		zoneCell.setCellStyle(titleStyle);

		HSSFCell telephoneCell = row.createCell(9);
		telephoneCell.setCellValue("十");
		telephoneCell.setCellStyle(titleStyle);

		HSSFCell managerCell = row.createCell(10);
		managerCell.setCellValue("十一");
		managerCell.setCellStyle(titleStyle);
}

  

6、设置下拉框

/**
	 * 添加下拉框限制
	 * @param sheet1
	 */
	public void createSelectValidate(HSSFSheet sheet1) {

		//省,第7列
		DVConstraint constraintPosType = DVConstraint.createFormulaListConstraint("provice");
		// 作用域:起始行、终止行、起始列、终止列
		CellRangeAddressList regionsPosType = new CellRangeAddressList(2, 499,6, 6);
		DataValidation data_validation_PosType = new HSSFDataValidation(regionsPosType, constraintPosType);
		sheet1.addValidationData(data_validation_PosType);

		//市,第8列
		DVConstraint constraintcityNo = DVConstraint.createFormulaListConstraint("INDIRECT($G$3:$G$500)");
		// 作用域:起始行、终止行、起始列、终止列
		CellRangeAddressList regionscityNo = new CellRangeAddressList(2, 499,7, 7);
		DataValidation data_validation_cityNo = new HSSFDataValidation(regionscityNo, constraintcityNo);
		sheet1.addValidationData(data_validation_cityNo);

		//区,第9列
		DVConstraint constraintmanager = DVConstraint.createFormulaListConstraint("INDIRECT($H$3:$H$500)");
		// 作用域:起始行、终止行、起始列、终止列
		CellRangeAddressList regionsmanager = new CellRangeAddressList(2, 499,8, 8);
		DataValidation data_validation_manager = new HSSFDataValidation(regionsmanager, constraintmanager);
		sheet1.addValidationData(data_validation_manager);

	}

  

7、工具方法-创建一列数据

        /**
	 * 创建一列数据
	 *
	 * @param currentRow
	 * @param textList
	 */
	public void creatRow(HSSFRow currentRow, List<String> textList) {
		if (textList != null && textList.size() > 0) {
			int i = 0;
			for (String cellValue : textList) {
				HSSFCell userNameLableCell = currentRow.createCell(i++);
				userNameLableCell.setCellValue(cellValue);
			}
		}
	}

	// 根据数据值确定单元格位置(比如:28-AB)
	private String getcellColumnFlag(int num) {
		String columFiled = "";
		int chuNum = 0;
		int yuNum = 0;
		if (num >= 1 && num <= 26) {
			columFiled = this.doHandle(num);
		} else {
			chuNum = num / 26;
			yuNum = num % 26;

			columFiled += this.doHandle(chuNum);
			columFiled += this.doHandle(yuNum);
		}
		return columFiled;
	}

	private String doHandle(final int num) {
		String[] charArr = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
				"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
				"W", "X", "Y", "Z" };
		return charArr[num - 1].toString();
	}

  

时间: 2024-11-06 03:55:31

poi之Excel下载之详细设置的相关文章

Springboot利用poi导出excel下载

Springboot利用poi导出excel下载 因为项目中之前的做法是用反射获取属性,所以demo中也是用的反射,我看网上很多文章都是存入一个List中,不知道这两种哪种更何合适一点,或者有什么更好的方法也请大佬们赐教. pom <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.13</version&g

使用poi进行excel下载

进行信息表excel导出 @RequestMapping("exportExl")public ResponseEntity<byte[]> exportExl() throws Exception {    List<Customer> clist = cs.getList();    HSSFWorkbook book = new HSSFWorkbook();    HSSFSheet sheet = book.createSheet("客户信息

poi之Excel(在线生成)下载

poi之Excel下载 @RequestMapping(value = "/xxx") public void importFileTemplate(HttpServletRequest req,HttpServletResponse response, RedirectAttributes redirectAttributes) { log.debug("下载"); try{ workbook = new HSSFWorkbook();// excel文件对象 H

Java POI 导出EXCEL经典实现 Java导出Excel弹出下载框

在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者财务系统中用的非常普遍,因为这些系统经常要做一些报表打印的工作.而数据导出的格式一般是EXCEL或者PDF,我这里就用两篇文章分别给大家介绍下.(注意,我们这里说的数据导出可不是数据库中的数据导出!么误会啦^_^) 呵呵,首先我们来导出EXCEL格式的文件吧.现在主流的操作Excel文件的开源工具有很多,用得比较多的就是Apache的POI及JExcelAPI.这里我们用Apache POI!我们先去Apach

POI对EXCEL的操作【重点:如何设置CELL格式为文本格式】

实际开发过程中通常用到的就是从数据库导出EXCEL表格了,JXL可以这样做,其实POI也可以(关于JXL与POI的异同可访问我之前总结的文章),之前写过POI对七种文档(当然也包括EXCEL)的内容读取操作的文章,这次要写的就非常重要了,就是开发中经常会用到的POI读取数据库导出EXCEL的操作,所谓导出EXCEL也就是生成带数据内容的新的EXCEL文件 目前的POI版本是3.7 下载地址:http://poi.apache.org/download.html#POI-3.7 必须包只有一个:p

struts2中利用POI导出Excel文档并下载

1.项目组负责人让我实现这个接口,因为以前做过类似的,中间并没有遇到什么太困难的事情.其他不说,先上代码: 1 package com.tydic.eshop.action.feedback; 2 3 import java.io.ByteArrayInputStream; 4 import java.io.ByteArrayOutputStream; 5 import java.io.FileInputStream; 6 import java.io.IOException; 7 import

poi导出Excel与项目的结合-案例(包含图片的设置)

不管是xml的导入和导出还是Excel的导入和导出,对应的无非都是文件的解析和下载,下面来讲解的主要是导出(下载) 导入:上传--->解析并组装javaBean(javaBeanList)-->插入数据库(多数为数据库) 导出:查询需要下载的数据-->将JavaBean生成对应的文件(eg:xml.poi.json等)-->下载 poi导出--即下载poi 前台: 下载form表单提交(下载不可以采用ajax提交)(form表单提交查看常识题中form表单的几种提交方式,即给for

POI操作EXCEL之导出Excel(设置有效性,下拉列表引用)

本人使用的是poi-bin-3.10-FINAL-20140208.zip 版本的poi以下是程序关键代码: //需要引用的类 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import ja

POI操作Excel详细解释,HSSF和XSSF两种方式

HSSF道路: package com.tools.poi.lesson1; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.