POI的入门:单元格样式处理
生活随笔
收集整理的這篇文章主要介紹了
POI的入门:单元格样式处理
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
設置格式?
//創(chuàng)建單元格樣式對象 CellStyle cellStyle = wb.createCellStyle();//設置邊框 cellStyle.setBorderBottom(BorderStyle.DASH_DOT);//下邊框 cellStyle.setBorderTop(BorderStyle.HAIR);//上邊框//設置字體 Font font = wb.createFont();//創(chuàng)建字體對象 font.setFontName("華文行楷");//設置字體 font.setFontHeightInPoints((short)28);//設置字號 cellStyle.setFont(font);//設置寬高 sheet.setColumnWidth(0, 31 * 256);//設置第一列的寬度是31個字符寬度 row.setHeightInPoints(50);//設置行的高度是50個點//設置居中顯示 cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中 cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中//設置單元格樣式 cell.setCellStyle(cellStyle);//合并單元格 CellRangeAddress region =new CellRangeAddress(0, 3, 0, 2); sheet.addMergedRegion(region); package com.learn.poi.test;import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.FileOutputStream;/*** 單元格樣式處理*/ public class PoiTest03 {public static void main(String[] args) throws Exception {//創(chuàng)建工作簿 HSSFWorkbook -- 2003Workbook wb = new XSSFWorkbook(); //2007版本//創(chuàng)建表單sheetSheet sheet = wb.createSheet("test");//創(chuàng)建行對象 參數(shù):索引(從0開始)Row row = sheet.createRow(2);//創(chuàng)建單元格對象 參數(shù):索引(從0開始)Cell cell = row.createCell(2);//向單元格中寫入內容cell.setCellValue("你好世界");//樣式處理//創(chuàng)建樣式對象CellStyle style = wb.createCellStyle();style.setBorderTop(BorderStyle.THIN);//上邊框style.setBorderBottom(BorderStyle.THIN);//下邊框style.setBorderLeft(BorderStyle.THIN);//左邊框style.setBorderRight(BorderStyle.THIN);//右邊框//創(chuàng)建字體對象Font font = wb.createFont();font.setFontName("華文行楷"); //字體font.setFontHeightInPoints((short)28);//字號style.setFont(font);//行高和列寬row.setHeightInPoints(50);//行高//列寬的寬度 字符寬度sheet.setColumnWidth(2,31 * 256);//列寬//劇中顯示style.setAlignment(HorizontalAlignment.CENTER);//水平居中style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中//向單元格設置樣式cell.setCellStyle(style);//文件流FileOutputStream pis = new FileOutputStream("C:\\Users\\leon\\Desktop\\00\\test2.xlsx");//寫入文件wb.write(pis);pis.close();} }?
總結
以上是生活随笔為你收集整理的POI的入门:单元格样式处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Redis与Zookeeper实现分布式
- 下一篇: POI的入门:绘制图形