且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

【web开发】☆★之利用POI操作Excel表格系列教程【8】设置单元格对其方式

更新时间:2021-09-23 20:54:09

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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); // 设置单元格样式
    }
}



【web开发】☆★之利用POI操作Excel表格系列教程【8】设置单元格对其方式










本文转自 小夜的传说 51CTO博客,原文链接:http://blog.51cto.com/1936625305/1408587,如需转载请自行联系原作者