[Poi export excel file] Java: bulk import and export files to achieve practical (compatible xls, xlsx)

Author: small shop selling grandfather

cnblogs.com/laoyeye/p/6938889.html

1 Introduction

java achieve import and export database file, is now more common feature in most systems, today wrote a little demo to understand its principle, no contact with students can also take a look at the reference.

Currently I have talked mainly POI import and export technology and iReport, poi mainly as bulk import some data database, iReport do export a report. Another poi jxl similar manner, but seemingly a long time not with a new, office after 2007 seems not to support, I can not say here.

2, POI uses detailed

2.1 What is Apache POI?

Apache POI is the Apache Software Foundation's open-source libraries, POI provides an API to the Java program to read and write Microsoft Office file format functions.

2.2 POI package into a jar

This explains the use maven project, jar package version using poi-3.14 and poi-ooxml-3.14. The latest version is 3.16. Because after 3.15 api-related updates, some operations may be different, we pay attention to the next.

<!-- poi的包 3.15版本后单元格类型获取方式有调整 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.14</version>
        </dependency>

2.3 POI explain the API

2.3.1 Structure

HSSF - provide read and write Microsoft Excel file format functions.
XSSF - provide read and write Microsoft Excel OOXML file format functions.
HWPF - provide read and write Microsoft Word file format functions.
HSLF - provide read and write Microsoft PowerPoint file format functions.
HDGF - provide read and write Microsoft Visio file format functions.

2.3.2 Object

This paper describes HSSF and XSSF two kinds of components, simply say HSSF used to operate the former version of Office 2007 excel.xls file, XSSF for excel.xlsx file version of Office 2007 operation, pay attention to both the suffix is ​​not the same.

In HSSF org.apache.poi.hssf.usermodel package. It implements the Workbook Interface for Excel .xls file format

Common components:

HSSFWorkbook: excel document object
HSSFSheet: excel form
HSSFRow: excel line
HSSFCell: excel grid unit
HSSFFont: excel font
HSSFDataFormat: Date format
HSSFHeader: sheet head
HSSFFooter: sheet tail (only when printing to see the results)

style:

HSSFCellStyle: cell style

Auxiliary operations include:

HSSFDateUtil: Date
HSSFPrintSetup: Print
HSSFErrorConstants: Error Message List

In org.apache.xssf.usemodel XSSF package, and implement Workbook interface for Excel file format .xlsx

Common components:

XSSFWorkbook: excel document object
XSSFSheet: excel form
XSSFRow: excel line
XSSFCell: excel grid unit of
XSSFFont: excel font
XSSFDataFormat: Date format

And HSSF similar;

Both components together 2.3.3 Description field type

In fact, the two components is to excel for the two formats, most of the operations are the same.

 

2.3.4 Procedure

In an example HSSF same XSSF operation.

First, understand how the organizational form of an Excel file, an Excel file corresponds to a workbook (HSSFWorkbook), a workbook can have multiple sheet (HSSFSheet) composed of a sheet is composed of multiple row (HSSFRow) composed of a row is a plurality of cell (HSSFCell) composition.

1, with HSSFWorkbook open or create a "Excel file object."

2, or created with the object returns HSSFWorkbook Sheet object

3, the object returns Sheet object rows, row objects obtained by the object Cell

4, to read and write the object Cell.

3, the operation codes

3.1 renderings

3.2 Detailed Code

I'm here with Spring + SpringMVC + Mybatis based extensions: SpringBoot + Mybatis multi-module (module) project to build a tutorial

Controller:

package com.allan.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.formula.functions.Mode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.allan.pojo.Student;
import com.allan.service.StudentService;
/**
 * 
 * @author 小卖铺的老爷爷
 *
 */
@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;
    /**
     * 批量导入表单数据
     * 
     * @param request
     * @param myfile
     * @return
     */

    @RequestMapping(value="/importExcel",method=RequestMethod.POST)
    public String importExcel(@RequestParam("myfile") MultipartFile myFile) {
        ModelAndView modelAndView = new ModelAndView();
        try {
            Integer num = studentService.importExcel(myFile);
        } catch (Exception e) {
            modelAndView.addObject("msg", e.getMessage());
            return "index";
        }
        modelAndView.addObject("msg", "数据导入成功");

        return "index";
    }

    @RequestMapping(value="/exportExcel",method=RequestMethod.GET)
    public void exportExcel(HttpServletResponse response) {    
        try {
            studentService.exportExcel(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}

Service

package com.allan.service.impl;

import java.io.OutputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.allan.mapper.StudentMapper;
import com.allan.pojo.Student;
import com.allan.service.StudentService;
/**
 * 
 * @author 小卖铺的老爷爷
 *
 */
@Service
public class StudentServiceImpl implements StudentService{
    private final static String XLS = "xls";  
    private final static String XLSX = "xlsx"; 
    @Autowired
    private StudentMapper studentMapper;
    /**
     * 导入Excel,兼容xls和xlsx
     */
    @SuppressWarnings("resource")
    public Integer importExcel(MultipartFile myFile) throws Exception {
        //        1、用HSSFWorkbook打开或者创建“Excel文件对象”
        //
        //        2、用HSSFWorkbook对象返回或者创建Sheet对象
        //
        //        3、用Sheet对象返回行对象,用行对象得到Cell对象
        //
        //        4、对Cell对象读写。
        //获得文件名  
        Workbook workbook = null ;
        String fileName = myFile.getOriginalFilename(); 
        if(fileName.endsWith(XLS)){  
            //2003  
            workbook = new HSSFWorkbook(myFile.getInputStream());  
        }else if(fileName.endsWith(XLSX)){  
            //2007  
            workbook = new XSSFWorkbook(myFile.getInputStream());  
        }else{
            throw new Exception("文件不是Excel文件");
        }

        Sheet sheet = workbook.getSheet("Sheet1");
        int rows = sheet.getLastRowNum();// 指的行数,一共有多少行+
        if(rows==0){
            throw new Exception("请填写数据");
        }
        for (int i = 1; i <= rows+1; i++) {
            // 读取左上端单元格
            Row row = sheet.getRow(i);
            // 行不为空
            if (row != null) {
                // **读取cell**
                Student student = new Student();
                //姓名
                String name = getCellValue(row.getCell(0));
                student.setName(name);
                //班级
                String classes = getCellValue(row.getCell(1));
                student.setClasses(classes);
                //分数
                String scoreString = getCellValue(row.getCell(2));
                if (!StringUtils.isEmpty(scoreString)) {
                    Integer score = Integer.parseInt(scoreString);
                    student.setScore(score);
                }
                //考试时间
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟  
                String dateString = getCellValue(row.getCell(3));  
                if (!StringUtils.isEmpty(dateString)) {
                    Date date=sdf.parse(dateString);  
                    student.setTime(date);
                }
                studentMapper.insert(student);
            }
        }
        return rows-1;
    }

    /**
     * 获得Cell内容
     * 
     * @param cell
     * @return
     */
    public String getCellValue(Cell cell) {
        String value = "";
        if (cell != null) {
            // 以下是判断数据的类型
            switch (cell.getCellType()) {
            case HSSFCell.CELL_TYPE_NUMERIC: // 数字
                value = cell.getNumericCellValue() + "";
                if (HSSFDateUtil.isCellDateFormatted(cell)) {
                    Date date = cell.getDateCellValue();
                    if (date != null) {
                        value = new SimpleDateFormat("yyyy-MM-dd").format(date);
                    } else {
                        value = "";
                    }
                } else {
                    value = new DecimalFormat("0").format(cell.getNumericCellValue());
                }
                break;
            case HSSFCell.CELL_TYPE_STRING: // 字符串
                value = cell.getStringCellValue();
                break;
            case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
                value = cell.getBooleanCellValue() + "";
                break;
            case HSSFCell.CELL_TYPE_FORMULA: // 公式
                value = cell.getCellFormula() + "";
                break;
            case HSSFCell.CELL_TYPE_BLANK: // 空值
                value = "";
                break;
            case HSSFCell.CELL_TYPE_ERROR: // 故障
                value = "非法字符";
                break;
            default:
                value = "未知类型";
                break;
            }
        }
        return value.trim();
    }
    /**
     * 导出excel文件
     */
    public void exportExcel(HttpServletResponse response) throws Exception {
        // 第一步,创建一个webbook,对应一个Excel文件  
        HSSFWorkbook wb = new HSSFWorkbook();  
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet  
        HSSFSheet sheet = wb.createSheet("Sheet1");  
        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
        HSSFRow row = sheet.createRow(0);  
        // 第四步,创建单元格,并设置值表头 设置表头居中  
        HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  

        HSSFCell cell = row.createCell(0);
        cell.setCellValue("姓名");  
        cell.setCellStyle(style);  
        cell = row.createCell(1);  
        cell.setCellValue("班级");  
        cell.setCellStyle(style);  
        cell = row.createCell(2);  
        cell.setCellValue("分数");  
        cell.setCellStyle(style);  
        cell = row.createCell(3);  
        cell.setCellValue("时间");  
        cell.setCellStyle(style);  

        // 第五步,写入实体数据 实际应用中这些数据从数据库得到,  
        List<Student> list = studentMapper.selectAll();  

        for (int i = 0; i < list.size(); i++){  
            row = sheet.createRow(i + 1);  
            Student stu = list.get(i);  
            // 第四步,创建单元格,并设置值  
            row.createCell(0).setCellValue(stu.getName());  
            row.createCell(1).setCellValue(stu.getClasses());  
            row.createCell(2).setCellValue(stu.getScore());  
            cell = row.createCell(3);  
            cell.setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(stu.getTime()));  
        }          
        //第六步,输出Excel文件
        OutputStream output=response.getOutputStream();
        response.reset();
        long filename = System.currentTimeMillis();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        String fileName = df.format(new Date());// new Date()为获取当前系统时间
        response.setHeader("Content-disposition", "attachment; filename="+fileName+".xls");
        response.setContentType("application/msexcel");        
        wb.write(output);
        output.close();
    }  

}

3.3 Supplementary export file api

We can see that the above code only the most basic service export.

Export Excel files in practical applications often need to read and print, which needs to be output to Excel document layout and style setting, significant operations are merged cells, set the cell style, set the font styles.

3.3.1 Merge cells

The use HSSFSheet addMergedRegion () method

public int addMergedRegion(CellRangeAddress region)

CellRangeAddress parameter indicates the consolidated region is constructed as follows: start line sequentially represents, as at row, starting column, as a column

CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol)

3.3.2 Set the cell row height and column width

HSSFSheet sheet=wb.createSheet();
sheet.setDefaultRowHeightInPoints(10);//设置缺省列高sheet.setDefaultColumnWidth(20);//设置缺省列宽
//设置指定列的列宽,256 * 50这种写法是因为width参数单位是单个字符的256分之一
sheet.setColumnWidth(cell.getColumnIndex(), 256 * 50);

3.3.3 Set cell style

1. Create HSSFCellStyle

HSSFCellStyle cellStyle=wkb.createCellStyle()

2, set the style

// 设置单元格的横向和纵向对齐方式,具体参数就不列了,参考HSSFCellStyle
  cellStyle.setAlignment(HSSFCellStyle.ALIGN_JUSTIFY);
  cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  /* 设置单元格的填充方式,以及前景颜色和背景颜色
   三点注意:
   1.如果需要前景颜色或背景颜色,一定要指定填充方式,两者顺序无所谓;
   2.如果同时存在前景颜色和背景颜色,前景颜色的设置要写在前面;
   3.前景颜色不是字体颜色。
  */
  //设置填充方式(填充图案)
  cellStyle.setFillPattern(HSSFCellStyle.DIAMONDS);
  //设置前景色
  cellStyle.setFillForegroundColor(HSSFColor.RED.index);
  //设置背景颜色
  cellStyle.setFillBackgroundColor(HSSFColor.LIGHT_YELLOW.index);
  // 设置单元格底部的边框及其样式和颜色
  // 这里仅设置了底边边框,左边框、右边框和顶边框同理可设
  cellStyle.setBorderBottom(HSSFCellStyle.BORDER_SLANTED_DASH_DOT);
  cellStyle.setBottomBorderColor(HSSFColor.DARK_RED.index);
  //设置日期型数据的显示样式
  cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));

3, the style applied to the cell

  cell.setCellStyle(cellStyle);
  //将样式应用到行,但有些样式只对单元格起作用
  row.setRowStyle(cellStyle);

3.3.4 set the font style

1. Create HSSFFont objects (call createFont method of HSSFWorkbook)

HSSFWorkbook wb=new HSSFWorkbook();
HSSFFont  fontStyle=wb.createFont();
HSSFWorkbook wb=new HSSFWorkbook ();

2, set the font variety of styles

//设置字体样式
  fontStyle.setFontName("宋体");  
  //设置字体高度
  fontStyle.setFontHeightInPoints((short)20);  
  //设置字体颜色
  font.setColor(HSSFColor.BLUE.index);
  //设置粗体
  fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  //设置斜体
  font.setItalic(true);
  //设置下划线
  font.setUnderline(HSSFFont.U_SINGLE);

3, the font style is set to the cell

//字体也是单元格格式的一部分,所以从属于HSSFCellStyle
// 将字体对象赋值给单元格样式对象
cellStyle.setFont(font);
// 将单元格样式应用于单元格
cell.setCellStyle(cellStyle);

We can see that the export file with the poi is quite troublesome, in the next method to introduce the next irport and so on.

Export of these api basically, above last hope that we can help.

Source address: https: //github.com/allanzhuo/myport.git

Guess you like

Origin www.cnblogs.com/Koaler/p/12381441.html