POI结构
HSSF - 提供读写Microsoft Excel XLS格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。
HWPF - 提供读写Microsoft Word DOC格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读Microsoft Visio格式档案的功能。
HPBF - 提供读Microsoft Publisher格式档案的功能。
HSMF - 提供读Microsoft Outlook格式档案的功能。
步骤一:访问apache网站,下载poi.Jar的类库
源代码
package com.txfy.PoiDemo;
import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.Date;
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.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class Createcell {
public static void main(String args[]) throws Exception
{
Workbook workbook = new HSSFWorkbook();//创建一个工作簿
FileOutputStream fiout = new FileOutputStream("c:\\工作簿.xls");
Sheet sheet = workbook.createSheet("第一个sheet");//创建一个工作表
Row row = sheet.createRow(0);//创建第一行,参数0表示行的索引,第一行
Cell cell = row.createCell(0);//创建第一个单元格,参数0表示列的索引,第一列
//给单元格设置值
cell.setCellValue("你好");//给单元格设置字符串值
row.createCell(1).setCellValue(1);//给单元格设置整型值
row.createCell(2).setCellValue(1.3);//给单元格设置浮点型值
row.createCell(3).setCellValue(false);//给单元格设置布尔型值
//给单元格设置样式
CellStyle cs = workbook.createCellStyle();
CreationHelper ch = workbook.getCreationHelper();//获得工作簿的样式帮助类
//对单元格日期的类型进行格式
cs.setDataFormat(ch.createDataFormat().getFormat("yyyy-mm-dd hh:mm:ss"));
//设置样式的语句在设置单元格的值前后均有效,一样的效果
cell =row.createCell(4);
cell.setCellStyle(cs);
cell.setCellValue(new Date());
cell = row.createCell(5);
cell.setCellValue(Calendar.getInstance());
cell.setCellStyle(cs);
//将内存中工作簿的内容写到磁盘中
workbook.write(fiout);
fiout.close();
}
}