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) {
}*/
}