ITPub博客

首页 > IT职业 > IT职场 > POI导出工具类

POI导出工具类

IT职场 作者:cookye 时间:2021-09-09 09:55:00 0 删除 编辑

         

import java.io.IOException;import java.io.OutputStream;import java.net.URLEncoder;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletResponse;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFFont;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;public class ExportUtils {    /**     * 导出     * @param data 数据     * @param columnTitleList 标题名称     * @param columnCodeList 列参数     * @param fileName 文件名称     * @param response     */    public static void exportData(List> data, String[] columnTitleList, String[] columnCodeList,            String fileName, HttpServletResponse response) {        HSSFWorkbook workbook = new HSSFWorkbook(); //定义一个新的工作簿        HSSFSheet sheet = workbook.createSheet();  // 创建第一个Sheet页        HSSFRow row = sheet.createRow(0);                // 创建标题样式         HSSFCellStyle headerStyle = (HSSFCellStyle) workbook .createCellStyle();// 创建标题样式          headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);    //设置垂直居中          headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);   //设置水平居中          HSSFFont headerFont = (HSSFFont) workbook.createFont(); //创建字体样式          headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗          headerFont.setFontName("Times New Roman");  //设置字体类型          headerFont.setFontHeightInPoints((short) 14);    //设置字体大小          headerStyle.setFont(headerFont);    //为标题样式设置字体样式        headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框          headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框          headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框          headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框                  //正文样式        HSSFCellStyle cell_Style = (HSSFCellStyle) workbook .createCellStyle();// 设置字体样式          cell_Style.setAlignment(HSSFCellStyle.ALIGN_CENTER);          cell_Style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直对齐居中          cell_Style.setWrapText(true); // 设置为自动换行          HSSFFont cell_Font = (HSSFFont) workbook.createFont();          cell_Font.setFontName("宋体");          cell_Font.setFontHeightInPoints((short) 12);          cell_Style.setFont(cell_Font);          cell_Style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框          cell_Style.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框          cell_Style.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框          cell_Style.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框                  // 设置标题        for (int i = 0; i  map : data) {                row = sheet.createRow(++n);                for (int i = 0; i 


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1369/viewspace-2814343/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    91
  • 访问量
    502