您的位置:首页 > Web前端 > CSS

POI简易帮助文档--给Excel设置样式

2015-06-17 14:14 591 查看
 正如Html需要CSS一样,我们的POI生成的Excel同样需要样式才能更完美的表现我们的数据。下面还是从简单的例子出发,学习和了解POI的样式设计。

  一、我的位置。

package com.myjava.poi;

import java.io.FileOutputStream;
import java.util.Date;

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.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class ExcelStyle {

public static void main(String[] args) throws Exception{
Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
Row row=sheet.createRow(2); // 创建一个行
row.setHeightInPoints(30);

createCell(wb, row, (short)0, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_BOTTOM);
createCell(wb, row, (short)1, HSSFCellStyle.ALIGN_FILL, HSSFCellStyle.VERTICAL_CENTER);
createCell(wb, row, (short)2, HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_TOP);
createCell(wb, row, (short)3, HSSFCellStyle.ALIGN_RIGHT, HSSFCellStyle.VERTICAL_TOP);

FileOutputStream fileOut=new FileOutputStream("D:\\工作簿.xls");
wb.write(fileOut);
fileOut.close();
}

/**
* 创建一个单元格并为其设定指定的对齐方式
* @param wb 工作簿
* @param row 行
* @param column  列
* @param halign  水平方向对其方式
* @param valign  垂直方向对其方式
*/
private static void createCell(Workbook wb,Row row,short column,short halign,short valign){
Cell cell=row.createCell(column);  // 创建单元格
cell.setCellValue(new HSSFRichTextString("我在这"));  // 设置值
CellStyle cellStyle=wb.createCellStyle(); // 创建单元格样式
cellStyle.setAlignment(halign);  // 设置单元格水平方向对其方式
cellStyle.setVerticalAlignment(valign); // 设置单元格垂直方向对其方式
cell.setCellStyle(cellStyle); // 设置单元格样式
}

}


  

效果显示:

  


  二、我的边框

package com.myjava.poi;

import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Border {

public static void main(String[] args) throws Exception{
Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
Row row=sheet.createRow(1); // 创建一个行

Cell cell=row.createCell(1); // 创建一个单元格
cell.setCellValue(4);

CellStyle cellStyle=wb.createCellStyle();
cellStyle.setBorderBottom(CellStyle.BORDER_THIN); // 底部边框
cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 底部边框颜色

cellStyle.setBorderLeft(CellStyle.BORDER_THIN);  // 左边边框
cellStyle.setLeftBorderColor(IndexedColors.RED.getIndex()); // 左边边框颜色

cellStyle.setBorderRight(CellStyle.BORDER_THIN); // 右边边框
cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex());  // 右边边框颜色

cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); // 上边边框
cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());  // 上边边框颜色

cell.setCellStyle(cellStyle);
FileOutputStream fileOut=new FileOutputStream("D:\\Border.xls");
wb.write(fileOut);
fileOut.close();
}
}


  

效果显示:

  


  三、我的背景

package com.myjava.poi;

import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Bg {

public static void main(String[] args) throws Exception{
Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
Row row=sheet.createRow(1); // 创建一个行

Cell cell=row.createCell(1);
cell.setCellValue("看不清我");
CellStyle cellStyle=wb.createCellStyle();
cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); // 背景色
cellStyle.setFillPattern(CellStyle.BIG_SPOTS);
cell.setCellStyle(cellStyle);

Cell cell2=row.createCell(2);
cell2.setCellValue("我的前景色与众不同");
CellStyle cellStyle2=wb.createCellStyle();
cellStyle2.setFillForegroundColor(IndexedColors.RED.getIndex()); // 前景色
cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell2.setCellStyle(cellStyle2);

FileOutputStream fileOut=new FileOutputStream("D:\\bg.xls");
wb.write(fileOut);
fileOut.close();
}
}


  

效果显示:

  


  四、合并单元格

package com.myjava.poi;

import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;

public class GetTogether {

public static void main(String[] args) throws Exception{
Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
Row row=sheet.createRow(1); // 创建一个行

Cell cell=row.createCell(1);
cell.setCellValue("我们被合并单元格啦!");

sheet.addMergedRegion(new CellRangeAddress(
1, // 起始行
2, // 结束行
1, // 其实列
2  // 结束列
));

FileOutputStream fileOut=new FileOutputStream("D:\\Together.xls");
wb.write(fileOut);
fileOut.close();
}
}


  

效果显示:

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: