java导出下载Excel文件的一种方式之一poi
发布日期:2021-06-29 11:46:58
浏览次数:2
分类:技术文章
本文共 5731 字,大约阅读时间需要 19 分钟。
service层
public void hisSoeExport(HttpServletResponse response, String beginTime, String endTime, String facId, String groupId, String typeId) { ExportParams exportParams = new ExportParams("告警报表", "告警数据"); exportParams.setCreateHeadRows(true); exportParams.setStyle(ExcelExportStylerBorderImpl.class); Workbook workbook = ExcelExportUtil.exportExcel(exportParams, HisDataSoe.class, getSoe(beginTime, endTime, facId, groupId, typeId)); try { ExportUtils.downloadEexcl(response, "告警", workbook); } catch (IOException e) { e.printStackTrace(); throw new HZException("HZ9999", "execl下载发生异常"); } }
导出工具类
package xxx.utils;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletResponse;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.ss.util.CellRangeAddress;public class ExportUtils { public static void downloadEexcl(HttpServletResponse response, String fileName, Workbook workbook) throws IOException { response.addHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"), "iso8859-1") + ".xls"); // 定义输出类型 response.setContentType("octets/stream;rset=UTF-8"); // 创建输出对象 ServletOutputStream out = response.getOutputStream(); // 工作簿输出 workbook.write(out); // 关闭输出流 out.close(); } /** * 去掉所有合并的单元格 * * @param sheet */ public static void removeMergedRegion(Sheet sheet) { int num = sheet.getNumMergedRegions(); while (num > 0) { sheet.removeMergedRegion(0); num = sheet.getNumMergedRegions(); } } /** * 自适应单元格 * * @param sheet * @param colNum * @param value * @param offset */ public static void adjustColumn(Sheet sheet, int colNum, String value, int offset) { int columnWidth = sheet.getColumnWidth(colNum); int valueWidth = value.getBytes().length * 256; if (valueWidth > columnWidth) { sheet.setColumnWidth(colNum, valueWidth + offset); } } /** * 复制整行 * * @param oldRow * @param newRow * @param startCol * @param endCol */ @SuppressWarnings("deprecation") public static void copyRow(Sheet sheet, Row oldRow, Row newRow) { int startCol = oldRow.getFirstCellNum(); int endCol = oldRow.getLastCellNum(); if (startCol >= 0 && endCol >= 0) { for (int i = startCol; i <= endCol; i++) { Cell oldCell = oldRow.getCell(i); Cell newCell = newRow.createCell(i); if (null == oldCell) { continue; } int type = oldCell.getCellType(); newCell.setCellType(type); switch (type) { case Cell.CELL_TYPE_BLANK: break; case Cell.CELL_TYPE_STRING: newCell.setCellValue(oldCell.getStringCellValue()); break; case Cell.CELL_TYPE_BOOLEAN: newCell.setCellValue(oldCell.getBooleanCellValue()); break; case Cell.CELL_TYPE_NUMERIC: newCell.setCellValue(oldCell.getNumericCellValue()); break; case Cell.CELL_TYPE_FORMULA: newCell.setCellValue(oldCell.getCellFormula()); break; default: newCell.setCellValue(oldCell.getStringCellValue()); break; } newCell.setCellStyle(oldCell.getCellStyle()); String value = oldCell.getStringCellValue(); HisDataExportUtils.adjustColumn(sheet, i, value, 100); oldCell.setCellValue(""); oldCell.setCellType(Cell.CELL_TYPE_BLANK); } } } /** * 剪裁除去 * * @param sheet * @param rowNum * @param columnNum */ public static void clipSheet(Sheet sheet, int rowNum, int columnNum) { int rowTotal = sheet.getLastRowNum(); while (rowNum < rowTotal) { sheet.removeRow(sheet.getRow(rowNum + 1)); rowTotal = sheet.getLastRowNum(); } for (int i = 0; i <= rowTotal; i++) { Row row = sheet.getRow(i); int columnTotal = row.getLastCellNum(); for (int j = columnNum + 1; j <= columnTotal; j++) { Cell cell = row.getCell(j); if (null != cell) { row.removeCell(cell); } } } } /** * 修改表头 * * @param sheet * @param columNum * @param time */ public static void editHead(Sheet sheet, int columNum, String time, String title,String unit) { if (null != title) { Row titleRow = sheet.getRow(0); Cell titleCell = titleRow.getCell(0); titleCell.setCellValue(title); } if(null != unit){ Row titleRow = sheet.getRow(1); Cell titleCell = titleRow.getCell(1); titleCell.setCellValue(unit); } sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, columNum)); Row dateRow = sheet.getRow(1); Cell dateCell = dateRow.getCell(0); String dateStr = dateCell.getStringCellValue(); dateStr += time; dateCell.setCellValue(dateStr); Cell unitCell = dateRow.getCell(1); Cell newCell = dateRow.createCell(columNum - 1); newCell.setCellStyle(unitCell.getCellStyle()); newCell.setCellValue(unitCell.getStringCellValue()); unitCell.setCellValue(""); sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, columNum - 2)); sheet.addMergedRegion(new CellRangeAddress(1, 1, columNum - 1, columNum)); } /** * 输入流存储到文件 * * @param inputStream * @param file * @throws IOException */ public static void inputStreamToFile(InputStream inputStream, File file) throws IOException { File parentDir = file.getParentFile(); if (!parentDir.exists()) { parentDir.mkdirs(); } file.createNewFile(); BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file)); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); int len = -1; byte[] bytes = new byte[1024]; while ((len = bufferedInputStream.read(bytes)) != -1) { outputStream.write(bytes, 0, len); } bufferedInputStream.close(); outputStream.close(); }}
转载地址:https://blog.csdn.net/zzchances/article/details/105052381 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2024年04月13日 01时08分03秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
光立方,永远的神!
2019-04-29
学习STM32很简单?
2019-04-29
电赛 | 电源题软件如何准备?
2019-04-29
手把手教你DIY一款属于自己的万能红外遥控器!
2019-04-29
速看 | 电子元器件如何确定好坏?
2019-04-29
485通信自动收发电路,历史上最详细的解释
2019-04-29
【视觉盛宴三】不好意思,这些线材接口的横截面真的没见过
2019-04-29
一位头发发白的神人教你怎么写程序,运维,买电脑,写文章,平面设计!
2019-04-29
【第二期】那些设计漂亮、有创意的电路板!
2019-04-29
【第三期】那些设计漂亮、有创意的电路板!
2019-04-29
继续推荐公众号~
2019-04-29
「第二篇」全国一等奖,经验帖。
2019-04-29
「第三篇」全国电子设计竞赛,这些你必须知道的比赛细节,文末附上近十年电赛题目下载...
2019-04-29
5G小科普(漫画版,So easy!)
2019-04-29
无人再提华强北
2019-04-29
千万不要小瞧那些不好好写代码的程序员
2019-04-29
80后,天才程序员, Facebook 第一任 CTO,看看开挂的人生到底有多变态?
2019-04-29
「第四篇」电赛控制题可以准备一些什么?
2019-04-29
「第五篇」全国电子设计竞赛-电源题设计方案总结
2019-04-29
「第六篇」对于电赛,我们应该看重什么?
2019-04-29