poi对excel操作实例

来源:互联网 发布:海康花生壳域名注册 编辑:程序博客网 时间:2024/06/05 07:28
在项目开发中少不了导入和导出,导入和导出用的最多的就是apache 的poi。今天没事就写了一些poi对excel简单操作。来和大家分享一下。
package com.laughing.poi;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.util.Date;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.poifs.filesystem.POIFSFileSystem;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.DateUtil;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;public class creatExcel {private static  creatExcel create=new creatExcel(); public static void main(String[] args) throws Exception  { //create.createWordBook(); create.readExcel();} public void createWordBook()throws Exception{ //创建新工作薄 Workbook work=new HSSFWorkbook(); FileOutputStream file=new FileOutputStream("work.xls"); work.write(file); file.close(); }  public void createWordSheet()throws Exception{ //创建sheet页 Workbook work=new HSSFWorkbook(); work.createSheet("this is first sheet"); work.createSheet("this is second sheet"); FileOutputStream fileOut=new FileOutputStream("work.xls"); work.write(fileOut); fileOut.close(); } //创建单元格 public void createRow()throws Exception{ Workbook work=new HSSFWorkbook(); CreationHelper creationHelper= work.getCreationHelper(); Sheet sheet= work.createSheet("this is first sheet"); //创建一列 Row row=sheet.createRow((short)0); //创建单元格 Cell cell=row.createCell(0); cell.setCellValue(0); row.createCell(1).setCellValue(creationHelper.createRichTextString("2012-2-24")); FileOutputStream file=new FileOutputStream("work.xls"); work.write(file); file.close();  } //创建时间单元格public void createDateCell()throws Exception{Workbook work=new HSSFWorkbook();CreationHelper helper=work.getCreationHelper();Sheet sheet=work.createSheet("this is data sheet");Row row=sheet.createRow(0);Cell cell=row.createCell(0);//创建单元格样式CellStyle style=work.createCellStyle();//创建时间格式style.setDataFormat(helper.createDataFormat().getFormat("m/d/yy h:mm"));cell.setCellValue(new Date());cell.setCellStyle(style);FileOutputStream file=new FileOutputStream("work.xls");work.write(file);file.close();}//处理不同的单元格public void createEveryCell() throws Exception{Workbook work=new HSSFWorkbook();CreationHelper helper=work.getCreationHelper();CellStyle style=work.createCellStyle();//创建时间格式style.setDataFormat(helper.createDataFormat().getFormat("m/d/yy h:mm"));Sheet sheet=work.createSheet("this is ervery create cell");Row row=sheet.createRow(2);row.createCell(0).setCellValue(1.1);row.createCell(2).setCellValue(true);row.createCell(4).setCellValue(new Date());Cell cell=row.createCell(5);cell.setCellValue(new Date());cell.setCellStyle(style);row.createCell(6).setCellValue("hello");row.createCell(8).setCellType(HSSFCell.CELL_TYPE_BLANK);FileOutputStream file=new FileOutputStream("work.xls");work.write(file);file.close();}//读取excel文档public void readExcel()throws Exception{FileInputStream in=new FileInputStream("work.xls"); Workbook work=new HSSFWorkbook(new POIFSFileSystem(in));Sheet sheet=work.getSheetAt(0);for(Row row:sheet){for(Cell cell:row){//System.out.println(cell.getStringCellValue());//System.out.println(cell.getCellType());switch (cell.getCellType()) {case Cell.CELL_TYPE_NUMERIC:if(DateUtil.isCellInternalDateFormatted(cell))System.out.println(cell.getDateCellValue());elseSystem.out.println(cell.getNumericCellValue());break;case Cell.CELL_TYPE_STRING:System.out.println(cell.getStringCellValue());break;case Cell.CELL_TYPE_BOOLEAN:System.out.println(cell.getBooleanCellValue());break;default:System.out.println(cell.getCellType());break;}}}} }

引用包的下载地址是

http://download.csdn.net/detail/gelk646944325/4479762


原创粉丝点击