Java (poi) export Excel data

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

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

@SuppressWarnings( { "deprecation" })
public class Test46 {

public void Excel(HttpServletRequest request, HttpServletResponse response) throws Exception {

String sheetName = "用车统计表单";
    String titleName = "用车申请数据统计表";
    String title2Name = "自查日期:";
    String fileName = "用车申请统计表单";
    String beiZhuName ="备注:1.测试 。\r\n2.测试 。\r\n3.测试 。";//**设置强制换行“\r\n”**
    int columnNumber = 3;
    int[] columnWidth = { 10, 20, 30 };
    String[][] dataList = { { "001", "2015-01-01", "IT" },
            { "002", "2015-01-02", "市场部" }, { "003", "2015-01-03", "测试" } };
    String[] columnName = { "单号", "申请时间", "申请部门" };
}
        // 第一步,创建一个webbook,对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetName);
        // sheet.setDefaultColumnWidth(15); //统一设置列宽
        for (int i = 0; i < columnNumber; i++) 
        {
            for (int j = 0; j <= i; j++) 
            {
                if (i == j) 
                {
                    sheet.setColumnWidth(i, columnWidth[j] * 256); // 单独设置每列的宽
                }
            }
        }
        // 创建第0行 也就是标题
        HSSFRow row1 = sheet.createRow((int) 0);
        row1.setHeightInPoints(50);// 设备标题的高度
        // 第三步创建标题的单元格样式style2以及字体样式headerFont1
        HSSFCellStyle style2 = wb.createCellStyle();
        style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        style2.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
        style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        HSSFFont headerFont1 = (HSSFFont) wb.createFont(); // 创建字体样式
        headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
        headerFont1.setFontName("黑体"); // 设置字体类型
        headerFont1.setFontHeightInPoints((short) 15); // 设置字体大小
        style2.setFont(headerFont1); // 为标题样式设置字体样式

        HSSFCell cell1 = row1.createCell(0);// 创建标题第一列
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0,
                columnNumber - 1)); // 合并第0到第17列
        cell1.setCellValue(titleName); // 设置值标题
        cell1.setCellStyle(style2); // 设置标题样式

        // 创建 时间
        HSSFRow row2 = sheet.createRow((int) 1);
        row2.setHeightInPoints(21);// 设置时间高度
        // 创建时间 单元格样式 以及表头的字体样式
        HSSFCellStyle style2 = wb.createCellStyle();
        style2.setAlignment(HSSFCellStyle.ALIGN_RIGHT);// 创建一个靠右格式 水平方向
        style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直方向
        HSSFFont headerFont2 = (HSSFFont) wb.createFont(); // 创建字体样式
        headerFont2.setFontName("楷体"); // 设置字体类型
        headerFont2.setFontHeightInPoints((short) 12); // 设置字体大小
        style2.setFont(headerFont2); // 为标题样式设置字体样式
        HSSFCell cell2 = row2.createCell(0);// 设置值在第一列
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, columnNumber - 1)); // 合并列标题 在用poi在EXECL报表设计的时候,遇到单元格合并问题,用到一个重要的函数:

CellRangeAddress(int, int, int, int)
parameters: starting row number, ending row number, starting column number, ending column number
SimpleDateFormat df2 = new SimpleDateFormat("MM, MM, DD, YYYY");
String date2 = df2. format(new Date());
cell2.setCellValue(title2Name + date2); // set the value title
cell2.setCellStyle(style2); // set the title style

        // 创建备注
        HSSFRow row3 = sheet.createRow((int) 12);
        row3.setHeightInPoints(71);// 设备标题的高度
        // 第三步创建标题的单元格样式style2以及字体样式headerFont1
        HSSFCellStyle style3 = wb.createCellStyle();
        style3.setWrapText(true);// 设置自动换行
        style3.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 水平靠左
        style3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直方向
        HSSFFont headerFont3 = (HSSFFont) wb.createFont(); // 创建字体样式
        headerFont3.setFontName("仿宋"); // 设置字体类型
        headerFont3.setFontHeightInPoints((short) 11); // 设置字体大小
        style3.setFont(headerFont3); // 为标题样式设置字体样式

        HSSFCell cell3 = row3.createCell(0);// 创建标题第一列
        sheet.addMergedRegion(new CellRangeAddress(12, 12, 0, columnNumber - 1)); // 合并列标题
        cell3.setCellValue(beiZhuName); // 设置值标题
        cell3.setCellStyle(style3); // 设置标题样式

        // 创建第1行 也就是表头
        HSSFRow row = sheet.createRow((int) 2);
        row.setHeightInPoints(25);// 设置表头高度

        // 创建第1行 也就是表头
        HSSFRow row = sheet.createRow((int) 1);
        row.setHeightInPoints(37);// 设置表头高度

        // 第四步,创建表头单元格样式 以及表头的字体样式
        HSSFCellStyle style = wb.createCellStyle();
        style.setWrapText(true);// 设置自动换行
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式

        style.setBottomBorderColor(HSSFColor.BLACK.index);
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);

        HSSFFont headerFont = (HSSFFont) wb.createFont(); // 创建字体样式
        headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
        headerFont.setFontName("黑体"); // 设置字体类型
        headerFont.setFontHeightInPoints((short) 10); // 设置字体大小
        style.setFont(headerFont); // 为标题样式设置字体样式

        // 第四.一步,创建表头的列
        for (int i = 0; i < columnNumber; i++) 
        {
            HSSFCell cell = row.createCell(i);
            cell.setCellValue(columnName[i]);
            cell.setCellStyle(style);
        }

        // 第五步,创建单元格,并设置值
        for (int i = 0; i < dataList.length; i++) 
        {
            row = sheet.createRow((int) i + 2);
            // 为数据内容设置特点新单元格样式1 自动换行 上下居中
            HSSFCellStyle zidonghuanhang = wb.createCellStyle();
            zidonghuanhang.setWrapText(true);// 设置自动换行
            zidonghuanhang
                    .setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式

            // 设置边框
            zidonghuanhang.setBottomBorderColor(HSSFColor.BLACK.index);
            zidonghuanhang.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            zidonghuanhang.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            zidonghuanhang.setBorderRight(HSSFCellStyle.BORDER_THIN);
            zidonghuanhang.setBorderTop(HSSFCellStyle.BORDER_THIN);

            // 为数据内容设置特点新单元格样式2 自动换行 上下居中左右也居中
            HSSFCellStyle zidonghuanhang2 = wb.createCellStyle();
            zidonghuanhang2.setWrapText(true);// 设置自动换行
            zidonghuanhang2
                    .setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个上下居中格式
            zidonghuanhang2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中

            // 设置边框
            zidonghuanhang2.setBottomBorderColor(HSSFColor.BLACK.index);
            zidonghuanhang2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            zidonghuanhang2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            zidonghuanhang2.setBorderRight(HSSFCellStyle.BORDER_THIN);
            zidonghuanhang2.setBorderTop(HSSFCellStyle.BORDER_THIN);
            HSSFFont neiRongFont = (HSSFFont) wb.createFont(); // 创建内容字体样式
            neiRongFont.setFontName("宋体"); // 设置内容字体类型
            neiRongFont.setFontHeightInPoints((short) 11); // 设置内容字体大小
            zidonghuanhang2.setFont(neiRongFont); // 为内容样式设置字体样式
            HSSFCell datacell = null;
            for (int j = 0; j < columnNumber; j++) 
            {
                datacell = row.createCell(j);
                datacell.setCellValue(dataList[i][j]);
                datacell.setCellStyle(zidonghuanhang2);
            }
        }

    // 输出Excel文件  ,直接找到浏览器设置的下载地址,下载完成
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String date = sdf.format(new Date());
            OutputStream output = response.getOutputStream();
            String filename = fileName + date + ".xls";
            response.getOutputStream().write(1);
            response.reset();
            response.setHeader("Content-disposition",
                    "attachment; filename=".concat(String.valueOf(URLEncoder.encode(filename, "UTF-8"))));
            response.setContentType("application/msexcel;charset=UTF-8");//设置导出的文件名称为UTF-8格式,支持汉字名称+时间
            wb.write(output);
            wb.close();
            output.flush();
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
}

}
Recommend a blog address with full knowledge of poi: https://www.cnblogs.com/azhqiang/p/4362090.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324506771&siteId=291194637