2019独角兽企业重金招聘Python工程师标准>>>
引用:http://apps.hi.baidu.com/share/detail/17249059
POI中可能会用到一些需要设置EXCEL单元格格式的操作小结:
先获取工作薄对象:
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFCellStyle setBorder = wb.createCellStyle();
一、设置背景色:
setBorder.setFillForegroundColor((short) 13);// 设置背景色
setBorder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
二、设置边框:
setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
三、设置居中:
setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
四、设置字体:
HSSFFont font = wb.createFont();
font.setFontName("黑体");
font.setFontHeightInPoints((short) 16);//设置字体大小
HSSFFont font2 = wb.createFont();
font2.setFontName("仿宋_GB2312");
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
font2.setFontHeightInPoints((short) 12);
setBorder.setFont(font);//选择需要用到的字体格式
五、设置列宽:
sheet.setColumnWidth(0, 3766); //第一个参数代表列id(从0开始),第2个参数代表宽度值 参考 :"2012-08-10"的宽度为2500
六、设置自动换行:
setBorder.setWrapText(true);//设置自动换行
七、合并单元格:
Region region1 = new Region(0, (short) 0, 0, (short) 6);
//参数1:行号 参数2:起始列号 参数3:行号 参数4:终止列号
或者用
CellRangeAddress region1 = new CellRangeAddress(rowNumber, rowNumber, (short) 0, (short) 11);
但应注意两个构造方法的参数不是一样的,具体使用哪个取决于POI的不同版本。
sheet.addMergedRegion(region1);
目前用过的就这么多,后续有新的会继续添加。
导出例子:
List statMonthRespList = tempResp.getResult();// 结果数据放入list
if (null == statMonthRespList)
{
statMonthRespList = new ArrayList();
}
String url = "/file/统计.xls";
try
{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFCellStyle setBorder = workbook.createCellStyle();
HSSFCellStyle setTitleBorder = workbook.createCellStyle();
HSSFCellStyle setTitleBorder1 = workbook.createCellStyle();
/*****设置单元格格式*****/
setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN);
setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);
setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);
setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);
setBorder.setBottomBorderColor(HSSFColor.BLACK.index); // 下边框
setBorder.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框
setBorder.setTopBorderColor(HSSFColor.BLACK.index);// 上边框
setBorder.setRightBorderColor(HSSFColor.BLACK.index);// 右边框
setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
setBorder.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFFont font = workbook.createFont();// 设置字体大小
font.setFontName("黑体");
font.setFontHeightInPoints((short) 10);
HSSFFont font2 = workbook.createFont();
font2.setFontName("黑体");
font2.setFontHeightInPoints((short) 20);// 设置标题字体大小
HSSFFont font3 = workbook.createFont();
font3.setFontName("黑体");
font3.setFontHeightInPoints((short) 14);// 设置小标题字体大小
setTitleBorder.setFont(font2);
setTitleBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER);
setBorder.setFont(font);
setBorder.setWrapText(true);
setTitleBorder1.setFont(font3);
/*****设置单元格格式 end*****/
HSSFSheet sheet = workbook.createSheet();
// 设置列宽
int[] columnWidths = getColumnWidths();
for (int i = 0; i < columnWidths.length; i++)
{
sheet.setColumnWidth(i, columnWidths[i]);
}
/******合并单元格*****/
CellRangeAddress region1 = new CellRangeAddress(0, 0, (short) 0, (short) 17);
CellRangeAddress region2 = new CellRangeAddress(1, 1, (short) 0, (short) 7);
sheet.addMergedRegion(region1);
sheet.addMergedRegion(region2);
/******合并单元格 end*****/
/****** 第1行 ********/
HSSFRow titleRow = sheet.createRow(0);
titleRow.setHeight((short) 800);
HSSFCell fcell = titleRow.createCell(0);
fcell.setCellValue(getMonth() + " 统计汇总表");
fcell.setCellStyle(setTitleBorder);
/****** 第2行 ********/
HSSFRow titleRow1 = sheet.createRow(1);
titleRow1.setHeight((short) 450);
HSSFCell fcell1 = titleRow1.createCell(0);
fcell1.setCellValue("填报单位:xxx");
fcell1.setCellStyle(setTitleBorder1);
/****** 第3行 ********/
HSSFRow firstRow = sheet.createRow(2);
firstRow.setHeight((short) 800);
HSSFCell cell;
int cellIndex = 0;
String[] titles = excelTitles();
for (String field : titles)
{
// 在索引0的位置创建单元格(左上端)
cell = firstRow.createCell(cellIndex);
// 定义单元格为字符串类型
cell.setCellStyle(setBorder);
cell.setCellValue(field);
cellIndex++;
}
/****** 第4行 ********/
int r = 3;
for (StatMonthResp statMonthResp : statMonthRespList)
{
String[] datas = excelData(statMonthResp);
HSSFRow secondRow = sheet.createRow(r);
for (int i = 0; i < datas.length; i++)
{
cell = secondRow.createCell(i);
cell.setCellStyle(setBorder);
cell.setCellValue(datas[i]);
}
r++;
}
// 新建一输出文件流
FileOutputStream fOut = new FileOutputStream(request.getRealPath("/") + url);
// 把相应的Excel 工作簿存盘
workbook.write(fOut);
fOut.flush();
// 操作结束,关闭文件
IOUtils.close(fOut);
System.out.println("文件生成...");
} catch (Exception e)
{
}
private int[] getColumnWidths()
{
int[] columnWidths = new int[4];
columnWidths[0] = 2000;
columnWidths[1] = 2000;
columnWidths[2] = 3000;
columnWidths[3] = 2000;
return columnWidths;
}
private String[] excelTitles()
{
String[] titles = new String[4];
titles[0] = "单位";
titles[1] = "车辆总数";
titles[2] = "其中已安装GPS车辆数";
titles[3] = "GPS安装率";
return titles;
}
private String[] excelData(StatMonthResp statMonthResp)
{
int c = 0;
String[] datas = new String[4];
datas[c++] = "";
datas[c++] = String.valueOf(statMonthResp.getCars());
datas[c++] = String.valueOf(statMonthResp.getGpsCars());
datas[c++] = String.valueOf(statMonthResp.getGpsRate());
return datas;
}