HSSFWorkbook 导出excel表格

demo如下:

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

  //上述是部分依赖

/**
 * 描述 导出数据表Excel
 *
 * @param response
 * @return
 * @author ***
 * @date ***
 */
@RequestMapping(value = "/dataExport", method = RequestMethod.GET)
public void exportExcel(HttpServletResponse response) throws Exception {

    //获取数据列表
    //MgrDataResultDto是自定义的实体bean
    List list = queryListService.getDataList();

    String[] title = {"序号", "部门", "源表名", "test01", "推荐名称", "匹配度", "test02",
            "test03", "test04", "状态", "test05", "推荐时间", "test06", "改进时间"};

    String fileName = "test.xls";
    String sheetName = "test合并";

    String[][] content = new String[list.size()][];
    for (int i = 0; i < list.size(); i++) {
        content[i] = new String[title.length];
        MgrDataResultDto obj = list.get(i);

        content[i][0] = i + 1 + "";
        content[i][1] = obj.getDatasrdep();
        content[i][2] = obj.getDatasource();
        content[i][3] = obj.getHjFrmcUn();
        content[i][4] = obj.getFrkFrmcNew();
        content[i][5] = obj.getScore() + "%";
        content[i][6] = "50";
        content[i][7] = obj.getFrkFrwybs();
        content[i][8] = "100%";
        content[i][9] = obj.getStatus();
        content[i][10] = obj.getHashUnique();
        content[i][11] = obj.getCreateTime().toString();
        content[i][12] = obj.getOpRecord();
        content[i][13] = obj.getResolveTime().toString();

    }

    HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);

    try {
        setResponseHeader(response, fileName);
        OutputStream os = response.getOutputStream();
        wb.write(os);
        os.flush();
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

/**
 * 描述 设置响应头
 *
 * @param response
 * @param fileName
 * @return
 * @author ***
 * @date ***
 */
public void setResponseHeader(HttpServletResponse response, String fileName) {
    try {
        try {
            fileName = new String(fileName.getBytes(), "ISO8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setContentType("application/octet-stream;charset=ISO8859-1");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        response.addHeader("Pargam", "no-cache");
        response.addHeader("Cache-Control", "no-cache");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

 

ExcelUtil 工具类代码如下:

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
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.util.CellRangeAddress;

/**
 * 描述 导出表到Excel
 *
 * @author ***
 * @date ***
 */
public class ExcelUtil {

    private static final String SHEET_NAME = "test合并";

    /**
     * 描述 导出表到Excel
     *
     * @param sheetName
     * @param title
     * @param values
     * @param wb
     * @return HSSFWorkbook
     * @return
     * @author ***
     * @date ***
     */
    public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] values, HSSFWorkbook wb) {

        if (wb == null) {
            wb = new HSSFWorkbook();
        }

        HSSFSheet sheet = wb.createSheet(sheetName);

        HSSFRow row = sheet.createRow(0);

        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        HSSFCell cell;

        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
        }

        for (int i = 0; i < values.length; i++) {
            row = sheet.createRow(i + 1);
            for (int j = 0; j < values[i].length; j++) {
                row.createCell(j).setCellValue(values[i][j]);
            }
        }

        //当当前的表格是预定义的表格时进行如下合并操作。
        if (SHEET_NAME.equals(sheetName)) {
            //下面是针对某些列需要合并的做处理
            //从第1行开始(第0行是标题行),此处设定每3行进行一次针对某些列的合并处理
            for (int i = 1; i < values.length; i += 3) {
                //以下是每隔3行,针对第1,2,3,6,9,10,11,13等列进行相同合并处理(列的下标从0开始)
                sheet.addMergedRegion(new CellRangeAddress(i, i + 2, 1, 1));
                sheet.addMergedRegion(new CellRangeAddress(i, i + 2, 2, 2));
                sheet.addMergedRegion(new CellRangeAddress(i, i + 2, 3, 3));
                sheet.addMergedRegion(new CellRangeAddress(i, i + 2, 6, 6));
                sheet.addMergedRegion(new CellRangeAddress(i, i + 2, 9, 9));
                sheet.addMergedRegion(new CellRangeAddress(i, i + 2, 10, 10));
                sheet.addMergedRegion(new CellRangeAddress(i, i + 2, 11, 11));
                sheet.addMergedRegion(new CellRangeAddress(i, i + 2, 13, 13));
            }
        }

        return wb;
    }
}

你可能感兴趣的:(Small,Technology,Point,Java,Basis,utils)