这是自己通过Java对POI的Excel所写的一个小Demo,有兴趣的可以看一看
package test;
import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.Date;
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;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
/**
*
* Title: TestJunit.java
* Description: java对POI的Excel操作
* @author liushaocheng
* @created 2016年6月5日 下午9:01:20
*/
public class TestJunit {
/**
*
* @description 创建Excel文件
* @author liushaocheng
* @throws Exception
* @created 2016年5月30日 上午11:53:51
*/
public void CreateWorkBook() throws Exception {
/**
* 这里说明一下,POI对Excel文件的操作提供了两个对象
* 一个是下面所写的XSSFWorkbook对象,另一个是HSSFWorkbook对象
* 它们的区别主要在于前者与MS-Office版本2007或更高版本兼容。后者与微软Office97-2003版本兼容。
* 另外,如果创建的文件有错,不要担心,只需要在创建工作薄的时候创建工作表就不会有错了
*/
Workbook wb = new XSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("D:\\workbook.xlsx");
wb.write(fileOut);
fileOut.close();
}
/**
*
* @description 创建工作表
* @author liushaocheng
* @created 2016年5月30日 下午4:56:15
* @throws Exception
*/
public void createSheet() throws Exception {
XSSFWorkbook wb = new XSSFWorkbook();
Sheet sheetOne = wb.createSheet("sheet1");
Sheet sheetTwo = wb.createSheet("sheet2");
FileOutputStream fileOut = new FileOutputStream("D:\\workbook.xlsx");
wb.write(fileOut);
fileOut.close();
}
/**
*
* @description 创建单元格并写入数据
* @author liushaocheng
* @throws Exception
* @created 2016年5月30日 下午5:38:39
*/
public void CreateCells() throws Exception {
XSSFWorkbook wb = new XSSFWorkbook();
CreationHelper creationHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue(0);
row.createCell(1).setCellValue(1);
row.createCell(2).setCellValue(creationHelper.createRichTextString("create cell"));
row.createCell(3).setCellValue(true);
FileOutputStream fileOutputStream = new FileOutputStream("D:\\workbook.xlsx");
wb.write(fileOutputStream);
fileOutputStream.close();
}
/**
*
* @description 创建日期格式的单元格
* @author liushaocheng
* @created 2016年5月30日 下午8:03:54
* @throws Exception
*/
public void createDateCells() throws Exception {
XSSFWorkbook wb = new XSSFWorkbook();
CreationHelper creationHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("sheet one");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue(new Date());
CellStyle cellstyle = wb.createCellStyle();
cellstyle.setDataFormat(creationHelper.createDataFormat().getFormat("m/d/yy h:mm"));
cell = row.createCell(1);
cell.setCellValue(new Date());
cell.setCellStyle(cellstyle);
cell = row.createCell(2);
cell.setCellValue(Calendar.getInstance());
cell.setCellStyle(cellstyle);
FileOutputStream file = new FileOutputStream("D:\\workbook.xlsx");
wb.write(file);
file.close();
}
/**
*
* @description 创建不同格式的单元格
* @author liushaocheng
* @created 2016年5月30日 下午8:14:10
* @throws Exception
*/
@Test
public void createDifferent() throws Exception {
XSSFWorkbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("sheet two");
Row row = sheet.createRow(0);
row.createCell(0).setCellValue(1.1);
row.createCell(1).setCellValue(new Date());
row.createCell(2).setCellValue(Calendar.getInstance());
row.createCell(3).setCellValue("string");
row.createCell(4).setCellValue(true);
row.createCell(5).setCellType(Cell.CELL_TYPE_ERROR);
FileOutputStream file = new FileOutputStream("D:\\workbook.xlsx");
wb.write(file);
file.close();
}
}