Poi创建Excel(入门级)

      通过Poi操作Excel十分方便,将一个Excel抽象为一个Workbook,一个表单页抽象为Sheet,表单中的一行抽象为Row,一行中的一个单元格可以抽象为Cell。HSSF对应的是97-03格式(.xls),XSSF对应的是07格式的(.xlsx)。

      Workbook的获取有以下几种方式:

      1.可以通过WorkbookFactory,工厂方法

         Workbook wb=WorkbookFactory.create(new FileInputStream(file));//可以读取xls格式或xlsx格式。

      2.直接通过HSSFWorkbook或XSSFWorkbook的构造方法

         Workbook wb=new HSSFWorkbook();//生成一个空的Excel文件

         Workbook wb=new HSSFWorkbook(new FileInputStream(file));//读取一个已经存在的Excel文件

      Sheet可由Workbook创建

         Sheet s=wb.createSheet(sheet);//创建一个名为sheet的表单

      Row由Sheet创建

         Row r=s.createRow(row);//新创建一行,行号为row+1

      Cell有Row创建

         Cell c=r.createCell(col);//创建一个单元格,列号为col+1

      最后就可以通过c.setCellValue(value)向单元格填充内容即可

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package imageprocessor.filehandler; import java.awt.Point; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; /** * * @author Administrator */ public class FileWriter { private String path;//文件的路径 private Workbook workbook;//工作集 private int circleIndex=1;//光斑数据的计数 private int iValueIndex=1;//阈值的计数 private int radiusIndex=1;//形心半径计数 private int diameterIndex=1;//光斑直径像素值计数 private FileInputStream fis; public FileWriter(String path){ this.path=path+"//数据文件.xls"; try { this.fis=new FileInputStream(new File(this.path)); workbook = new HSSFWorkbook(fis); } catch (Exception ex) { workbook = new HSSFWorkbook(); } } /** * 生成阈值sheet */ public void createIValueSheet(){ int sheetIndex=workbook.getSheetIndex("阈值"); if(sheetIndex>=0) workbook.removeSheetAt(sheetIndex); Sheet s=workbook.createSheet("阈值"); Row r=s.createRow(0); Cell c=r.createCell(0); c.setCellValue("图像文件名"); c=r.createCell(1); c.setCellValue("阈值"); } /** * 生成光斑半径,形心的sheet */ public void createRadiusSheet(){ int sheetIndex=workbook.getSheetIndex("形心和半径"); if(sheetIndex>=0) workbook.removeSheetAt(sheetIndex); Sheet s=workbook.createSheet("形心和半径"); Row r=s.createRow(0); Cell c=r.createCell(0); c.setCellValue("图像文件名"); c=r.createCell(1); c.setCellValue("光斑形心坐标"); c=r.createCell(2); c.setCellValue("光斑半径"); } /** * 生成光斑直径上像素值sheet */ public void createDiameterValuesSheet(){ int sheetIndex=workbook.getSheetIndex("光斑直径上的像素"); if(sheetIndex>=0) workbook.removeSheetAt(sheetIndex); Sheet s=workbook.createSheet("光斑直径上的像素"); Row r=s.createRow(0); Cell c=r.createCell(0); c.setCellValue("图像文件名"); c=r.createCell(1); c.setCellValue("光斑直径上的像素值"); } public void addDiameterValues(String file,Object[] vals){ Sheet s=workbook.getSheet("光斑直径上的像素"); Row r=s.createRow(this.diameterIndex++); Cell c=r.createCell(0); c.setCellValue(file); int i=1; for(Object value:vals){ c=r.createCell(i++); c.setCellValue(value.toString()); } } public void addRadius(String file, Point center,double raduis){ Sheet s=workbook.getSheet("形心和半径"); Row r=s.createRow(this.radiusIndex++); Cell c=r.createCell(0); c.setCellValue(file); c=r.createCell(1); c.setCellValue("("+center.x+","+center.y+")"); c=r.createCell(2); c.setCellValue(raduis); } public void addIValue(String file,int value){ Sheet s=workbook.getSheet("阈值"); Row r=s.createRow(this.iValueIndex++); Cell c=r.createCell(0); c.setCellValue(file); c=r.createCell(1); c.setCellValue(value); } /** * 生成光斑像素数据文件sheet */ public void createCircleSheet(){ int sheetIndex=workbook.getSheetIndex("光斑像素数据"); if(sheetIndex>=0) workbook.removeSheetAt(sheetIndex); Sheet s=workbook.createSheet("光斑像素数据"); Row r=s.createRow(0); Cell c=r.createCell(0); c.setCellValue("图像文件名"); c=r.createCell(1); c.setCellValue("光斑像素数"); c=r.createCell(2); c.setCellValue("中心光斑像素数"); String[] strs="0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80-89,90-99,100-109,110-119,120-129,130-139,140-149,150-159,160-169,170-179,180-189,190-199".split(","); int i=3; for(String st:strs){ c=r.createCell(i++); c.setCellValue(st); } } public static void main(String[] args) { FileWriter writer=new FileWriter("d://ff.xls"); writer.createCircleSheet(); } /** * 添加一个图像的光斑像素数据 * @param file * @param cirPix * @param cenPix * @param grays */ public void addCircleData(String file,int cirPix,int cenPix,int[] grays){ Sheet s=workbook.getSheet("光斑像素数据"); Row r=s.createRow(this.circleIndex++); Cell c=r.createCell(0); c.setCellValue(file); c=r.createCell(1); c.setCellValue(cirPix); c=r.createCell(2); c.setCellValue(cenPix); int i=3; for(int st:grays){ c=r.createCell(i++); c.setCellValue(st); } } /** * 将内容写入Excel中,并关闭该文件 */ public void writeToFile(){ try { FileOutputStream fos=new FileOutputStream(new File(path)); this.workbook.write(fos); fos.close(); if(fis!=null) fis.close(); } catch (Exception ex) { Logger.getLogger(FileWriter.class.getName()).log(Level.SEVERE, null, ex); } } }  

你可能感兴趣的:(c,exception,String,Excel,File,templates)