java实现文件导出到Excel


导出excel功能:
//首先把要导出文件的模板放到项目默认的地址下,(也就是webapp地址下)

Controller

    @RequestMapping("value="")
    //导出方法
    public String getExportExcel(HttpServletRequest request, HttpSeiions session,
                    HttpServletResponse response,//包括前台传过来的参数(自己定义) ){
        log.info("--------------正在导出文件");
        //获取模板的位置,以及要导出的路径
        //模板的位置
        String modelPath = session.getServletContext().getRealPath("/")+"模板的位置.xlsx";
        //导出的路径地址
        String hisPath = session.getServletContext().getRealPath("/")+"导出的路径";
        
     //相当于模板对象    
     Workbook book;    
     try{
        //获取模板文件
        File excelFile = new File(modelPath);
        book = WorkbookFactory.creat(excelFile);
        
        //获取你要导出的信息,将其封装为list对象(就是业务逻辑处理的结果集)
        //List<封装的实体类> list = 你的service层.service方法(前台传的参数);
        如:List<AmReportFormSalePriceSection> amReportingList = amReportingService.getSalePriceSectionList(propertyLevel, priceSection, wslName);
        //你的service层.导出的方法(book, list, hisPath);
        如:amReportingService.getReportFormSalePriceSectionExcel(book, amReportingList, hisPath);
    
     //如果你要导出的excel是多个sheet组成的,并且在导出后只显示当前的某一个sheet,
     //其他sheet都默认删除,则执行下面的遍历删除,如果不需要则不用执行
     
     /**
     * for(sheet的数量遍历){
     *    if(当不是当前显示的sheet则进行删除){
     *        book.removeSheetAt(遍历序号);  
     * }
     */
     
     
     //设置导出格式
     response.setCharacterEncoding("utg-8");
     //设置文件ContentType类型,这样设置会自动判断下载文件类型
     response.setContentType("multipart/form-data");
     //设置文件头;最后一个参数是设置下载文件名
     response.setHeader("Content-Disposition", "attachment;fileName="+new String("导出文件的名称".getBytes("UTF-8"),"ISO-8859-1")+".xlsx");
     response.setHeader("Pragma","No-cache");
     OutputStream out = response.getOutputStream();
     book.write(out);
     out.flush();
     out.close();
    }catch(Exception e){
         log.error("发生异常 msg={}","原因:",e);
         return   导出失败信息;
     }
      return 导出成功信息;
    }
    
    
    
Service实现类:

   @Override
   public void getExportExcel(Workbook book, List<AmReportFormSalePriceSection> amReportingList,
            String hisPath){
        try{
           //获取要导出的excel模板中的该sheet(也就是当前要导出的sheet的下标(一般都以零为第一个))
           Sheet sheetArea = book.getSheetAt(6);  //假设现在要导出的sheet是在模板中的第七个
           int rowNum = 10;  //在要导出的sheet中,导出数据是从该sheet中的第几行开始数据导出的(起始下标也是从零开始)
           Row dkCopyRow = sheetArea.getRow(10);
           //开始遍历导出的数据,进行数据导出
           while(amReportingList != null && amReportingList.size() > 0){
              //由于导出数据过多,则进行分页导出,每页2000条内,所以要设置一个临时集合存放当前导出的数据(每一页)
              List<AmReportFormSalePriceSection> subList = amReportingList.subList(0,Math.min(2000,amReportingList.size()));
              for(AmReportFormSalePriceSection reportForm : subList){
                Row trow = sheetArea.getRow(rowNum);
                if(trow == null){
                   trow = sheetArea.createRow(rowNum);
                   ExcelUtil.copyRow(book, dkCopyRow, trow);
                }
                 //service实现类中的方法(实现单挑要导出的数据填入excel中的一行)
                 seRowValueOfSheet(reportForm, trow, 10);
                 rowNum++;
              }
              //进行释放该集合
              subList.clear(Exception e);
           }
        }catch(Exception e){
            log.error("发生异常 msg={}", "原因:", e);
            throw new RuntimeException(e);
        }
   }
   
   
    //service实现类中的方法(实现单条要导出的数据填入excel中的一行)
    public void seRowValueOfSheet(Object o, Row row, int sheetNum){
        //getDeclareFields()方法返回该类中所有的字段包括共有的和私有的
        //getFieds()方法是返回该类对应的公共类字段
        List<Field> list = Arrays.asList(o.getClass().getDeclareFields());
        for(int i = 0; i < list.size(); i++){
            Field field = list.get(i);
            if(field.isAnnotationPresent(ImportSign.class)){
                //ImportSign是单独建立的一个有关导出注解的类具体在下面(@注释)
                ImportSign importSign =  field.getAnnotation(ImportSign.class);
                int cellNum = importSign.cellNum();
                if(cellNum == -1){
                   continue;
                }
                Cell cell = row.getCell(cellNum);
                if(cell == null){
                    cell = row.createCell(cellNum);
                    CellStyle cellStyle = cell.getCellStyle();
                    cellStyle.setClocked(false);
                    cellStyle.setBorderTop(BorderStyle.THIN);
                    cellStyle.setBorderBottom(BorderStyle.THIN);
                    cellStyle.setBorderLeft(BorderStyle.THIN);
                    cellStyle.setBorderRight(BorderStyle.THIN);
                }
                //获取该字段的数据类型
                String fieldTypeName = field.getType().getName();
                try{
                    //获取该字段的编码
                    String fileName = field.getName();
                    //通过该字段来获取该字段的get方法
                    Method method = o.getClass().getMethod(
                    "get" + fileName.subString(0,1).toUpperCase() + fileName.subString(1));
                    //执行该字段的get方法,获取该字段的值
                    Object v = method.invoke(o);
                    if (null != v) {
                        if (fieldTypeName.) {
                           cell.setCellValue(v.toString());
                        }else if (fieldTypeName.contains("BigDecimal")) {
                            BigDecimal b = new BigDecimal(v.toString());
                            cell.setCellValue(b.doubleValue());
                        } else if (fieldTypeName.contains("Date")) {
                            SimpleDateFormat sd = new SimpleDateFormat("yyyy/MM/dd");
                            cell.setCellValue(sd.format(new Date(v.toString())));
                        }
                    }
                }catch(Exception e){
                    log.error("发生异常 msg={}", "原因:", e);
                }
            }
        }
    }
   
   

 ExcelUtil类

 public class  ExcelUtil [
    /**
    *行复制功能
    */
    public static void copyRow(Workbook wb, Row fromRow, Row toRow) {
       toRow.setHeight(fromRow.getHeight());
       CellStyle newStyle = wb.createCellStyle();
       for(Iterator<Cell> cellTt = fromRow.cellIterator(); cellTt.hasNext();) {
          Cell tmpCell = (Cell) cellTr.next();
          Cell newCell = toRow.createCell(tmpCell.getColumnIndex());
          copyCell(wb, newStyle, tmpCell, newCell);
       }
       Sheet worksheet = fromRow.getSheet();
       for(int i = 0; i < worksheet.getNumMergedRegions(); i++) {
           CellRangeAddress cellRangeAndress = worksheet.getMergedRegion(i);
           if(cellRangeAddress.getFirstRow() == fromRow.getRowNum()) {
               CellRangeAddress newCellRangeAddress = new CellRangeAddress(toRow.getRowNum(), (toRow.getRowNum() +
                        (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow())), cellRangeAddress
                        .getFirstColumn(), cellRangeAddress.getLastColumn());
                worksheet.addMergedRegionUnsafe(newCellRangeAddress);
           }
       }
    } 

   
   
   /**
   *复制单元格
   * @param srcCell
   * @param distCell
   * @param //copyValueFlag true则连同cell的内容一起复制
   */
    public static void copyCell(Workbook wb,CellStyle newStyle, Cell srcCell, Cell distCell) {
        CellStyle srcStyle = srcCell.getCellStyle();
        newStyle.cloneStyleFrom(srcStyle);
        newStyle.setFont(wb.getFontAt(srcStyle.getFontIndex()));
        newStyle.setLocked(false);
        newStyle.setBorderTop(BorderStyle.THIN);
        newStyle.setBorderBottom(BorderStyle.THIN);
        newStyle.setBorderLeft(BorderStyle.THIN);
        newStyle.setBorderRight(BorderStyle.THIN);
        //样式
        distCell.setCellStyle(newStyle);
        //评论
        if(srcCell.getCellComment() != null) {
            distCell.setCellComment(srcCell.getCellComment());
        }
        // 不同数据类型处理
        CellType srcCellType = srcCell.getCellTypeEnum();
        distCell.setCellType(srcCellType);
        if(srcCellType == CellType.NUMERIC) {
            if(org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(srcCell)) {
                distCell.setCellValue(srcCell.getDateCellValue());
            } else {
                distCell.setCellValue(srcCell.getNumericCellValue());
            }
        } else if(srcCellType == CellType.STRING) {
            distCell.setCellValue(srcCell.getRichStringCellValue());
        } else if(srcCellType == CellType.BLANK) {

        } else if(srcCellType == CellType.BOOLEAN) {
            distCell.setCellValue(srcCell.getBooleanCellValue());
        } else if(srcCellType == CellType.ERROR) {
            distCell.setCellErrorValue(srcCell.getErrorCellValue());
        } else if(srcCellType == CellType.FORMULA) {
            distCell.setCellFormula(srcCell.getCellFormula());
        } else {
        }
    }
   
 }
   
   
   @注释:
        @Documented
        @Target({ElementType.FIELD})
        @Retention(RetentionPolicy.RUNTIME)
        public @interface ImportSign {
            int cellNum() default -1;//excel 列序号
            boolean isnull() default false;//是否可以为空,默认不可以为空
            boolean isYn() default false;//单元格是否为是/否值,如果等于true时自动将是转换成1,否转换成0
            boolean isDict() default false;//单元格是否为数据字典值,如果等于true时,则需要转换
            int columnLength() default 0;    //单元格值字节长度
            String dictGroupCode() default "";//当isDict为true时,设置对应的字典组编码
            String description() default "";//描述
        }
   
   在实现导出功能的时候,我们使用字段加注解的方式)
   在实体类中需要进行导出的字段上加注解
   如:
    @ImportSign(cellNum = 1, isnull = true, description = "区域名称", columnLength = 50)
    private String areaName;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Apache POI API来实现。可以参考以下代码来实现批量导出excel文件:Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1");// 创建行 Row row = sheet.createRow(0);// 创建单元格 Cell cell = row.createCell(0); cell.setCellValue("Data");// 将生成的excel文件输出到磁盘 FileOutputStream fos = new FileOutputStream("test.xlsx"); workbook.write(fos); fos.close(); ### 回答2: 要使用Java实现批量导出Excel文件,可以使用Apache POI库来操作Excel文件。下面是一个简单的示例代码: ```java // 导入必需的POI库 import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class ExcelExporter { public static void main(String[] args) { List<String[]> data = new ArrayList<>(); data.add(new String[]{"姓名", "年龄", "性别"}); data.add(new String[]{"张三", "26", "男"}); data.add(new String[]{"李四", "30", "女"}); exportToExcel(data, "output.xlsx"); } public static void exportToExcel(List<String[]> data, String filename) { // 创建新的工作簿 Workbook workbook = new XSSFWorkbook(); // 创建新的工作表 Sheet sheet = workbook.createSheet(); // 写入数据 for (int i = 0; i < data.size(); i++) { Row row = sheet.createRow(i); String[] rowData = data.get(i); for (int j = 0; j < rowData.length; j++) { Cell cell = row.createCell(j); cell.setCellValue(rowData[j]); } } // 保存到文件 try { FileOutputStream outputStream = new FileOutputStream(filename); workbook.write(outputStream); workbook.close(); outputStream.close(); System.out.println("导出成功!"); } catch (IOException e) { System.out.println("导出失败:" + e.getMessage()); } } } ``` 以上代码使用Apache POI库创建了一个包含数据的Excel文件,并保存到磁盘中。在示例中,使用了`exportToExcel`方法将数据导出Excel文件中,并指定了文件名为`output.xlsx`。导出Excel文件包含三列数据,每列数据分别为姓名、年龄和性别。可以根据实际需要修改导出的数据。最后,通过调用`workbook.write(outputStream)`保存Excel文件,并关闭资源。如果导出成功,控制台会打印"导出成功!",否则会打印具体的错误信息。 ### 回答3: Java实现批量导出Excel文件的代码可以使用Apache POI库进行操作。下面是一个简单的示例代码: 1. 导入所需的POI库: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; ``` 2. 创建一个方法来导出Excel文件: ```java public static void exportDataToExcel(String[] headers, String[][] data, String fileName) { Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); // 创建表头 Row headerRow = sheet.createRow(0); for (int i = 0; i < headers.length; i++) { Cell headerCell = headerRow.createCell(i); headerCell.setCellValue(headers[i]); } // 填充数据 for (int row = 0; row < data.length; row++) { Row dataRow = sheet.createRow(row+1); for (int col = 0; col < data[row].length; col++) { Cell dataCell = dataRow.createCell(col); dataCell.setCellValue(data[row][col]); } } // 保存文件 try { FileOutputStream outputStream = new FileOutputStream(fileName); workbook.write(outputStream); workbook.close(); } catch (IOException e) { e.printStackTrace(); } } ``` 3. 在主程序中调用方法来批量导出Excel文件: ```java public static void main(String[] args) { String[] headers = {"姓名", "年龄", "性别"}; String[][] data = { {"张三", "20", "男"}, {"李四", "25", "女"}, {"王五", "30", "男"}, // 可以根据实际需求添加更多的数据 }; for (int i = 1; i <= 5; i++) { String fileName = "导出文件" + i + ".xlsx"; exportDataToExcel(headers, data, fileName); System.out.println(fileName + " 导出成功!"); } } ``` 以上代码将会创建一个Excel文件,每个文件中包含相同的表头和数据。将数据放入一个二维数组中,循环调用`exportDataToExcel()`方法即可批量导出多个Excel文件。每个Excel文件文件名通过追加数字来区分。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值