package test;
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.HSSFFont;
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.util.CellRangeAddress;
public class CreateCells {
/**
* 文檔對象 HSSFWorkbook ;表單對象 HSSFSheet ;行對象 HSSFRow ;列對象 HSSFCell
* excell的格子單元 HSSFFont excell字體 HSSFName 名稱 HSSFDataFormat 日期格式 HSSFHeader
* sheet頭 HSSFFooter sheet尾 HSSFCellStyle cell樣式
*/
public static void main(String[] args) throws IOException {
// 建立新HSSFWorkbook對象
HSSFWorkbook workbook = new HSSFWorkbook();
// 建立新的sheet對象
// Create a row and put some cells in it.Rows are 0 based.
HSSFSheet sheet = workbook.createSheet("表單1");
// 建立新行
// Create a cell and put a value in it.
HSSFRow row = sheet.createRow((short) 0);
//修改當前行 默認行高 列寬
//行高
sheet.setDefaultRowHeightInPoints(10);
//列款寬
sheet.setDefaultColumnWidth(10);
//設置特定單元格的寬度
sheet.setColumnWidth(4, 20*256);
sheet.setColumnWidth(5, 30*256);
sheet.setColumnWidth(6, 30*256);
// 整數類型的cell樣式
//HSSFDataFormat.getBuiltinFormat("0.00") 字符串的內容是 Excel有的格式
HSSFCellStyle numStyle = workbook.createCellStyle();
numStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0"));
//創建1列
HSSFCell cellNum = row.createCell(0);
cellNum.setCellValue(1);
cellNum.setCellStyle(numStyle);
// 浮點類型的cell樣式
HSSFCellStyle doubleStyle = workbook.createCellStyle();
doubleStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
HSSFCell cellDouble = row.createCell(1);
cellDouble.setCellValue(1.2);
cellDouble.setCellStyle(doubleStyle);
//字符串類型的cell樣式
HSSFCellStyle stringStyle = workbook.createCellStyle();
stringStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("G/通用格式"));
HSSFCell cellString= row.createCell(2);
cellString.setCellValue("test");
cellString.setCellStyle(stringStyle);
//添加cell布爾類型的值
row.createCell(3).setCellValue(true);
//日期類型的cell樣式 yyyy-m-d h:mm:ss AM/PM
HSSFCellStyle dateStyle = workbook.createCellStyle();
dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
HSSFCell dCell = row.createCell(4);
dCell.setCellValue(new Date());
dCell.setCellStyle(dateStyle);
//設置cell編碼解決中文高位字節截斷
HSSFCell csCell = row.createCell(5);
csCell.setCellType(HSSFCell.ENCODING_UTF_16);
csCell.setCellValue("中文測試_Chinese Words Test");
// 設置 背景色 邊框
HSSFCellStyle style1 = workbook.createCellStyle();
//前景色和後景色都要有 否則會出網格
style1.setFillForegroundColor(new HSSFColor.YELLOW().getIndex());
style1.setFillBackgroundColor(new HSSFColor.YELLOW().getIndex());
//設置邊框
style1.setBorderBottom((short) 1);
style1.setBorderTop((short) 1);
style1.setBorderLeft((short) 1);
style1.setBorderRight((short) 1);
//問題:用poi將一個cell中的字體設置成了紅色,結果用excell打開後,這個cell中只有前面一個或幾個字爲紅色
//HSSFFont font = workbook.createFont(); font.setColor(HSSFFont.COLOR_RED);
//先從Cell中把HSSFRichTextString取出來
//然後HSSFRichTextString對象.applyFont(font)
//最後再把HSSFRichTextString對象set回到cell中就行了。。。。。
//設置字體樣式=====================================
HSSFFont font = workbook.createFont();
//字體位置 上 下 左 右
//font.setTypeOffset((short)0);
//字體寬度
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
//字體高度
font.setFontHeightInPoints((short)8);
//字體顏色
font.setColor(HSSFFont.COLOR_RED);
//=================================================
style1.setFont(font);
/**
* 注意這句代碼, style1.setFillPattern, 如果你在你的程序中不設置fill pattern,那麼
* 你上面設置的前景色和背景色就顯示不出來.網絡上很多文章都沒有設置fillpattern
* 如果不改變樣式 不需要添加(如:居中)
*/
style1.setFillPattern(HSSFCellStyle.SPARSE_DOTS);
HSSFCell cellCH = row.createCell(6);
cellCH.setCellValue("中文測試_Chinese Words Testsss");
cellCH.setCellStyle(style1);
//貨幣樣式
HSSFCellStyle moneyStyle = workbook.createCellStyle();
moneyStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0"));
HSSFCell cell12 = row.createCell(7);
cell12.setCellValue((double) 10000000);
cell12.setCellStyle(moneyStyle);
// 錯誤顯示
row.createCell(8).setCellType(HSSFCell.CELL_TYPE_ERROR);
//合併單元格
int startRowNo=0;
int endRowNo=0;
int startCellNo=9;
int endCellNo=10;
sheet.addMergedRegion(new CellRangeAddress(startRowNo, endRowNo,startCellNo, endCellNo));
HSSFCell cell = row.createCell(9);
cell.setCellValue("合併");
//即垂直居中對齊且水平居中對齊 居中後背景顏色變化了
HSSFCellStyle style = workbook.createCellStyle();
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平
//如果不改變樣式 不需要添加
//style.setFillPattern(HSSFCellStyle.SPARSE_DOTS);
cell.setCellStyle(style);
FileOutputStream fileOut = new FileOutputStream("e:/workbook.xls");
workbook.write(fileOut);
fileOut.close();
}
}