import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class ExcelUtil {
private final static String XLS = "xls";
private final static String XLSX = "xlsx";
private static Logger logger = LoggerFactory.getLogger(ExcelUtil.class);
public static void exportExcel(HttpServletResponse response, List<String> headList, List<Map<String, Object>> resultList, String[] rowName, String fileName) {
if (resultList.size() <= 0) {
logger.info("没有导出的数据");
return;
}
//创建excel表格对象
HSSFWorkbook wb = new HSSFWorkbook();
//创建工作表
HSSFSheet sheet = wb.createSheet("sheet");
//设置默认字体大小
sheet.setDefaultColumnWidth(20);
//设置样式
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//设置字体
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 16);
cellStyle.setFont(font);
//写入第一行
HSSFRow row = sheet.createRow(0);
//将信息写入
HSSFCell headCell = null;
for (int i = 0; i < headList.size(); i++) {
headCell = row.createCell(i);
headCell.setCellValue(headList.get(i));
java导出导入工具类
于 2022-09-27 14:16:06 首次发布
本文介绍了一个用于Java应用的数据导出与导入的工具类,支持常见的文件格式如CSV、Excel。该工具类利用了Apache POI库进行Excel操作,结合Servlet实现文件下载与上传功能,方便在Web应用中进行批量数据处理。
摘要由CSDN通过智能技术生成