Skip to content

[util]export excel via java list structure

Phinehas Zhi edited this page Feb 2, 2019 · 1 revision

copy and modified from web, a easy util to generate excel
can export xlsx
maven:

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.6</version>
        </dependency>

util code:

public class ExcelUtil {

	public static HSSFWorkbook getHSSFWorkbook(String sheetName, List<List<String>> everyLines, HSSFWorkbook wb){
		if(CollectionUtil.isEmpty(everyLines)){
			return null;
		}
		// 第一步,创建一个HSSFWorkbook,对应一个Excel文件
		if(wb == null){
			wb = new HSSFWorkbook();
		}

		// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet(sheetName);

		sheet.setDefaultRowHeight(ROW_HEIGHT);
		// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
		HSSFRow row = sheet.createRow(0);
		// 第四步,创建单元格,并设置值表头
		HSSFCellStyle style = wb.createCellStyle();
		HSSFFont font = wb.createFont();
		font.setFontName("微软雅黑");
		style.setFont(font);
		//声明列对象
		HSSFCell cell = null;

		//创建标题
		List<String> titles = everyLines.get(0);
		for(int i = 0; i< titles.size(); i++){
			cell = row.createCell(i);
			cell.setCellValue(titles.get(i));
			cell.setCellStyle(style);
		}

		//创建内容
		for(int i=0;i<everyLines.size()-1;i++){
			row = sheet.createRow(i + 1);
			for(int j=0;j<everyLines.get(i+1).size();j++){
				//将内容按顺序赋给对应的列对象
				HSSFCell cell1 = row.createCell(j);
				cell1.setCellValue(everyLines.get(i+1).get(j));
				cell1.setCellStyle(style);
			}
		}
		return wb;
	}

	public static HSSFWorkbook getHSSFWorkbookWithWidth(String sheetName, List<List<String>> everyLines, HSSFWorkbook wb, int...widths){
		if(CollectionUtil.isEmpty(everyLines)){
			return null;
		}
		// 第一步,创建一个HSSFWorkbook,对应一个Excel文件
		if(wb == null){
			wb = new HSSFWorkbook();
		}

		// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet(sheetName);

		sheet.setDefaultRowHeight(ROW_HEIGHT);

		for (int i=0; i<widths.length; i++){
			sheet.setColumnWidth(i, widths[i]);
		}

		// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
		HSSFRow row = sheet.createRow(0);
		// 第四步,创建单元格,并设置值表头
		HSSFCellStyle style = wb.createCellStyle();
		HSSFFont font = wb.createFont();
		font.setFontName("微软雅黑");
		style.setFont(font);
		//声明列对象
		HSSFCell cell = null;

		//创建标题
		List<String> titles = everyLines.get(0);
		for(int i = 0; i< titles.size(); i++){
			cell = row.createCell(i);
			cell.setCellValue(titles.get(i));
			cell.setCellStyle(style);
		}

		//创建内容
		for(int i=0;i<everyLines.size()-1;i++){
			row = sheet.createRow(i + 1);
			for(int j=0;j<everyLines.get(i+1).size();j++){
				//将内容按顺序赋给对应的列对象
				HSSFCell cell1 = row.createCell(j);
				cell1.setCellValue(everyLines.get(i+1).get(j));
				cell1.setCellStyle(style);
			}
		}
		return wb;
	}

how export in tomcat:

private void exportExcel(String reportId, HttpServletResponse response, List<List<String>> everyLines) {
		try {
			ServletOutputStream outputStream = response.getOutputStream();
			//设置文件名
			response.addHeader("Content-Disposition", "attachment;filename=inputYourOwnExcelNameHere.xls");
			response.setContentType("APPLICATION/msexcel");
			response.addHeader("Pargam", "no-cache");
			response.addHeader("Cache-Control", "no-cache");

			HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(SHEET_NAME, everyLines, null);

			if(wb != null){
				HSSFSheet sheet = wb.getSheet(SHEET_NAME);
				wb.write(outputStream);
			}
			outputStream.flush();
			outputStream.close();
		} catch (IOException e) {
			LogUtil.warn(LOGGER,e,"export excel error!");
		}
	}

Clone this wiki locally