標籤:poi設定儲存格對其方式
【web開發】☆★之利用POI操作Excel表格系列教程【8】設定儲存格對其方式
package csg.xiaoye.poidemo;import java.io.FileOutputStream;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFRichTextString;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;public class Dspace { public static void main(String[] args) throws Exception { Workbook wb = new HSSFWorkbook(); // 定義一個新的活頁簿 Sheet sheet = wb.createSheet("第一個Sheet頁"); // 建立第一個Sheet頁 Row row = sheet.createRow(2); // 建立一個行 row.setHeightInPoints(30); createCell(wb, row, (short) 0, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_BOTTOM); createCell(wb, row, (short) 1, HSSFCellStyle.ALIGN_FILL, HSSFCellStyle.VERTICAL_CENTER); createCell(wb, row, (short) 2, HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_TOP); createCell(wb, row, (short) 3, HSSFCellStyle.ALIGN_RIGHT, HSSFCellStyle.VERTICAL_TOP); FileOutputStream fileOut = new FileOutputStream("d:\\小夜.xls"); wb.write(fileOut); fileOut.close(); } /** * 建立一個儲存格並為其設定指定的對其方式 * * @param wb活頁簿 * @param row 行 * @param column列 * @param halign水平方向對其方式 * @param valign垂直方向對其方式 */ private static void createCell(Workbook wb, Row row, short column, short halign, short valign) { Cell cell = row.createCell(column); // 建立儲存格 cell.setCellValue(new HSSFRichTextString("xiaoye")); // 設定值 CellStyle cellStyle = wb.createCellStyle(); // 建立儲存格樣式 cellStyle.setAlignment(halign); // 設定儲存格水平方向對其方式 cellStyle.setVerticalAlignment(valign); // 設定儲存格垂直方向對其方式 cell.setCellStyle(cellStyle); // 設定儲存格樣式 }}
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/26/90/wKioL1NrfO6TFRJuAABuvjThQls181.jpg" title="1.png" alt="wKioL1NrfO6TFRJuAABuvjThQls181.jpg" />
本文出自 “諾言永遠依戀小柴、、、” 部落格,請務必保留此出處http://1936625305.blog.51cto.com/6410597/1408587