確定excel類型的:
HSSFWorkbook wb = new HSSFWorkbook(); //創建HSSFWorkbook 對象
HSSFSheet sheet = wb.createSheet("new sheet"); //創建一個新的excel表格
//遍歷新創建的excel表格
for(Iterator rit=sheet.rowIterator();rit.hasNext();){
HSSFRow row = (HSSFRow)rit.next(); //獲取每excel的每一行
// row.setHeight((short) (25 * 20))
// row.setHeightInPoints(20) ////heightInPoints 設置的值是height屬性值的20倍
for(Iterator cit = row.cellIterator();cit.hasNext();){ //遍歷每一行
HSSFCell cell = (HSSFCell)cit.next(); //獲取行中的每一個單元格
cell.setCellValue("hello"); //給每一行中設置“hello” 內容
}
}
下面陳列EXCEL表格樣式相關的內容:
HSSFCellStyle cellStyle = wb.createCellStyle(); //創建設置EXCEL表格樣式對象 cellStyle
一、設置背景色:
cellStyle.setFillForegroundColor((short) 13);// 設置背景色
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
具體顏色可以參照:http://blog.csdn.net/qq_27937043/article/details/72779442
二、設置邊框:
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下邊框
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框
三、設置居中:
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
cellStyle.setAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
cellStyle.setAlignment(HSSFCellStyle.VERTICAL_BOTTOM);//垂直底部
cellStyle.setAlignment(HSSFCellStyle.VERTICAL_TOP);//垂直頂部
四、設置字體:
HSSFFont font = wb.createFont();
font.setFontName("黑體");
font.setFontHeightInPoints((short) 16);//設置字體大小
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗體顯示
cellStyle.setFont(font);//選擇創建的字體格式
五、設置列寬:
sheet.setColumnWidth(0, 3766);
//第一個參數代表列id(從0開始),第2個參數代表寬度值 參考 :"2017-06-01"的寬度爲2500
六、設置自動換行:
cellStyle.setWrapText(true);//設置自動換行
七、合併單元格:
方法1. 此方法在POI3.8中已經被廢棄,建議使用方法2
Region region1 = new Region(0, (short) 0, 0, (short) 6);//參數1:行號 參數2:起始列號 參數3:行號 參數4:終止列號
方法2
CellRangeAddress region1 = new CellRangeAddress(rowNumber, rowNumber, (short) 0, (short) 11);
//參數1:起始行 參數2:終止行 參數3:起始列 參數4:終止列
但應注意兩個構造方法的參數不是一樣的,具體使用哪個取決於POI的不同版本。
sheet.addMergedRegion(region1);
常用方法記錄:
public static void setWorkbookStyle(Workbook workbook,int columnLength){
try {
//第一個sheet
Sheet sheet = workbook.getSheetAt(0);
//設置列寬
for (int i = 0; i < columnLength; i++) {
sheet.setColumnWidth((short)i,65 * 256 + 200);
}
//設置第一行格式
Row row = sheet.getRow(0);
row.setHeight((short) (30 * 20));
CellStyle style = workbook.createCellStyle();
style.setFillPattern(FillPatternType.BRICKS);
style.setFillForegroundColor(IndexedColors.RED.getIndex());// 設置背景色
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setBorderBottom(BorderStyle.THIN); //下邊框
style.setBorderLeft(BorderStyle.THIN);//左邊框
style.setBorderTop(BorderStyle.THIN);//上邊框
style.setBorderRight(BorderStyle.THIN);//右邊框
style.setAlignment(HorizontalAlignment.CENTER); // 水平居中
style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
Font font = workbook.createFont();
font.setFontName("黑體");
font.setFontHeightInPoints((short) 22);//設置字體大小
font.setBold(true);
style.setFont(font);
//遍歷每一行的cell
for(Iterator cit = row.cellIterator(); cit.hasNext();){
Cell cell = (Cell)cit.next();
cell.setCellStyle(style);
}
workbook.setActiveSheet(0);
}catch (Exception e){
log.error("---設置excel格式錯誤!");
}
}
/**
* @description Workbook 轉 InputStream 流
* @author sgl
*/
public static InputStream workbookConvertorStream(Workbook workbook) {
InputStream in = null;
try{
//臨時緩衝區
ByteArrayOutputStream out = new ByteArrayOutputStream();
//創建臨時文件
workbook.write(out);
byte [] bookByteAry = out.toByteArray();
in = new ByteArrayInputStream(bookByteAry);
}catch (Exception e){
log.error("轉換錯誤");
}
return in;
}
/**
* @description InputStream流 轉 二進制數組
* @author sgl
*/
public static byte [] inputStreamToByte(InputStream is) throws IOException {
ByteArrayOutputStream bAOutputStream = new ByteArrayOutputStream();
int ch;
while((ch = is.read() ) != -1){
bAOutputStream.write(ch);
}
byte data [] =bAOutputStream.toByteArray();
bAOutputStream.close();
return data;
}