import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
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;
public class CreateCells {
/**
* HSSFWorkbook excell的文档对象 HSSFSheet excell的表单 HSSFRow excell的行 HSSFCell
* excell的格子单元 HSSFFont excell字体 HSSFName 名称 HSSFDataFormat 日期格式 HSSFHeader
* sheet头 HSSFFooter sheet尾 HSSFCellStyle cell样式
*/
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
// 建立新HSSFWorkbook对象
HSSFSheet sheet = wb.createSheet("new sheet");
// 建立新的sheet对象
// Create a row and put some cells in it.Rows are 0 based.
HSSFRow row = sheet.createRow((short) 0);
// 建立新行
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short) 0);
// 建立新cell
cell.setCellValue(1);// 设置cell的整数类型的值
// Or do it on one line.
row.createCell((short) 1).setCellValue(1.2);
// 设置cell浮点类型的值
row.createCell((short) 2).setCellValue("test");
// 设置cell字符类型的值
row.createCell((short) 3).setCellValue(true);
// 设置cell布尔类型的值
HSSFCellStyle cellStyle = wb.createCellStyle();
// 建立新的cell样式
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
// 设置cell样式为定制的日期格式
HSSFCell dCell = row.createCell((short) 4);
dCell.setCellValue(new Date());
// 设置cell为日期类型的值
dCell.setCellStyle(cellStyle);
// 设置该cell日期的显示格式
HSSFCell csCell = row.createCell((short) 5);
csCell.setEncoding(HSSFCell.ENCODING_UTF_16);
// 设置cell编码解决中文高位字节截断
csCell.setCellValue("中文测试_Chinese Words Test");
// 设置背景色
HSSFCellStyle style = wb.createCellStyle();
style
.setFillForegroundColor(new HSSFColor.GREY_25_PERCENT()
.getIndex());
style
.setFillBackgroundColor(new HSSFColor.GREY_25_PERCENT()
.getIndex());
style.setFillPattern(HSSFCellStyle.SPARSE_DOTS);
HSSFCell cell1 = row.createCell((short) 6);
cell1.setCellValue("X");
cell1.setCellStyle(style);
// 设置背景色
HSSFCellStyle style1 = wb.createCellStyle();
style1.setFillForegroundColor(new HSSFColor.GREY_40_PERCENT()
.getIndex());
style1.setFillBackgroundColor(new HSSFColor.GREY_40_PERCENT()
.getIndex());
style1.setBorderBottom((short) 1);
style1.setBorderTop((short) 1);
style1.setBorderLeft((short) 1);
style1.setBorderRight((short) 1);
/**
* 注意这句代码, style1.setFillPattern, 如果你在你的程序中不设置fill pattern,那么
* 你上面设置的前景色和背景色就显示不出来.网络上很多文章都没有设置fillpattern, 不知道那些达人 的机器是不是比我的机器智能很多.
*/
style1.setFillPattern(HSSFCellStyle.SPARSE_DOTS);
HSSFCell cell11 = row.createCell((short) 7);
cell11.setCellValue("X11");
cell11.setCellStyle(style1);
// 数字格式化
HSSFCellStyle st = wb.createCellStyle();
// 建立新的cell样式
st.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0"));
HSSFCell cell12 = row.createCell((short) 8);
cell12.setCellValue((double) 10000000);
cell12.setCellStyle(st);
// 设置中西文结合字符串
row.createCell((short) 9).setCellType(HSSFCell.CELL_TYPE_ERROR);
// 建立错误cell
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("c:/workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}
第一次,不知道怎么插入图片,附件就是运行得到的EXCEL的截图