Excel导出



> 导出则相对麻烦点 自己要是想自己写导出的工具类,首先的知道你要什么样的数据可是,然后再根据遍历数据格式添加到数据中 ---------- import org.apache.commons.lang.StringUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.BufferedOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.net.URLEncoder; import java.util.LinkedList; import java.util.List; import java.util.Map; /** * 导出EXCEL/EXCEL模版下载 工具类 * <xls> * <xlsx> * @author Wangbt * */ public class ExportExcelUtil { /** * 导出Excel * @param titleCode 标题字段(下载模版时用到) * @param titles 标题列 * @param list 内容<br> * 备注:1:建议使用LinkedList<br> * 2:第一个List代表行,第二个List代表当前行的单元格 * @param sheetName excel中sheet名称 * @param filepath 导出路径(带有文件后缀名) * @param response */ public static boolean ExportExcel(String[] titleCode, String[] titles, List<Map<String, Object>> list, String sheetName, String filepath, HttpServletRequest request, HttpServletResponse response) { LinkedList<LinkedList> linkedLists = new LinkedList<>(); LinkedList<Object> objects = null; for (Map<String, Object> map : list) { for (String s : map.keySet()) { objects=new LinkedList<>(); objects.add(s); objects.add(map.get(s)); linkedLists.add(objects); } } BufferedOutputStream outputStream = null; try { // 创建一个excel文件 Workbook workbook = null; String fileType = filepath.substring(filepath.lastIndexOf(".") + 1, filepath.length()); response.setContentType("application/msexcel");// 设置文件类型 String userAgent = request.getHeader("USER-AGENT");// 文件名,包含后缀 if (StringUtils.contains(userAgent, "Chrome") || StringUtils.contains(userAgent, "Firefox")) { // google,火狐浏览器 filepath = new String((filepath).getBytes(), "ISO8859-1"); } else { filepath = URLEncoder.encode(filepath, "UTF8"); // 其他浏览器 } response.setHeader("Content-Disposition","attachment;filename="+filepath); outputStream = new BufferedOutputStream(response.getOutputStream()); if (fileType.equals("xls")) { workbook = new HSSFWorkbook(); } else if (fileType.equals("xlsx")) { workbook = new XSSFWorkbook(); } else { return false; // new NeieException(CommonExceptionDict.ParameterFail); // new CommonExceptionDict("BZ09040"); } // 在workbook中添加一个sheet,对应excel中的sheet // sheet名称可以自定义中文名 if (StringUtils.isBlank(sheetName)) sheetName = "sheet1"; Sheet sheet = workbook.createSheet(sheetName); // 隐藏第一列 // sheet.setColumnHidden(0, true); ExportInternalUtil internalUtil = new ExportInternalUtil(workbook, sheet); CellStyle headStyle = internalUtil.headStyle(); CellStyle bodyStyle = internalUtil.bodySryle(); Cell cell = null; if (titleCode != null && titleCode.length > 0) {// 设置导入时对应字段 Row hiddenRow = sheet.createRow(0); for (int i = 0; i < titleCode.length; i++) { cell = hiddenRow.createCell(i); cell.setCellValue(titleCode[i]); } // 隐藏当前行 hiddenRow.setZeroHeight(true); } // 构建表头 Row headRow = null; if (titleCode != null && titleCode.length > 0) {// 设置了对应字段 headRow = sheet.createRow(1); } else {// 无对应字段 headRow = sheet.createRow(0); } // 循环输出标题 for (int i = 0; i < titles.length; i++) { cell = headRow.createCell(i); cell.setCellStyle(headStyle); cell.setCellValue(titles[i]); } if (linkedLists != null && linkedLists.size() > 0) { // 构建表体数据 for (int i = 0; i < linkedLists.size(); i++) { Row bodyRow = null; if (titleCode != null && titleCode.length > 0) {// 有对应字段 bodyRow = sheet.createRow(i + 2); } else {// 无对应字段 bodyRow = sheet.createRow(i + 1); } List<Object> linked = linkedLists.get(i); for (int j = 0; j < linked.size(); j++) { cell = bodyRow.createCell(j); cell.setCellStyle(bodyStyle); cell.setCellValue(linked.get(j) != null ? linked.get(j).toString() : ""); } } } workbook.write(outputStream); outputStream.flush(); outputStream.close(); } catch (FileNotFoundException e1) { new FileNotFoundException(); // new AppException("BZ09042", "导出文件为::" + filepath == null ? "文件为空" : filepath); return false; } catch (IOException e) { new IOException(); // new AppException("BZ09001", e, ExportExcelUtil.class); return false; } finally { try { outputStream.close(); return true; } catch (IOException e) { new IOException(); // new AppException("BZ09050", e, ExportExcelUtil.class); } } return false; } } ---------- > 下面的的工具类是 给不同的人设计不同的 导出Excel表格样式 工具类的注释已经写了很详细了 ---------- import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; 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.ss.util.CellRangeAddress; /** * 设置Excle样式 * @author Wangbt * */ public class ExportInternalUtil { private Workbook workbook = null; private Sheet sheet = null; public ExportInternalUtil(Workbook workbook, Sheet sheet) { this.workbook = workbook; this.sheet = sheet; } /** * 合并单元格后给单元格加边框 * @param rangeAddress * @param cellStyle */ public void setRegionStyle(CellRangeAddress rangeAddress, CellStyle cellStyle) { int toprowNum = rangeAddress.getFirstRow(); for (int i = 0; i < toprowNum; i++) { Row row = sheet.getRow(i); for (int j = rangeAddress.getFirstColumn(); j <= rangeAddress.getLastColumn(); j++) { Cell cell = row.getCell(j); cell.setCellStyle(cellStyle); } } } /** * 设置表头单元格样式 * @return */ public CellStyle headStyle() { // 创建单元格样式 CellStyle cellStyle = workbook.createCellStyle(); // 设置单元格背景颜色为浅蓝色 cellStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index); cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); // 设置单元格居中对齐 cellStyle.setAlignment(CellStyle.ALIGN_CENTER); // 设置单元格垂直居中对齐 cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 设置单元格内不自动换行 cellStyle.setWrapText(true); // 设置单元格字体样式 Font font = workbook.createFont(); // 字体加粗 font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setFontName("仿宋"); font.setFontHeight((short)200); // 设置单元格边框为细线 cellStyle.setBorderLeft(CellStyle.BORDER_THIN); cellStyle.setBorderBottom(CellStyle.BORDER_THIN); cellStyle.setBorderRight(CellStyle.BORDER_THIN); cellStyle.setBorderTop(CellStyle.BORDER_THIN); return cellStyle; } /** * 设置表体单元格样式 * @return */ public CellStyle bodySryle() { // 创建单元格样式 CellStyle cellStyle = workbook.createCellStyle(); // 设置单元格背景颜色为白色 cellStyle.setFillForegroundColor(HSSFColor.WHITE.index); cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); // 设置单元格居中对齐 cellStyle.setAlignment(CellStyle.ALIGN_CENTER); // 设置单元格垂直居中对齐 cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 设置单元格内不自动换行 cellStyle.setWrapText(true); // 设置单元格字体样式 Font font = workbook.createFont(); // 默认字体 font.setBoldweight(Font.BOLDWEIGHT_NORMAL); font.setFontName("仿宋"); font.setFontHeight((short)200); // 设置单元格边框为细线 cellStyle.setBorderLeft(CellStyle.BORDER_THIN); cellStyle.setBorderBottom(CellStyle.BORDER_THIN); cellStyle.setBorderRight(CellStyle.BORDER_THIN); cellStyle.setBorderTop(CellStyle.BORDER_THIN); return cellStyle; } }
> 导出则相对麻烦点 自己要是想自己写导出的工具类,首先的知道你要什么样的数据可是,然后再根据遍历数据格式添加到数据中 ---------- import org.apache.commons.lang.StringUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.BufferedOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.net.URLEncoder; import java.util.LinkedList; import java.util.List; import java.util.Map; /** * 导出EXCEL/EXCEL模版下载 工具类 * <xls> * <xlsx> * @author Wangbt * */ public class ExportExcelUtil { /** * 导出Excel * @param titleCode 标题字段(下载模版时用到) * @param titles 标题列 * @param list 内容<br> * 备注:1:建议使用LinkedList<br> * 2:第一个List代表行,第二个List代表当前行的单元格 * @param sheetName excel中sheet名称 * @param filepath 导出路径(带有文件后缀名) * @param response */ public static boolean ExportExcel(String[] titleCode, String[] titles, List<Map<String, Object>> list, String sheetName, String filepath, HttpServletRequest request, HttpServletResponse response) { LinkedList<LinkedList> linkedLists = new LinkedList<>(); LinkedList<Object> objects = null; for (Map<String, Object> map : list) { for (String s : map.keySet()) { objects=new LinkedList<>(); objects.add(s); objects.add(map.get(s)); linkedLists.add(objects); } } BufferedOutputStream outputStream = null; try { // 创建一个excel文件 Workbook workbook = null; String fileType = filepath.substring(filepath.lastIndexOf(".") + 1, filepath.length()); response.setContentType("application/msexcel");// 设置文件类型 String userAgent = request.getHeader("USER-AGENT");// 文件名,包含后缀 if (StringUtils.contains(userAgent, "Chrome") || StringUtils.contains(userAgent, "Firefox")) { // google,火狐浏览器 filepath = new String((filepath).getBytes(), "ISO8859-1"); } else { filepath = URLEncoder.encode(filepath, "UTF8"); // 其他浏览器 } response.setHeader("Content-Disposition","attachment;filename="+filepath); outputStream = new BufferedOutputStream(response.getOutputStream()); if (fileType.equals("xls")) { workbook = new HSSFWorkbook(); } else if (fileType.equals("xlsx")) { workbook = new XSSFWorkbook(); } else { return false; // new NeieException(CommonExceptionDict.ParameterFail); // new CommonExceptionDict("BZ09040"); } // 在workbook中添加一个sheet,对应excel中的sheet // sheet名称可以自定义中文名 if (StringUtils.isBlank(sheetName)) sheetName = "sheet1"; Sheet sheet = workbook.createSheet(sheetName); // 隐藏第一列 // sheet.setColumnHidden(0, true); ExportInternalUtil internalUtil = new ExportInternalUtil(workbook, sheet); CellStyle headStyle = internalUtil.headStyle(); CellStyle bodyStyle = internalUtil.bodySryle(); Cell cell = null; if (titleCode != null && titleCode.length > 0) {// 设置导入时对应字段 Row hiddenRow = sheet.createRow(0); for (int i = 0; i < titleCode.length; i++) { cell = hiddenRow.createCell(i); cell.setCellValue(titleCode[i]); } // 隐藏当前行 hiddenRow.setZeroHeight(true); } // 构建表头 Row headRow = null; if (titleCode != null && titleCode.length > 0) {// 设置了对应字段 headRow = sheet.createRow(1); } else {// 无对应字段 headRow = sheet.createRow(0); } // 循环输出标题 for (int i = 0; i < titles.length; i++) { cell = headRow.createCell(i); cell.setCellStyle(headStyle); cell.setCellValue(titles[i]); } if (linkedLists != null && linkedLists.size() > 0) { // 构建表体数据 for (int i = 0; i < linkedLists.size(); i++) { Row bodyRow = null; if (titleCode != null && titleCode.length > 0) {// 有对应字段 bodyRow = sheet.createRow(i + 2); } else {// 无对应字段 bodyRow = sheet.createRow(i + 1); } List<Object> linked = linkedLists.get(i); for (int j = 0; j < linked.size(); j++) { cell = bodyRow.createCell(j); cell.setCellStyle(bodyStyle); cell.setCellValue(linked.get(j) != null ? linked.get(j).toString() : ""); } } } workbook.write(outputStream); outputStream.flush(); outputStream.close(); } catch (FileNotFoundException e1) { new FileNotFoundException(); // new AppException("BZ09042", "导出文件为::" + filepath == null ? "文件为空" : filepath); return false; } catch (IOException e) { new IOException(); // new AppException("BZ09001", e, ExportExcelUtil.class); return false; } finally { try { outputStream.close(); return true; } catch (IOException e) { new IOException(); // new AppException("BZ09050", e, ExportExcelUtil.class); } } return false; } } ---------- > 下面的的工具类是 给不同的人设计不同的 导出Excel表格样式 工具类的注释已经写了很详细了 ---------- import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; 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.ss.util.CellRangeAddress; /** * 设置Excle样式 * @author Wangbt * */ public class ExportInternalUtil { private Workbook workbook = null; private Sheet sheet = null; public ExportInternalUtil(Workbook workbook, Sheet sheet) { this.workbook = workbook; this.sheet = sheet; } /** * 合并单元格后给单元格加边框 * @param rangeAddress * @param cellStyle */ public void setRegionStyle(CellRangeAddress rangeAddress, CellStyle cellStyle) { int toprowNum = rangeAddress.getFirstRow(); for (int i = 0; i < toprowNum; i++) { Row row = sheet.getRow(i); for (int j = rangeAddress.getFirstColumn(); j <= rangeAddress.getLastColumn(); j++) { Cell cell = row.getCell(j); cell.setCellStyle(cellStyle); } } } /** * 设置表头单元格样式 * @return */ public CellStyle headStyle() { // 创建单元格样式 CellStyle cellStyle = workbook.createCellStyle(); // 设置单元格背景颜色为浅蓝色 cellStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index); cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); // 设置单元格居中对齐 cellStyle.setAlignment(CellStyle.ALIGN_CENTER); // 设置单元格垂直居中对齐 cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 设置单元格内不自动换行 cellStyle.setWrapText(true); // 设置单元格字体样式 Font font = workbook.createFont(); // 字体加粗 font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setFontName("仿宋"); font.setFontHeight((short)200); // 设置单元格边框为细线 cellStyle.setBorderLeft(CellStyle.BORDER_THIN); cellStyle.setBorderBottom(CellStyle.BORDER_THIN); cellStyle.setBorderRight(CellStyle.BORDER_THIN); cellStyle.setBorderTop(CellStyle.BORDER_THIN); return cellStyle; } /** * 设置表体单元格样式 * @return */ public CellStyle bodySryle() { // 创建单元格样式 CellStyle cellStyle = workbook.createCellStyle(); // 设置单元格背景颜色为白色 cellStyle.setFillForegroundColor(HSSFColor.WHITE.index); cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); // 设置单元格居中对齐 cellStyle.setAlignment(CellStyle.ALIGN_CENTER); // 设置单元格垂直居中对齐 cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 设置单元格内不自动换行 cellStyle.setWrapText(true); // 设置单元格字体样式 Font font = workbook.createFont(); // 默认字体 font.setBoldweight(Font.BOLDWEIGHT_NORMAL); font.setFontName("仿宋"); font.setFontHeight((short)200); // 设置单元格边框为细线 cellStyle.setBorderLeft(CellStyle.BORDER_THIN); cellStyle.setBorderBottom(CellStyle.BORDER_THIN); cellStyle.setBorderRight(CellStyle.BORDER_THIN); cellStyle.setBorderTop(CellStyle.BORDER_THIN); return cellStyle; } }

基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip 【备注】 1、该资源内项目代码百分百可运行,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值