CellStyle决定了单元格的显示样式,决定了单元格:
- 单元格各个边框样式及颜色
- 单元格填充图案、前景色及背景色
- 单元格内容水平、垂直对齐方式
- 单元格内容字体Font
- 单元格内容格式化DataFormat
- 单元格内容是否换行
- 单元格内容旋转
工作簿Workbook中单元格样式个数是有限制的,所以在 程序中应该重复使用相同CellStyle,而不是为每个单元 格创建一个CellStyle
HSSFCellStyle - 4000个
XSSFCellStyle - 64000个
1. workbook创建一个单元格样式cellStyle
2. 设置属性
3. 单元格设置样式cellStyle
CellStyle style = workbook.createCellStyle();
cell.setCellStyle(style);
package hssf.sheet.color;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
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.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExportXSSFColor {
public static void main(String[] args) throws Exception {
File file = new File("C:\\Users\\Administrator\\Desktop\\test.xlsx");
if (file.exists()) {
file.delete();
}
BufferedOutputStream out = null;
try {
FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test.xlsx");
out = new BufferedOutputStream(fileOutputStream);
exportExcel(out);
} finally {
out.close();
}
}
/**
* @param out
* @throws IOException
*/
private static void exportExcel(BufferedOutputStream out) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
sheet.setColumnWidth(0, 20*256);
Row row = sheet.createRow((short) 0);
Cell cell = row.createCell((short) 0);
cell.setCellValue("调色板");
// 创建一个单元格样式
XSSFCellStyle style = workbook.createCellStyle();
cell.setCellStyle(style);
/*****************************使用默认颜色**************************************************/
// 填充色
// style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
style.setFillForegroundColor(HSSFColorPredefined.BLUE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// 字体颜色
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
style.setFont(font);
/**************************************************************************************/
/*****************************自定义颜色**************************************************/
XSSFColor color = new XSSFColor(new java.awt.Color(255, 0, 0));
style.setFillForegroundColor(color);
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
XSSFColor color1 = new XSSFColor(new java.awt.Color(0, 0, 0));
// 字体颜色
XSSFFont font1 = workbook.createFont();
font1.setColor(color1);
style.setFont(font1);
/**************************************************************************************/
workbook.write(out);
}
}
在工作中碰巧遇到一种情况卡了好久,当更改单元格样式,会将全部样式都修改掉,如何根据业务要求设置特定的样式呢?
XSSFCell cell = sheet_unit.getRow(writerow).createCell(maxcolumnindex);//注意单元格必须创建
XSSFCellStyle cellStyle_date = book.createCellStyle();
cellStyle_date.cloneStyleFrom(cell.getCellStyle());
XSSFFont font = book.createFont();
font.setFontName("Arial");
font.setFontHeight(10);
cellStyle_date.setFont(font);
cellStyle_date.setAlignment(HorizontalAlignment.CENTER);
cellStyle_date.setBorderBottom(BorderStyle.THIN);
cellStyle_date.setBorderTop(BorderStyle.THIN);
cellStyle_date.setBorderLeft(BorderStyle.THIN);
cellStyle_date.setBorderRight(BorderStyle.THIN);
cell.setCellStyle(cellStyle_date);