码迷,mamicode.com
首页 > 其他好文 > 详细

POI操作excel

时间:2014-10-23 16:08:25      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   java   for   sp   

1.导出一个excel文件,写入内容:

package poi;

import org.apache.poi.hssf.usermodel.*;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 */
public class POICreateTest {
    public static void main(String args[]){
        HSSFWorkbook wb=new HSSFWorkbook();
        HSSFSheet sheet=wb.createSheet("new sheet");
        HSSFRow row=sheet.createRow((short)0);
        HSSFCell cell=row.createCell((short)0);
        cell.setCellValue(1);
        row.createCell((short)1).setCellValue(1.2);
        row.createCell((short) 2).setCellValue("test");
        row.createCell((short) 3).setCellValue(true);

        HSSFCellStyle cellStyle=wb.createCellStyle();
        cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));

        HSSFCell dCell=row.createCell((short) 4);
        dCell.setCellValue(new Date());
        dCell.setCellStyle(cellStyle);

        HSSFCell csCell=row.createCell((short) 5);
        csCell.setCellType(HSSFCell.ENCODING_UTF_16);
        csCell.setCellValue("你好啊——hello");
        row.createCell((short) 6).setCellType(HSSFCell.CELL_TYPE_ERROR);

        try {
            FileOutputStream fileOut=new FileOutputStream("D:\\workbook.xls");
            try {
                wb.write(fileOut);
                fileOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

2.导入一个excel,读取其中的内容:

package poi;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import java.io.FileInputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 */
public class POIReadTest {
    public static String file="D:\\workbook.xls";
    public static void main(String args[]){
        try {
            HSSFWorkbook workbook=new HSSFWorkbook(new FileInputStream(file));
            HSSFSheet sheet=workbook.getSheetAt(0);
            //遍历行
            for (Row row:sheet){
                for(Cell cell:row){
                    System.out.print(formatPOI((HSSFCell) cell)+"   ");
                }
                System.out.println("");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String formatPOI(HSSFCell cell){
        switch (cell.getCellType()){
            case HSSFCell.CELL_TYPE_BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case HSSFCell.CELL_TYPE_ERROR:
                return "this data is error";
            case HSSFCell.CELL_TYPE_NUMERIC:
                if (HSSFDateUtil.isCellDateFormatted(cell)){
                    Date date=cell.getDateCellValue();
                    DateFormat dateFormat=new SimpleDateFormat("m/d/yy h:mm");
                    return dateFormat.format(date).toString();
                }else {
                    return String.valueOf(cell.getNumericCellValue());
                }
            case HSSFCell.CELL_TYPE_STRING:
                return cell.getStringCellValue();
            default:
                return "bad errors!!!";
        }
    }

}

 

POI操作excel

标签:style   blog   color   io   os   ar   java   for   sp   

原文地址:http://www.cnblogs.com/juepei/p/4045769.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!