---
title: "java POI"
author: "test_touch"
date: "2012-04-19T22:45:42Z"
tags: ["Excel", "POI"]
categories: ["编程语言"]
slug: "java-poi"
draft: false
lastmod: "2012-04-19T22:45:42Z"
id: 1490834
url: "https://test-touch.iteye.com/blog/1490834"
---

使用这套API操作Excel时,将对Excel进行全程锁定,所以不能有其他程序访问同一文件.

```
package poi;
```

```
 
```

```
import java.io.FileOutputStream;
```

```
import java.util.Calendar;
```

```
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.HSSFRichTextString;
```

```
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;
```

```
 
```

```
public class App {
```

```
 
```

```
    public static void main(String[] args) {
```

```
        try {
```

```
            FileOutputStream fos = new FileOutputStream("d:/poi.xls");
```

```
            // 建立工作空间
```

```
            HSSFWorkbook wb = new HSSFWorkbook();
```

```
            // 创建工作表
```

```
            HSSFSheet sheet = wb.createSheet("first Sheet");
```

```
            wb.createSheet("second Sheet");
```

```
            // 创建行
```

```
            HSSFRow row = sheet.createRow(0);
```

```
            // 创建单元格
```

```
            HSSFCell cell = row.createCell(0);
```

```
            // 设置不同的值进行比较
```

```
            cell.setCellValue(true);// boolean
```

```
            row.createCell(1).setCellValue(Calendar.getInstance());// 日历类型
```

```
            row.createCell(2).setCellValue(new Date());// date类型
```

```
            row.createCell(3).setCellValue(123456789.987654321);// double
```

```
            String str = "sadfasdfsadfsadfsdddddddddddffffffffffffffffdsadf";
```

```
            row.createCell(4).setCellValue(new HSSFRichTextString(str));// String
```

```
 
```

```
            // 格式化单元格数据
```

```
            HSSFCellStyle style = wb.createCellStyle();// 创建样式对象
```

```
            HSSFDataFormat format = wb.createDataFormat();// 创建数据格式对象
```

```
            style.setDataFormat(format.getFormat("yyyy-MM-dd hh:mm:ss"));
```

```
            row.getCell(1).setCellStyle(style);
```

```
            row.getCell(2).setCellStyle(style);
```

```
 
```

```
            style = wb.createCellStyle();
```

```
            style.setDataFormat(format.getFormat("#,###.0000"));
```

```
            row.getCell(3).setCellStyle(style);
```

```
 
```

```
            // 设置列宽,单位int:是点的1/20
```

```
            sheet.setColumnWidth(1, 5000);
```

```
            sheet.autoSizeColumn((short) 2);// 自动列宽
```

```
            sheet.autoSizeColumn((short) 4);
```

```
 
```

```
            // 设置文本的对齐方式
```

```
            row = sheet.createRow(1);
```

```
            row.createCell(0).setCellValue(new HSSFRichTextString("左上"));
```

```
            row.createCell(1).setCellValue(new HSSFRichTextString("中中"));
```

```
            row.createCell(2).setCellValue(new HSSFRichTextString("右下"));
```

```
            // #左上
```

```
            style = wb.createCellStyle();
```

```
            style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 水平左
```

```
            style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 水平左
```

```
            row.getCell(0).setCellStyle(style);
```

```
            // #中中
```

```
            style = wb.createCellStyle();
```

```
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
```

```
            style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
```

```
            row.getCell(1).setCellStyle(style);
```

```
            // #左上
```

```
            style = wb.createCellStyle();
```

```
            style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
```

```
            style.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM);
```

```
            row.getCell(2).setCellStyle(style);
```

```
 
```

```
            // 行高
```

```
            row.setHeight((short) 2000);
```

```
 
```

```
            // 字体颜色和大小
```

```
            HSSFFont font = wb.createFont();
```

```
            font.setFontHeightInPoints((short) 30);// 字体高度
```

```
            font.setItalic(true);// 斜体
```

```
            font.setColor(HSSFColor.RED.index);// 字体颜色
```

```
 
```

```
            style = row.getCell(0).getCellStyle();
```

```
            style.setFont(font);// 为单元格样式应用字体
```

```
            sheet.setColumnWidth(0, (short) 5000);
```

```
 
```

```
            // 设置旋转
```

```
            style.setRotation((short) 60);
```

```
 
```

```
            // 设置边框样式
```

```
            row = sheet.createRow(2);
```

```
            cell = row.createCell(0);
```

```
            style = wb.createCellStyle();
```

```
            style.setTopBorderColor(HSSFColor.RED.index);// 上边框
```

```
            style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT);
```

```
            cell.setCellStyle(style);
```

```
 
```

```
            // 计算列
```

```
            row = sheet.createRow(3);
```

```
            row.createCell(0).setCellValue(12);
```

```
            row.createCell(1).setCellValue(13);
```

```
            row.createCell(2).setCellValue(14);
```

```
 
```

```
            cell = row.createCell(3);
```

```
            cell.setCellFormula("average(A4:C4)");
```

```
 
```

```
            row = sheet.createRow(4);
```

```
            row.createCell(0).setCellValue(22);
```

```
            row.createCell(1).setCellValue(23);
```

```
            row.createCell(2).setCellValue(24);
```

```
            cell = row.createCell(3);
```

```
            cell.setCellFormula("sum(A4:C5)");
```

```
 
```

```
            // 整体移动行
```

```
            // sheet.shiftRows(2, 4, -1);
```

```
 
```

```
            // 拆分窗格
```

```
            // 1000:x轴拆分距离 2000:y轴拆分距离
```

```
            // 1:右侧窗格开始显示的列的索引数 2:下策窗口开始显示的行的索引的数
```

```
            // 2:哪个面板去激活
```

```
            // sheet.createSplitPane(1000, 2000, 1, 2, 2);
```

```
 
```

```
            // 冻结窗格
```

```
            // 1:冻结的列数 2:冻结行数 3:右侧窗格从第几列开始显示 4:下侧窗格从第几行开始显示
```

```
            // sheet.createFreezePane(1, 2, 3, 4);
```

```
            wb.write(fos);
```

```
            fos.close();
```

```
        } catch (Exception e) {
```

```
            e.printStackTrace();
```

```
        }
```

```
    }
```

```
}
```