开发记录——EXCEL导出

本文介绍如何使用Java实现带有格式的Excel表格导出,包括设置背景颜色、合并单元格等操作,并提供完整的代码示例。

在java开发中,excel表格的导出可谓是家常便饭,本周的工作中被分配了导出excel,于是便写了一个导出。但后来又提出了对表格有格式的要求,比如背景啊,合并单元格之类的,于是就在网上找了一堆资料慢慢自己研究出来了,觉得挺有记录价值的,所以现在把它记录下来。
这次不废话,直接上代码,因为不是很难的东西所以直接用注释解释

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

public class download{
    public static void download(){
        try{
        //首先是初始化一个excel表格并给入所需要的参数
            HSSFWorkbook workbook = new HSSFWorkbook();//产生工作簿对象
            HSSFSheet sheet = workbook.createSheet("测试表格");//产生工作表对象括号中则可写入sheet名称(注意sheet名和文件名是两个不同的东西,这部分请了解excel的使用)
            sheet.setDefaultColumnWidth(17);//设置单元格宽度(Arial字体,10号字下9个中文字符)
            HSSFCellStyle titleStyle = workbook.createCellStyle();//设置抬头样式
            HSSFCellStyle style = workbook.createCellStyle();//其他样式
            titleStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex());//设置单元格颜色
            titleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);//使颜色设置生效
            titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中设置
            titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中设置
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//正文水平居中设置

            //初始化行,列,单元格参数,方便后续使用
            HSSFRow row = null;//行
            HSSFCell cell = null;//列
            HSSFRichTextString text = null;//单元格内容

            //因为我需要合并单元格作为抬头所以这里写两个合并单元格的样式并插入sheet样式
            //初始化两个合并单元格的样式
            CellRangeAddress cra = new CellRangeAddress(0, 1, 0, 0);//合并单元格,括号内的参数分别是(起始行数,结束行数,起始列数,结束列数)
            CellRangeAddress cra2 = new CellRangeAddress(0, 0, 1, 5);
            //将样式插入进sheet生效
            sheet.addMergedRegion(cra);
            sheet.addMergedRegion(cra2);

            //抬头
            HSSFRow firstRow = sheet.createRow(0);//注意,要在一行内添加多个列,row要提前申明,不然每sheet一次相当于初始化一次,这里是定位需要写入数据的行数,从0开始计数     
            //标题名
            cell = firstRow.createCell(0);//定位行数中的具体列数
            cell.setCellStyle(titleStyle);//将写好的样式放入
            text = new HSSFRichTextString("标题名");//需要写入的文本数据,注意,这种方法放入的必须是String类型的数据,注意要放入数据的类型转换
            cell.setCellValue(text);//将数据放入准备好的单元格

            //因为抬头是比较特殊的,所以只能单独一个一个写入
            //用户信息
            cell = firstRow.createCell(1);
            cell.setCellStyle(titleStyle);
            text = new HSSFRichTextString("用户信息");
            cell.setCellValue(text);

            //而像这种比较有规律的标题就可以用for循环写入
            List<String> titles = new ArrayList<>();
            titles.add("登录名");
            titles.add("姓名");
            titles.add("部门");
            titles.add("完成时间(分钟)");
            titles.add("状态");

            //注意一定要头脑清晰,确认角标的位置
            //其他标题起始列数
            int titleNum = 1;

            HSSFRow sceondRow = sheet.createRow(1);

            //其他标题
            for(String title : titles){
                cell = sceondRow.createCell(titleNum++);
                cell.setCellStyle(titleStyle);
                text = new HSSFRichTextString(title);
                cell.setCellValue(text);
            }

            //导出excel
            String fileName = URLEncoder.encode("测试excel", "UTF-8");//这里是导出的文件名和编码格式
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls"); //这里注意一下,这里的.xls是导出文件的后缀,一般导出的后缀是xls,不要写成xlsx这种07之后的excel格式,会造成文件无法读取的状况,建议最好用这种比较低,通用的格式
            workbook.write(response.out);
            response.out.close();
        }catch(Exception e){
            throw e;
        }
    }
}

这样就能导出一个带有格式的excel的表格,下面附上导出的图
这里写图片描述
下面附上背景颜色的对照数据

style.setFillForegroundColor(IndexedColors.AQUA.getIndex());
cell.setCellValue("X1");

style.setFillForegroundColor(IndexedColors.AUTOMATIC.getIndex());
cell.setCellValue("X2");

style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
cell.setCellValue("X3");

style.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
cell.setCellValue("X4");

style.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
cell.setCellValue("X5");

style.setFillForegroundColor(IndexedColors.BROWN.getIndex());
cell.setCellValue("X6");

style.setFillForegroundColor(IndexedColors.CORAL.getIndex());
cell.setCellValue("X7");

style.setFillForegroundColor(IndexedColors.CORNFLOWER_BLUE.getIndex());
cell.setCellValue("X8");

style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
cell.setCellValue("X9");

style.setFillForegroundColor(IndexedColors.DARK_GREEN.getIndex());
cell.setCellValue("X10");

style.setFillForegroundColor(IndexedColors.DARK_RED.getIndex());
cell.setCellValue("X11");

style.setFillForegroundColor(IndexedColors.DARK_TEAL.getIndex());
cell.setCellValue("X12");

style.setFillForegroundColor(IndexedColors.DARK_YELLOW.getIndex());
cell.setCellValue("X13");

style.setFillForegroundColor(IndexedColors.GOLD.getIndex());
cell.setCellValue("X14");

style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
cell.setCellValue("X15");

style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
cell.setCellValue("X16");

style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
cell.setCellValue("X17");

style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
cell.setCellValue("X18");

style.setFillForegroundColor(IndexedColors.GREY_80_PERCENT.getIndex());
cell.setCellValue("X19");

style.setFillForegroundColor(IndexedColors.INDIGO.getIndex());
cell.setCellValue("X20");

style.setFillForegroundColor(IndexedColors.LAVENDER.getIndex());
cell.setCellValue("X21");

style.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
cell.setCellValue("X22");

style.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
cell.setCellValue("X23");

style.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
cell.setCellValue("X24");

style.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
cell.setCellValue("X25");

style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
cell.setCellValue("X26");

style.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
cell.setCellValue("X27");

style.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex());
cell.setCellValue("X28");

style.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex());
cell.setCellValue("X29");

style.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
cell.setCellValue("X30");

style.setFillForegroundColor(IndexedColors.LIME.getIndex());
cell.setCellValue("X31");

style.setFillForegroundColor(IndexedColors.MAROON.getIndex());
cell.setCellValue("X32");

style.setFillForegroundColor(IndexedColors.OLIVE_GREEN.getIndex());
cell.setCellValue("X33");

style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
cell.setCellValue("X34");

style.setFillForegroundColor(IndexedColors.ORCHID.getIndex());
cell.setCellValue("X35");

style.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
cell.setCellValue("X36");

style.setFillForegroundColor(IndexedColors.PINK.getIndex());
cell.setCellValue("X37");

style.setFillForegroundColor(IndexedColors.PLUM.getIndex());
cell.setCellValue("X38");

style.setFillForegroundColor(IndexedColors.RED.getIndex());
cell.setCellValue("X39");

style.setFillForegroundColor(IndexedColors.ROSE.getIndex());
cell.setCellValue("X40");

style.setFillForegroundColor(IndexedColors.ROYAL_BLUE.getIndex());
cell.setCellValue("X41");

style.setFillForegroundColor(IndexedColors.SEA_GREEN.getIndex());
cell.setCellValue("X42");

style.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
cell.setCellValue("X43");

style.setFillForegroundColor(IndexedColors.TAN.getIndex());
cell.setCellValue("X44");

style.setFillForegroundColor(IndexedColors.TEAL.getIndex());
cell.setCellValue("X45");

style.setFillForegroundColor(IndexedColors.TURQUOISE.getIndex());
cell.setCellValue("X46");

style.setFillForegroundColor(IndexedColors.VIOLET.getIndex());
cell.setCellValue("X47");

style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
cell.setCellValue("X48");

style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
cell.setCellValue("X49");

下面附上参照图
这里写图片描述
感谢iteye的网友xiaohewoai提供的颜色参照,我截取了重点部分,这里附上原址(http://xiaohewoai.iteye.com/blog/1300817),可供大家参考。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值