POI读取和生成EXCEL

package test.excel;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/**
 * @ClassName:      OperateExcel.java
 * @PackageName:    test
 * @ProjectName:    healthApp
 * @Copyright:      Copyright(c)Kolter 2012 
 * @DateTime:       Nov 28, 2012 2:18:46 PM 
 * @author          Kolter
 * @version         V1.0 
 * @Readme:
 */
public class OperateExcel {
 
 public static void main(String[] args) {
  new OperateExcel().ReadExelByPOI("C:\\Documents and Settings\\Administrator\\桌面\\地址是否填写.xls");
  new OperateExcel().WriteExcelByPOI("C:\\Documents and Settings\\Administrator\\桌面\\test.xls");
 }
 
 /**
  * 使用POI组件读取Excel文档<br>
  * POI读取Excel步骤:<br>
  * 第一步:加载Excel文件<br>
  * 第二步:指定要读取的sheet<br>
  * 第三步:指定要读取的Row<br>
  * 第四步:获得要读取的Cell<br>
  * @param realPath 读取文件路径
  */
 public void ReadExelByPOI(String realPath) {
  POIFSFileSystem fs = null;
  HSSFWorkbook workbook = null;
  HSSFSheet sheet = null;
  HSSFRow row = null;
  HSSFCell cell = null;
  try {
   fs = new POIFSFileSystem(new FileInputStream(realPath));
   // 创建对Excel工作簿文件的引用
   workbook = new HSSFWorkbook(fs);
   // 创建对工作表的引用。
   // sheet = workbook.getSheet("Sheet1");
      // 本例是按名引用(让我们假定那张表有着缺省名"Sheet1"),
   // 也可用getSheetAt(int index)按索引引用.
   // 在Excel文档中,第一张工作表的缺省索引是0,
   // 其语句为:sheet = workbook.getSheetAt(0);
   sheet = workbook.getSheet("Sheet1");
   // 读取第一行
   row = sheet.getRow(0);
   // 读取第一行第一列
   cell = row.getCell((short)0);
   // 输出数据
   System.out.println(cell.getStringCellValue() + "\t");
   // 遍历数据并输出
   for(int i=0; i < sheet.getLastRowNum(); i++) {
    row = sheet.getRow(i);
    for(int j=0; j < row.getLastCellNum(); j++) {
     cell = row.getCell((short)j);
     System.out.print(cell.getStringCellValue() + "\t");
    }
    System.out.println();
   }
  } catch (FileNotFoundException e) {
   // 提示客户文件未找到
   e.printStackTrace();
  } catch (IOException e) {
   // 提示IO错误
   e.printStackTrace();
  }
 }
 
 /**
  * 使用POI组件保存Excel文档<br>
  * POI保存Excel步骤:<br>
  * 第一步:创建Excel文件<br>
  * 第二步:创建sheet<br>
  * 第三步:创建Row<br>
  * 第四步:创建Cell<br>
  * 第五步:设置格式<br>
  * @param realPath 保存文件路径
  */
 public void WriteExcelByPOI(String realPath) {
  HSSFWorkbook workbook = null;
  HSSFSheet sheet = null;
  HSSFRow row = null;
  HSSFCell cell = null;
  FileOutputStream out = null;
  try {
   // 创建新的Excel工作簿
   workbook = new HSSFWorkbook();
   // 在Excel工作簿中建一工作表,其名为缺省值
   // 如要新建一名为"效益指标"的工作表,其语句为:
   // sheet = workbook.createSheet("效益指标");
   sheet = workbook.createSheet();
   // 在索引0的位置创建行(最顶端的行)
   row = sheet.createRow((short)0);
   //在索引0的位置创建单元格(左上端)
   cell = row.createCell((short)0);
   // 1、创建字体,设置其为红色、粗体:
   HSSFFont font = workbook.createFont();
   font.setColor(HSSFFont.COLOR_RED);
   font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
   // 2、创建格式
   HSSFCellStyle cellStyle = workbook.createCellStyle();
   // 设置字体
   cellStyle.setFont(font);
   // 设置颜色
   cellStyle.setTopBorderColor(HSSFColor.OLIVE_GREEN.index);
   cellStyle.setFillBackgroundColor(HSSFColor.VIOLET.index);
   cellStyle.setBottomBorderColor(HSSFColor.DARK_BLUE.index);
   // 3、应用格式 
   cell.setCellStyle(cellStyle);
   // 定义单元格为字符串类型
   cell.setCellType(HSSFCell.CELL_TYPE_STRING);
   // 设置字符编码
   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
   // 在单元格中输入一些内容
   cell.setCellValue("增加值");
   // 新建一输出文件流
   out = new FileOutputStream(realPath); 
   // 把相应的Excel 工作簿存盘
   workbook.write(out);
   out.flush();
   // 操作结束,关闭文件
   out.close();
   System.out.println("文件生成...");
  } catch(FileNotFoundException e) {
   // 提示客户文件未找到
   e.printStackTrace();
  } catch (IOException e) {
   // 提示IO错误
   e.printStackTrace();
  }
 }
 
 /**
  * 使用jXLS组件读取Excel文档<br>
  * @param realPath 读取文件路径
  */
 /*public void ReadExelByJXLS(String realPath) {
  
 }*/
 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值