package com.ciic.hr.lzd.tools; import cn.hutool.core.util.RandomUtil; import com.ciic.hr.lzd.enterprise.dto.EnterpriseInformationIndexDTO; import com.ciic.hr.lzd.enterprise.dto.ExcelReturnDTO; import com.ciic.hr.lzd.enterprise.dto.SubTaskADindexDTO; import com.ciic.hr.lzd.enterprise.entity.EmployeeCompensationArrangement; import com.ciic.hr.lzd.enterprise.entity.EnterpriseDataPool; import com.ciic.hr.lzd.personalExcelUpload.entity.EtlLog; import com.ciic.hr.lzd.renshe.dto.ErrorDTO; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.poifs.crypt.EncryptionInfo; import org.apache.poi.poifs.crypt.EncryptionMode; import org.apache.poi.poifs.crypt.Encryptor; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellUtil; import org.apache.poi.xssf.streaming.SXSSFCell; import org.apache.poi.xssf.streaming.SXSSFRow; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.*; import java.io.*; import java.util.*; @SuppressWarnings("all") @Slf4j public class GenerateExcelTemplate { private static String testEnterpriseExport = System.getProperty("user.dir") + File.separator + "testEnterpriseExport"; public static void main(String[] args) throws IOException { String a = "数据年份,来源渠道,证券代码,股票简称,公司所在省份,公司所在城市,公司级别,上级企业,所属集团名称,组织机构代码/统一社会信用代码,法人姓名,公司地址,公司成立年份,是否为集团总部,企业规模,单位隶属关系,登记注册类型,组织形式,公司性质,填写其他公司性质,国有企业类别,投资国别,中智行业大类"; String[] strArray = a.split(","); String generateTemplatePath = System.getProperty("user.dir") + File.separator + "generateTemplate"; String model_name = "模板"; System.out.println("generateTemplatePath:" + generateTemplatePath); //createExcelXls(strArray, generateTemplatePath, model_name); //createExcelXlsx(strArray, generateTemplatePath); /*FileSystemView fsv = FileSystemView.getFileSystemView(); File path = fsv.getHomeDirectory(); System.out.println("path:" + path); String str = "错误类型编号,错误位置"; String[] arrStr = str.split(","); String errorExcelPath = System.getProperty("user.dir") + File.separator + "generateTemplate"; Map aa = new HashMap(); aa.put("type","T05"); aa.put("msg","此单元格数据不在字典表范围内"); Map bb = new HashMap(); bb.put("type","T05"); bb.put("msg","企业在本子任务已存在,请勿重复导入"); List list = new ArrayList(); list.add(aa); list.add(bb); createErrorExcel(arrStr,errorExcelPath,list);*/ } public static String createExcelXls(List list, String generateTemplatePath, String model_name) throws IOException { // 第一步,创建一个webbook,对应一个Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet HSSFSheet sheet = wb.createSheet("sheet1"); sheet.setDefaultColumnWidth(20);// 默认列宽 // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short //HSSFRow row = sheet.createRow((int) 0); // 第四步,创建单元格,并设置值表头 设置表头居中 HSSFCellStyle style = wb.createCellStyle(); // 创建一个居中格式 style.setAlignment(HorizontalAlignment.CENTER); // 设置背景颜色 style.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 设置边框 style.setBorderTop(BorderStyle.THIN); style.setBorderRight(BorderStyle.THIN); style.setBorderBottom(BorderStyle.THIN); style.setBorderLeft(BorderStyle.THIN); // 添加excel title // 单元格 for (int i = 0; i < list.size(); i++) { LinkedHashMap map = (LinkedHashMap) list.get(i); HSSFRow row = sheet.createRow(i); int j = 0; Iterator it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next(); String value = entry.getValue(); HSSFCell cell = row.createCell(j); cell.setCellType(CellType.STRING); cell.setCellValue(value); cell.setCellStyle(style); j++; } } // 第五步,将文件存到指定位置,返回文件路径以供下载 String newFileName = model_name + ".xls"; String path = generateTemplatePath + File.separator + newFileName; if (!new File(generateTemplatePath).exists()) { new File(generateTemplatePath).mkdirs(); } FileOutputStream fout = new FileOutputStream(new File(path)); //FileOutputStream fout = new FileOutputStream("D:/Backup/桌面/错误模板.xlsx"); wb.write(fout); fout.close(); return path; } public static String createExcelXlsx(List list, String model_name, String fileModelPath, String generateTemplatePath) throws IOException { // 从服务器上读取文件模板 //String fileModelPath = "D:/Backup/桌面/企业.xlsx"; FileInputStream fis = new FileInputStream(fileModelPath); // 创建写工作簿对象 XSSFWorkbook workBook = new XSSFWorkbook(fis); // 工作表 XSSFSheet sheet = workBook.getSheetAt(0); // 创建单元格,并设置值表头 设置表头居中 XSSFCellStyle style = workBook.createCellStyle(); // 创建一个居中格式 style.setAlignment(HorizontalAlignment.CENTER); // 设置背景颜色 style.setFillForegroundColor(new XSSFColor(new java.awt.Color(238, 161, 8))); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 设置边框 style.setBorderTop(BorderStyle.THIN); style.setBorderRight(BorderStyle.THIN); style.setBorderBottom(BorderStyle.THIN); style.setBorderLeft(BorderStyle.THIN); // 设置边框颜色 style.setTopBorderColor(new XSSFColor(new java.awt.Color(255, 255, 255))); style.setRightBorderColor(new XSSFColor(new java.awt.Color(255, 255, 255))); style.setBottomBorderColor(new XSSFColor(new java.awt.Color(255, 255, 255))); style.setLeftBorderColor(new XSSFColor(new java.awt.Color(255, 255, 255))); // 设置字体颜色 XSSFFont font = workBook.createFont(); font.setFontName("微软雅黑"); font.setBold(true); font.setColor(new XSSFColor(new java.awt.Color(255, 255, 255))); style.setFont(font); // 设置单元格宽度 sheet.setDefaultColumnWidth(20); // 第二行 XSSFCellStyle _style = workBook.createCellStyle(); // 设置背景颜色 _style.setFillForegroundColor(new XSSFColor(new java.awt.Color(0, 61, 126))); _style.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 添加excel title // 单元格 for (int i = 0; i < list.size(); i++) { LinkedHashMap map = (LinkedHashMap) list.get(i); int j = 11; Iterator it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next(); String value = StrTools.parseString(entry.getValue()); if (i == 0) { XSSFRow row = sheet.getRow(0); CellUtil.createCell(row, j, value); } else { XSSFRow row = sheet.getRow(1); XSSFRow _row = sheet.getRow(2);
Excel相关的导入导出的功能(原生poi编写)
最新推荐文章于 2024-07-20 13:59:34 发布