| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- package com.goafanti.common.utils;
- import org.apache.poi.ss.usermodel.HorizontalAlignment;
- import org.apache.poi.ss.util.CellRangeAddress;
- import org.apache.poi.xssf.usermodel.XSSFCell;
- import org.apache.poi.xssf.usermodel.XSSFCellStyle;
- import org.apache.poi.xssf.usermodel.XSSFFont;
- import org.apache.poi.xssf.usermodel.XSSFRow;
- import org.apache.poi.xssf.usermodel.XSSFSheet;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- public class ExportExcelUtil {
-
- //设置单元格格式
- public static XSSFCellStyle setCellStyle1(XSSFWorkbook workbook){
- // 设置字体
- XSSFFont font = workbook.createFont();
- // font.setColor(HSSFFont.COLOR_RED);
- font.setFontName("微软雅黑");
- font.setFontHeightInPoints((short) 24);//字体大小
-
- // 设置样式
- XSSFCellStyle cellStyle = workbook.createCellStyle();
- cellStyle.setFont(font); //字体
-
- // 设置单元格内容水平对其方式
- // XSSFCellStyle.ALIGN_CENTER 居中对齐
- // XSSFCellStyle.ALIGN_LEFT 左对齐
- // XSSFCellStyle.ALIGN_RIGHT 右对齐
- cellStyle.setAlignment(HorizontalAlignment.CENTER);
- //设置自动换行
- cellStyle.setWrapText(true);
- return cellStyle;
- }
- public static XSSFCellStyle setCellStyle2(XSSFWorkbook workbook){
- // 设置字体
- XSSFFont font = workbook.createFont();
- // font.setColor(HSSFFont.COLOR_RED);
- font.setFontName("微软雅黑");
- font.setFontHeightInPoints((short) 12);//字体大小
-
- // 设置样式
- XSSFCellStyle cellStyle = workbook.createCellStyle();
- cellStyle.setFont(font); //字体
-
- // 设置单元格内容水平对其方式
- // XSSFCellStyle.ALIGN_CENTER 居中对齐
- // XSSFCellStyle.ALIGN_LEFT 左对齐
- // XSSFCellStyle.ALIGN_RIGHT 右对齐
- cellStyle.setAlignment(HorizontalAlignment.CENTER);
- //设置自动换行
- cellStyle.setWrapText(true);
- return cellStyle;
- }
-
-
- public static XSSFWorkbook exportTemplateInfoS(String[] comment,String fileName){
- //map 数据 ,comment表头 (列名称)
- //1,创建一个workbook,对应的是一个excel文件
- XSSFWorkbook wb = new XSSFWorkbook();
- //遍历map
- XSSFSheet sheet = wb.createSheet(fileName);
- //3。创建一个row
- XSSFRow row = sheet.createRow(0);
- //4.创建一个cell
- XSSFCell cell = row.createCell(0);
- cell.setCellValue(fileName);
- XSSFCellStyle style1 = ExportExcelUtil.setCellStyle1(wb);//标题
- XSSFCellStyle style2 = ExportExcelUtil.setCellStyle2(wb);//正文
- cell.setCellStyle(style1);
- //合并列
- sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, comment.length-1));//合并单元格
- //设置其他单位格式
- //创建表头
- XSSFRow row2 = sheet.createRow(1);
- //设置换行
- for(int i = 0; i < comment.length; i++){
- if (i==0) {
- sheet.setColumnWidth(i, 1000);//设置列宽
- }else {
- sheet.setColumnWidth(i, 3000);//设置列宽
- }
- XSSFCell c = row2.createCell(i);
- c.setCellValue(comment[i]);
- c.setCellStyle(style2);
- }
- return wb;
- }
- }
|