|
@@ -0,0 +1,586 @@
|
|
|
+package com.attendance.util;
|
|
|
+
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFCellStyle;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFColor;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class ExcelUtil {
|
|
|
+ /**
|
|
|
+ * 简单Excel导出
|
|
|
+ * @param title 标题
|
|
|
+ * @param list 数据
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+
|
|
|
+ public static String exportGeneralExcelByTitleAndList(String title, List<List<String>> list, String downloadPath) {
|
|
|
+ String result="系统提示:Excel文件导出成功!";
|
|
|
+ String fileName= title+".xlsx";
|
|
|
+ try {
|
|
|
+// response.reset();
|
|
|
+// response.setHeader("Content-disposition",
|
|
|
+// "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));
|
|
|
+// //设置文件头编码格式
|
|
|
+// response.setContentType("APPLICATION/OCTET-STREAM;charset=UTF-8");//设置类型
|
|
|
+// response.setHeader("Cache-Control","no-cache");//设置头
|
|
|
+// response.setDateHeader("Expires", 0);//设置日期头
|
|
|
+ // 创建工作簿, 换成XSSSF 来支持万以上的数据
|
|
|
+ SXSSFWorkbook workBook = new SXSSFWorkbook();
|
|
|
+// HSSFWorkbook workBook = new HSSFWorkbook();
|
|
|
+ // 创建工作类
|
|
|
+ Sheet sheet = workBook.createSheet();
|
|
|
+ //设置首行冻结
|
|
|
+ sheet.createFreezePane(0, 1);
|
|
|
+ sheet.setDefaultColumnWidth(16);
|
|
|
+ //设置字体样式
|
|
|
+ Font headFont = workBook.createFont();
|
|
|
+ headFont.setBold(true);
|
|
|
+ headFont.setFontHeightInPoints((short) 10);
|
|
|
+ headFont.setFontName("黑体");
|
|
|
+
|
|
|
+ Font titleFont = workBook.createFont();
|
|
|
+ titleFont.setBold(true);
|
|
|
+ titleFont.setFontHeightInPoints((short) 10);
|
|
|
+ titleFont.setFontName("黑体");
|
|
|
+
|
|
|
+ Font font = workBook.createFont();
|
|
|
+ font.setFontHeightInPoints((short) 10);
|
|
|
+ font.setFontName("宋体");
|
|
|
+
|
|
|
+ //设置单元格样式
|
|
|
+ XSSFCellStyle headStyle = (XSSFCellStyle) workBook.createCellStyle();
|
|
|
+ headStyle.setFont(headFont);
|
|
|
+ headStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ headStyle.setWrapText(true);
|
|
|
+ headStyle.setBorderBottom(BorderStyle.THIN); //下边框
|
|
|
+ headStyle.setBorderLeft(BorderStyle.THIN);//左边框
|
|
|
+ headStyle.setBorderTop(BorderStyle.THIN);//上边框
|
|
|
+ headStyle.setBorderRight(BorderStyle.THIN);//右边框
|
|
|
+
|
|
|
+ String color = "c0c0c0"; //此处得到的color为16进制的字符串
|
|
|
+ //转为RGB码
|
|
|
+ int r = Integer.parseInt((color.substring(0,2)),16); //转为16进制
|
|
|
+ int g = Integer.parseInt((color.substring(2,4)),16);
|
|
|
+ int b = Integer.parseInt((color.substring(4,6)),16);
|
|
|
+
|
|
|
+ //自定义cell颜色
|
|
|
+// HSSFPalette palette = workBook.getCustomPalette();
|
|
|
+ //这里的9是索引
|
|
|
+// palette.setColorAtIndex((short)9, (byte) r, (byte) g, (byte) b);
|
|
|
+
|
|
|
+ //设置自定义颜色
|
|
|
+ XSSFColor xssfColor = new XSSFColor();
|
|
|
+ byte[] colorRgb = { (short)9, (byte) r, (byte) g, (byte) b };
|
|
|
+ xssfColor.setRGB(colorRgb);
|
|
|
+
|
|
|
+ headStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
|
|
|
+ headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);// 填充模式(和背景颜色成对使用)
|
|
|
+ /*headStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex());*/ //设置自带的颜色
|
|
|
+
|
|
|
+ CellStyle titleStyle = workBook.createCellStyle();
|
|
|
+ titleStyle.setFont(titleFont);
|
|
|
+ titleStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); //填充单元格
|
|
|
+ titleStyle.setFillForegroundColor((short)9); //填色
|
|
|
+ titleStyle.setWrapText(true);
|
|
|
+ titleStyle.setBorderBottom(BorderStyle.THIN); //下边框
|
|
|
+ titleStyle.setBorderLeft(BorderStyle.THIN);//左边框
|
|
|
+ titleStyle.setBorderTop(BorderStyle.THIN);//上边框
|
|
|
+ titleStyle.setBorderRight(BorderStyle.THIN);//右边框
|
|
|
+
|
|
|
+ CellStyle cellStyle = workBook.createCellStyle();
|
|
|
+ cellStyle.setFont(font);
|
|
|
+ cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ cellStyle.setWrapText(true);
|
|
|
+ cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
|
|
|
+ cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
|
|
|
+ cellStyle.setBorderTop(BorderStyle.THIN);//上边框
|
|
|
+ cellStyle.setBorderRight(BorderStyle.THIN);//右边框
|
|
|
+
|
|
|
+ if(list.size() > 0) {
|
|
|
+ //标题(如果需要在EXCEL内容最上面加标题,请打开下面的注释,修改start)
|
|
|
+ /*
|
|
|
+ HSSFRow titleRow = sheet.createRow(0);
|
|
|
+ titleRow.setHeightInPoints(30);
|
|
|
+ HSSFCell titleCell = titleRow.createCell(0);
|
|
|
+ titleCell.setCellStyle(headStyle);
|
|
|
+ titleCell.setCellValue(title);
|
|
|
+ //合并单元格
|
|
|
+ CellRangeAddress cellRangeAddress = new CellRangeAddress(0,0,0, list.get(0).size() - 1);
|
|
|
+ //加入合并单元格对象
|
|
|
+ sheet.addMergedRegion(cellRangeAddress);
|
|
|
+ //使用RegionUtil类为合并后的单元格添加边框
|
|
|
+ RegionUtil.setBorderBottom(BorderStyle.THIN, cellRangeAddress, sheet); // 下边框
|
|
|
+ RegionUtil.setBorderLeft(BorderStyle.THIN, cellRangeAddress, sheet); // 左边框
|
|
|
+ RegionUtil.setBorderRight(BorderStyle.THIN, cellRangeAddress, sheet); // 有边框
|
|
|
+ RegionUtil.setBorderTop(BorderStyle.THIN, cellRangeAddress, sheet); // 上边框
|
|
|
+ */
|
|
|
+ int start = 0;
|
|
|
+ for(List<String> rowList : list) {
|
|
|
+ Row row = sheet.createRow(start);
|
|
|
+ row.setHeightInPoints(24);
|
|
|
+
|
|
|
+ for(int i = 0; i < rowList.size(); i++) {
|
|
|
+ Cell cell = row.createCell(i);
|
|
|
+ if(start == 0) {
|
|
|
+ cell.setCellStyle(headStyle);
|
|
|
+ }else {
|
|
|
+ cell.setCellStyle(cellStyle);
|
|
|
+ }
|
|
|
+ cell.setCellValue(rowList.get(i));
|
|
|
+ }
|
|
|
+ start++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //用于非传统ajax;
|
|
|
+// String headStr = "attachment; filename=\"" + fileName + "\"";
|
|
|
+// response.setContentType("APPLICATION/OCTET-STREAM");//返回格式为流
|
|
|
+// response.setHeader("Content-Disposition", headStr);
|
|
|
+// //普通下载不需要以上三行,注掉即可
|
|
|
+// OutputStream os = response.getOutputStream();//在线下载
|
|
|
+ File dir = null;
|
|
|
+ dir = new File(downloadPath);
|
|
|
+ // D://cloud/upload 文件上传后所存储的位置,部署到服务器上时配置服务器地址即可
|
|
|
+ if (!dir.exists()) {
|
|
|
+ dir.mkdirs();
|
|
|
+ }
|
|
|
+ FileOutputStream os = new FileOutputStream(downloadPath+fileName);//保存到本地
|
|
|
+ workBook.write(os);
|
|
|
+ os.flush();
|
|
|
+ os.close();
|
|
|
+ }catch(Exception e) {
|
|
|
+ System.out.println(result);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return "/upload/"+fileName;
|
|
|
+// return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String exportGeneralExcelByTitleAndList2(String title, List<List<String>> list, String downloadPath) {
|
|
|
+ String result="系统提示:Excel文件导出成功!";
|
|
|
+ String fileName= title+".xlsx";
|
|
|
+ try {
|
|
|
+// response.reset();
|
|
|
+// response.setHeader("Content-disposition",
|
|
|
+// "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));
|
|
|
+// //设置文件头编码格式
|
|
|
+// response.setContentType("APPLICATION/OCTET-STREAM;charset=UTF-8");//设置类型
|
|
|
+// response.setHeader("Cache-Control","no-cache");//设置头
|
|
|
+// response.setDateHeader("Expires", 0);//设置日期头
|
|
|
+ // 创建工作簿
|
|
|
+ SXSSFWorkbook workBook = new SXSSFWorkbook();
|
|
|
+// XSSFWorkbook workBook = new XSSFWorkbook();
|
|
|
+ // 创建工作类
|
|
|
+ Sheet sheet = workBook.createSheet();
|
|
|
+ //设置首行冻结
|
|
|
+ sheet.createFreezePane(0, 1);
|
|
|
+ sheet.setDefaultColumnWidth(16);
|
|
|
+ //设置字体样式
|
|
|
+ Font headFont = workBook.createFont();
|
|
|
+ headFont.setBold(true);
|
|
|
+ headFont.setFontHeightInPoints((short) 10);
|
|
|
+ headFont.setFontName("黑体");
|
|
|
+
|
|
|
+ Font titleFont = workBook.createFont();
|
|
|
+ titleFont.setBold(true);
|
|
|
+ titleFont.setFontHeightInPoints((short) 10);
|
|
|
+ titleFont.setFontName("黑体");
|
|
|
+
|
|
|
+ Font font = workBook.createFont();
|
|
|
+ font.setFontHeightInPoints((short) 10);
|
|
|
+ font.setFontName("宋体");
|
|
|
+
|
|
|
+ //设置单元格样式
|
|
|
+ XSSFCellStyle headStyle = (XSSFCellStyle) workBook.createCellStyle();
|
|
|
+ headStyle.setFont(headFont);
|
|
|
+ headStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ headStyle.setWrapText(true);
|
|
|
+ headStyle.setBorderBottom(BorderStyle.THIN); //下边框
|
|
|
+ headStyle.setBorderLeft(BorderStyle.THIN);//左边框
|
|
|
+ headStyle.setBorderTop(BorderStyle.THIN);//上边框
|
|
|
+ headStyle.setBorderRight(BorderStyle.THIN);//右边框
|
|
|
+
|
|
|
+ String color = "c0c0c0"; //此处得到的color为16进制的字符串
|
|
|
+ //转为RGB码
|
|
|
+ int r = Integer.parseInt((color.substring(0,2)),16); //转为16进制
|
|
|
+ int g = Integer.parseInt((color.substring(2,4)),16);
|
|
|
+ int b = Integer.parseInt((color.substring(4,6)),16);
|
|
|
+
|
|
|
+ //自定义cell颜色
|
|
|
+// HSSFPalette palette = workBook.getCustomPalette();
|
|
|
+ //这里的9是索引
|
|
|
+// palette.setColorAtIndex((short)9, (byte) r, (byte) g, (byte) b);
|
|
|
+
|
|
|
+ //设置自定义颜色
|
|
|
+ XSSFColor xssfColor = new XSSFColor();
|
|
|
+ byte[] colorRgb = { (byte)118, (byte)147, (byte)60 };
|
|
|
+ xssfColor.setRGB(colorRgb);
|
|
|
+ headStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
|
|
|
+ headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);// 填充模式(和背景颜色成对使用)
|
|
|
+ //cs.setFillForegroundColor(IndexedColors.AQUA.getIndex()); //设置自带的颜色
|
|
|
+
|
|
|
+
|
|
|
+ CellStyle titleStyle = workBook.createCellStyle();
|
|
|
+ titleStyle.setFont(titleFont);
|
|
|
+ titleStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); //填充单元格
|
|
|
+ titleStyle.setFillForegroundColor((short)9); //填色
|
|
|
+ titleStyle.setWrapText(true);
|
|
|
+ titleStyle.setBorderBottom(BorderStyle.THIN); //下边框
|
|
|
+ titleStyle.setBorderLeft(BorderStyle.THIN);//左边框
|
|
|
+ titleStyle.setBorderTop(BorderStyle.THIN);//上边框
|
|
|
+ titleStyle.setBorderRight(BorderStyle.THIN);//右边框
|
|
|
+
|
|
|
+ CellStyle cellStyle = workBook.createCellStyle();
|
|
|
+ cellStyle.setFont(font);
|
|
|
+ cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ cellStyle.setWrapText(true);
|
|
|
+ cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
|
|
|
+ cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
|
|
|
+ cellStyle.setBorderTop(BorderStyle.THIN);//上边框
|
|
|
+ cellStyle.setBorderRight(BorderStyle.THIN);//右边框
|
|
|
+
|
|
|
+ if(list.size() > 0) {
|
|
|
+ //标题(如果需要在EXCEL内容最上面加标题,请打开下面的注释,修改start)
|
|
|
+ /*
|
|
|
+ HSSFRow titleRow = sheet.createRow(0);
|
|
|
+ titleRow.setHeightInPoints(30);
|
|
|
+ HSSFCell titleCell = titleRow.createCell(0);
|
|
|
+ titleCell.setCellStyle(headStyle);
|
|
|
+ titleCell.setCellValue(title);
|
|
|
+ //合并单元格
|
|
|
+ CellRangeAddress cellRangeAddress = new CellRangeAddress(0,0,0, list.get(0).size() - 1);
|
|
|
+ //加入合并单元格对象
|
|
|
+ sheet.addMergedRegion(cellRangeAddress);
|
|
|
+ //使用RegionUtil类为合并后的单元格添加边框
|
|
|
+ RegionUtil.setBorderBottom(BorderStyle.THIN, cellRangeAddress, sheet); // 下边框
|
|
|
+ RegionUtil.setBorderLeft(BorderStyle.THIN, cellRangeAddress, sheet); // 左边框
|
|
|
+ RegionUtil.setBorderRight(BorderStyle.THIN, cellRangeAddress, sheet); // 有边框
|
|
|
+ RegionUtil.setBorderTop(BorderStyle.THIN, cellRangeAddress, sheet); // 上边框
|
|
|
+ */
|
|
|
+ int start = 0;
|
|
|
+ for(List<String> rowList : list) {
|
|
|
+ Row row = sheet.createRow(start);
|
|
|
+ row.setHeightInPoints(24);
|
|
|
+
|
|
|
+ for(int i = 0; i < rowList.size(); i++) {
|
|
|
+ Cell cell = row.createCell(i);
|
|
|
+ if(start == 0) {
|
|
|
+ cell.setCellStyle(headStyle);
|
|
|
+ }else {
|
|
|
+ cell.setCellStyle(cellStyle);
|
|
|
+ }
|
|
|
+ cell.setCellValue(rowList.get(i));
|
|
|
+ }
|
|
|
+ start++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //用于非传统ajax;
|
|
|
+// String headStr = "attachment; filename=\"" + fileName + "\"";
|
|
|
+// response.setContentType("APPLICATION/OCTET-STREAM");//返回格式为流
|
|
|
+// response.setHeader("Content-Disposition", headStr);
|
|
|
+// //普通下载不需要以上三行,注掉即可
|
|
|
+// OutputStream os = response.getOutputStream();//在线下载
|
|
|
+ File dir = null;
|
|
|
+ dir = new File(downloadPath);
|
|
|
+ // D://cloud/upload 文件上传后所存储的位置,部署到服务器上时配置服务器地址即可
|
|
|
+ if (!dir.exists()) {
|
|
|
+ dir.mkdirs();
|
|
|
+ }
|
|
|
+ FileOutputStream os = new FileOutputStream(downloadPath+fileName);//保存到本地
|
|
|
+ workBook.write(os);
|
|
|
+ os.flush();
|
|
|
+ os.close();
|
|
|
+ }catch(Exception e) {
|
|
|
+ System.out.println(result);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return "/upload/"+fileName;
|
|
|
+// return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String exportTwoSheetGeneralExcelByTitleAndList(String title, List<List<String>> sheetOneList,List<List<String>> sheetTwoList, String downloadPath,String sheetOneName,String sheetTwoName) {
|
|
|
+ String result="系统提示:Excel文件导出成功!";
|
|
|
+ String fileName= title+".xlsx";
|
|
|
+ try {
|
|
|
+ // 创建工作簿
|
|
|
+ SXSSFWorkbook workBook = new SXSSFWorkbook();
|
|
|
+ // 创建工作类
|
|
|
+ Sheet sheetOne = workBook.createSheet();
|
|
|
+ workBook.setSheetName(0,sheetOneName);
|
|
|
+ Sheet sheetTwo = workBook.createSheet();
|
|
|
+ workBook.setSheetName(1,sheetTwoName);
|
|
|
+ sheetOne.setDefaultColumnWidth(16);
|
|
|
+ sheetTwo.setDefaultColumnWidth(16);
|
|
|
+ //设置字体样式
|
|
|
+ Font headFont = workBook.createFont();
|
|
|
+ headFont.setBold(true);
|
|
|
+ headFont.setFontHeightInPoints((short) 10);
|
|
|
+ headFont.setFontName("黑体");
|
|
|
+
|
|
|
+ Font titleFont = workBook.createFont();
|
|
|
+ titleFont.setBold(true);
|
|
|
+ titleFont.setFontHeightInPoints((short) 10);
|
|
|
+ titleFont.setFontName("黑体");
|
|
|
+
|
|
|
+ Font font = workBook.createFont();
|
|
|
+ font.setFontHeightInPoints((short) 10);
|
|
|
+ font.setFontName("宋体");
|
|
|
+
|
|
|
+ //设置单元格样式
|
|
|
+ XSSFCellStyle headStyle = (XSSFCellStyle) workBook.createCellStyle();
|
|
|
+ headStyle.setFont(headFont);
|
|
|
+ headStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ headStyle.setWrapText(true);
|
|
|
+ headStyle.setBorderBottom(BorderStyle.THIN); //下边框
|
|
|
+ headStyle.setBorderLeft(BorderStyle.THIN);//左边框
|
|
|
+ headStyle.setBorderTop(BorderStyle.THIN);//上边框
|
|
|
+ headStyle.setBorderRight(BorderStyle.THIN);//右边框
|
|
|
+
|
|
|
+ String color = "c0c0c0"; //此处得到的color为16进制的字符串
|
|
|
+ //转为RGB码
|
|
|
+ int r = Integer.parseInt((color.substring(0,2)),16); //转为16进制
|
|
|
+ int g = Integer.parseInt((color.substring(2,4)),16);
|
|
|
+ int b = Integer.parseInt((color.substring(4,6)),16);
|
|
|
+
|
|
|
+ //自定义cell颜色
|
|
|
+// HSSFPalette palette = workBook.getCustomPalette();
|
|
|
+ //这里的9是索引
|
|
|
+// palette.setColorAtIndex((short)9, (byte) r, (byte) g, (byte) b);
|
|
|
+
|
|
|
+ //设置自定义颜色
|
|
|
+ XSSFColor xssfColor = new XSSFColor();
|
|
|
+ byte[] colorRgb = { (byte)118, (byte)147, (byte)60 };
|
|
|
+ xssfColor.setRGB(colorRgb);
|
|
|
+ headStyle.setFillForegroundColor(xssfColor);
|
|
|
+ headStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
|
|
|
+ headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);// 填充模式(和背景颜色成对使用)
|
|
|
+ //cs.setFillForegroundColor(IndexedColors.AQUA.getIndex()); //设置自带的颜色
|
|
|
+
|
|
|
+ CellStyle titleStyle = workBook.createCellStyle();
|
|
|
+ titleStyle.setFont(titleFont);
|
|
|
+ titleStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); //填充单元格
|
|
|
+ titleStyle.setFillForegroundColor((short)9); //填色
|
|
|
+ titleStyle.setWrapText(true);
|
|
|
+ titleStyle.setBorderBottom(BorderStyle.THIN); //下边框
|
|
|
+ titleStyle.setBorderLeft(BorderStyle.THIN);//左边框
|
|
|
+ titleStyle.setBorderTop(BorderStyle.THIN);//上边框
|
|
|
+ titleStyle.setBorderRight(BorderStyle.THIN);//右边框
|
|
|
+
|
|
|
+ CellStyle cellStyle = workBook.createCellStyle();
|
|
|
+ cellStyle.setFont(font);
|
|
|
+ cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ cellStyle.setWrapText(true);
|
|
|
+ cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
|
|
|
+ cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
|
|
|
+ cellStyle.setBorderTop(BorderStyle.THIN);//上边框
|
|
|
+ cellStyle.setBorderRight(BorderStyle.THIN);//右边框
|
|
|
+
|
|
|
+ if(sheetOneList.size() > 0) {
|
|
|
+ //标题(如果需要在EXCEL内容最上面加标题,请打开下面的注释,修改start)
|
|
|
+ /*
|
|
|
+ HSSFRow titleRow = sheet.createRow(0);
|
|
|
+ titleRow.setHeightInPoints(30);
|
|
|
+ HSSFCell titleCell = titleRow.createCell(0);
|
|
|
+ titleCell.setCellStyle(headStyle);
|
|
|
+ titleCell.setCellValue(title);
|
|
|
+ //合并单元格
|
|
|
+ CellRangeAddress cellRangeAddress = new CellRangeAddress(0,0,0, list.get(0).size() - 1);
|
|
|
+ //加入合并单元格对象
|
|
|
+ sheet.addMergedRegion(cellRangeAddress);
|
|
|
+ //使用RegionUtil类为合并后的单元格添加边框
|
|
|
+ RegionUtil.setBorderBottom(BorderStyle.THIN, cellRangeAddress, sheet); // 下边框
|
|
|
+ RegionUtil.setBorderLeft(BorderStyle.THIN, cellRangeAddress, sheet); // 左边框
|
|
|
+ RegionUtil.setBorderRight(BorderStyle.THIN, cellRangeAddress, sheet); // 有边框
|
|
|
+ RegionUtil.setBorderTop(BorderStyle.THIN, cellRangeAddress, sheet); // 上边框
|
|
|
+ */
|
|
|
+ int start = 0;
|
|
|
+ for(List<String> rowList : sheetOneList) {
|
|
|
+ Row row = sheetOne.createRow(start);
|
|
|
+ row.setHeightInPoints(24);
|
|
|
+ for(int i = 0; i < rowList.size(); i++) {
|
|
|
+ Cell cell = row.createCell(i);
|
|
|
+ if(start == 0) {
|
|
|
+ cell.setCellStyle(headStyle);
|
|
|
+ }else {
|
|
|
+ cell.setCellStyle(cellStyle);
|
|
|
+ }
|
|
|
+ cell.setCellValue(rowList.get(i));
|
|
|
+ }
|
|
|
+ start++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(sheetTwoList.size()>0){
|
|
|
+ int start = 0;
|
|
|
+ for(List<String> rowList : sheetTwoList) {
|
|
|
+ Row row = sheetTwo.createRow(start);
|
|
|
+ row.setHeightInPoints(24);
|
|
|
+ for(int i = 0; i < rowList.size(); i++) {
|
|
|
+ Cell cell = row.createCell(i);
|
|
|
+ if(start == 0) {
|
|
|
+ cell.setCellStyle(titleStyle);
|
|
|
+ }else {
|
|
|
+ cell.setCellStyle(cellStyle);
|
|
|
+ }
|
|
|
+ cell.setCellValue(rowList.get(i));
|
|
|
+ }
|
|
|
+ start++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ File dir = null;
|
|
|
+ dir = new File(downloadPath);
|
|
|
+ if (!dir.exists()) {
|
|
|
+ dir.mkdirs();
|
|
|
+ }
|
|
|
+ FileOutputStream os = new FileOutputStream(downloadPath+fileName);//保存到本地
|
|
|
+ workBook.write(os);
|
|
|
+ os.flush();
|
|
|
+ os.close();
|
|
|
+ }catch(Exception e) {
|
|
|
+ System.out.println(result);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return "/upload/"+fileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String exportMultiSheetGeneralExcelByTitleAndList(String title, List<List<String>>[] multiSheetList, String downloadPath,String[] sheetsName) {
|
|
|
+ String result="系统提示:Excel文件导出成功!";
|
|
|
+ String fileName= title+".xlsx";
|
|
|
+ try {
|
|
|
+ // 创建工作簿
|
|
|
+ SXSSFWorkbook workBook = new SXSSFWorkbook();
|
|
|
+ // 创建工作类
|
|
|
+ for (int sheetIndex=0;sheetIndex<multiSheetList.length; sheetIndex++) {
|
|
|
+ Sheet sheetOne = workBook.createSheet();
|
|
|
+ workBook.setSheetName(sheetIndex,sheetsName[sheetIndex]);
|
|
|
+ sheetOne.setDefaultColumnWidth(16);
|
|
|
+
|
|
|
+ //设置字体样式
|
|
|
+ Font headFont = workBook.createFont();
|
|
|
+ headFont.setBold(true);
|
|
|
+ headFont.setFontHeightInPoints((short) 10);
|
|
|
+ headFont.setFontName("黑体");
|
|
|
+
|
|
|
+ Font titleFont = workBook.createFont();
|
|
|
+ titleFont.setBold(true);
|
|
|
+ titleFont.setFontHeightInPoints((short) 10);
|
|
|
+ titleFont.setFontName("黑体");
|
|
|
+
|
|
|
+ Font font = workBook.createFont();
|
|
|
+ font.setFontHeightInPoints((short) 10);
|
|
|
+ font.setFontName("宋体");
|
|
|
+
|
|
|
+ //设置单元格样式
|
|
|
+ XSSFCellStyle headStyle = (XSSFCellStyle) workBook.createCellStyle();
|
|
|
+ headStyle.setFont(headFont);
|
|
|
+ headStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ headStyle.setWrapText(true);
|
|
|
+ headStyle.setBorderBottom(BorderStyle.THIN); //下边框
|
|
|
+ headStyle.setBorderLeft(BorderStyle.THIN);//左边框
|
|
|
+ headStyle.setBorderTop(BorderStyle.THIN);//上边框
|
|
|
+ headStyle.setBorderRight(BorderStyle.THIN);//右边框
|
|
|
+
|
|
|
+ String color = "c0c0c0"; //此处得到的color为16进制的字符串
|
|
|
+ //转为RGB码
|
|
|
+ int r = Integer.parseInt((color.substring(0,2)),16); //转为16进制
|
|
|
+ int g = Integer.parseInt((color.substring(2,4)),16);
|
|
|
+ int b = Integer.parseInt((color.substring(4,6)),16);
|
|
|
+
|
|
|
+ //自定义cell颜色
|
|
|
+// HSSFPalette palette = workBook.getCustomPalette();
|
|
|
+ //这里的9是索引
|
|
|
+// palette.setColorAtIndex((short)9, (byte) r, (byte) g, (byte) b);
|
|
|
+
|
|
|
+ //设置自定义颜色
|
|
|
+ XSSFColor xssfColor = new XSSFColor();
|
|
|
+ byte[] colorRgb = { (byte)118, (byte)147, (byte)60 };
|
|
|
+ xssfColor.setRGB(colorRgb);
|
|
|
+ headStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
|
|
|
+ headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);// 填充模式(和背景颜色成对使用)
|
|
|
+ //cs.setFillForegroundColor(IndexedColors.AQUA.getIndex()); //设置自带的颜色
|
|
|
+
|
|
|
+ CellStyle titleStyle = workBook.createCellStyle();
|
|
|
+ titleStyle.setFont(titleFont);
|
|
|
+ titleStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); //填充单元格
|
|
|
+ titleStyle.setFillForegroundColor((short)9); //填色
|
|
|
+ titleStyle.setWrapText(true);
|
|
|
+ titleStyle.setBorderBottom(BorderStyle.THIN); //下边框
|
|
|
+ titleStyle.setBorderLeft(BorderStyle.THIN);//左边框
|
|
|
+ titleStyle.setBorderTop(BorderStyle.THIN);//上边框
|
|
|
+ titleStyle.setBorderRight(BorderStyle.THIN);//右边框
|
|
|
+
|
|
|
+ CellStyle cellStyle = workBook.createCellStyle();
|
|
|
+ cellStyle.setFont(font);
|
|
|
+ cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ cellStyle.setWrapText(true);
|
|
|
+ cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
|
|
|
+ cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
|
|
|
+ cellStyle.setBorderTop(BorderStyle.THIN);//上边框
|
|
|
+ cellStyle.setBorderRight(BorderStyle.THIN);//右边框
|
|
|
+
|
|
|
+ if(multiSheetList[sheetIndex].size() > 0) {
|
|
|
+ int start = 0;
|
|
|
+ for(List<String> rowList : multiSheetList[sheetIndex]) {
|
|
|
+ Row row = sheetOne.createRow(start);
|
|
|
+ row.setHeightInPoints(24);
|
|
|
+ for(int i = 0; i < rowList.size(); i++) {
|
|
|
+ Cell cell = row.createCell(i);
|
|
|
+ if(start == 0) {
|
|
|
+ cell.setCellStyle(headStyle);
|
|
|
+ }else {
|
|
|
+ cell.setCellStyle(cellStyle);
|
|
|
+ }
|
|
|
+ cell.setCellValue(rowList.get(i));
|
|
|
+ }
|
|
|
+ start++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ File dir = new File(downloadPath);
|
|
|
+ if (!dir.exists()) {
|
|
|
+ dir.mkdirs();
|
|
|
+ }
|
|
|
+ FileOutputStream os = new FileOutputStream(downloadPath+fileName);//保存到本地
|
|
|
+ workBook.write(os);
|
|
|
+ os.flush();
|
|
|
+ os.close();
|
|
|
+ }catch(Exception e) {
|
|
|
+ System.out.println(result);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return "/upload/"+fileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isRowEmpty(Row row){
|
|
|
+ for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
|
|
|
+ Cell cell = row.getCell(i);
|
|
|
+ if (cell != null && cell.getCellTypeEnum() != CellType.BLANK){
|
|
|
+// System.out.println(i+":"+cell.getCellTypeEnum());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+}
|