java POI 导出 Excel

做个笔记以后用,高手勿喷

pom.xml (这里使用的是JFinal的JBolt创建的项目,版本号可以随意,好用就行呗~)

		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>${poi.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>${poi.version}</version>
		</dependency>

分了两个类,一个是直接访问的,用来度数据和控制流程,一个用来操作文件

第一个:

package controller;

import java.io.File;
import java.net.MalformedURLException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import util.ExcelExportUtil;

public class OutExcel extends Controller {

	public void index() throws MalformedURLException {
		String path = null;
		try {
  
			// 中文名称
			String[] nameArr = { "用户名", "密码" };
			// 对应的字段
			String[] checkArr = { "username", "password" };

			// 将所有列的名称和对应的字段存储为Map
			Map<String, String> titleData = new LinkedHashMap<String, String>();
			for (int i = 0; i < nameArr.length; i++) {
				titleData.put(checkArr[i], nameArr[i]);
			}

			// 获取所有数据
			List<Record> dataList = Db.find("select *  from user ");

			// 创建一个文件
			File file = new File(ExcelExportUtil.getFilePath());

			// 将数据写入文件
			file = ExcelExportUtil.saveFile(titleData, dataList, file);
			//获取文件地址
			path = file.getPath().toString();

		} catch (Exception e) {
			e.printStackTrace();
		}
		renderFile(new File(path));
	}

}

第二个:

package util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Record;

public class ExcelExportUtil {

	private static final String FILE_PATH = PathKit.getWebRootPath() + File.separator + "upload" + File.separator+"reportExcel"+File.separator;
	
	public static String getFilePath(){
		File tempFile = new File(FILE_PATH);
		if (!tempFile.exists()) {
			tempFile.mkdirs();
		}
		String title = FILE_PATH + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + ".xlsx";
	     return title;
	}
	
	
	@SuppressWarnings("resource")
	public static File saveFile(Map<String, String> headData, List<Record> dataList, File file) {
		// 创建工作薄
		XSSFWorkbook hssfWorkbook = new XSSFWorkbook();
		// sheet:一张表的简称
		// row:表里的行
		// 创建工作薄中的工作表
		XSSFSheet hssfSheet = hssfWorkbook.createSheet();
		// 创建行
		XSSFRow row = hssfSheet.createRow(0);
		// 创建单元格,设置表头 创建列
		XSSFCell cell = null;
		// 初始化索引
		int rowIndex = 0;
		int cellIndex = 0;

		// 创建标题行
		row = hssfSheet.createRow(rowIndex);
		rowIndex++;
		// 遍历标题
		for (String h : headData.keySet()) {
			//创建列
			cell = row.createCell(cellIndex);
			//索引递增
			cellIndex++;
			//逐列插入标题
			cell.setCellValue(headData.get(h));
		}

		// 得到所有记录 行:列
	 
		Record record = null;
		if (dataList != null) {
			// 获取所有的记录 有多少条记录就创建多少行
			for (int i = 0; i < dataList.size(); i++) {
				row = hssfSheet.createRow(rowIndex);
				// 得到所有的行 一个record就代表 一行
				record = dataList.get(i);
				//下一行索引
				rowIndex++;
				//刷新新行索引
				cellIndex = 0;
				// 在有所有的记录基础之上,便利传入进来的表头,再创建N行
				for (String h : headData.keySet()) {
					cell = row.createCell(cellIndex);
					cellIndex++;
					//按照每条记录匹配数据
					cell.setCellValue(record.get(h) == null ? "" : record.get(h).toString());
				}
			}
		}
		try {
			FileOutputStream fileOutputStreane = new FileOutputStream(file);
			hssfWorkbook.write(fileOutputStreane);
			fileOutputStreane.flush();
			fileOutputStreane.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return file;
	}
}

瞅啥呢都完事了

发布了34 篇原创文章 · 获赞 5 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_36623327/article/details/94154476