采用POI操作excel
API:http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFCell.html
poi包:http://pan.baidu.com/s/1hmIQU
一.讀取excel内容
1.excel内容的值如果不是string,則用getNumericCellValue(),得到double類型,再做相應轉換,如果為string,則用getStringCellValue()
public static String getExcel(int index,int rowNum,int colNum) {
//File file = new File("D:/BaiduYunDownload/excel/testdata.xls");
File file = new File("./POIexcel/testdata.xls");
String cellValue = null;
int rowN = rowNum-1;//将excel的行數-1
Row row = null;
Cell cell= null;
HSSFCell hf = null;
// Cell cell_b = null;
try {
FileInputStream in = new FileInputStream(file);
HSSFWorkbook wb = new HSSFWorkbook(in);
HSSFSheet sheet = wb.getSheetAt(index);//sheet頁,index從0開始
//從哪行讀取
// int firstRowNum = sheet.getFirstRowNum()+1;
// int lastRowNum = sheet.getLastRowNum();
row = sheet.getRow(rowN); //取得第幾行
cell = row.getCell(colNum); //取得行的第3列,從0開始
if(cell!=null){
//((Object) hf).setEncoding(HSSFCell.ENCODING_UTF_16);
//判斷excel内容的數值類型
switch(cell.getCellType()) {
case Cell.CELL_TYPE_STRING://String
cellValue = cell.getStringCellValue().trim();
break;
case Cell.CELL_TYPE_NUMERIC://number
if(HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
if (date != null) {//date
cellValue = new SimpleDateFormat("yyyy-MM-dd").format(date);
} else {
cellValue = "";
}
}else {
cellValue = new DecimalFormat("###.###").format(cell.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_FORMULA:
// 導入時如果為公式生成的資料則無值
if (!cell.getStringCellValue().equals("")) {
cellValue = cell.getStringCellValue();
} else {
cellValue = cell.getNumericCellValue() + "";
}
break;
case HSSFCell.CELL_TYPE_BLANK:
break;
case HSSFCell.CELL_TYPE_ERROR:
cellValue = "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
cellValue = (cell.getBooleanCellValue() == true ? "Y": "N");
break;
default:
cellValue = "";
/*
if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC) {
double i = cell.getNumericCellValue();
cellValue = String.valueOf(i);
}else{
cellValue = cell.getStringCellValue().trim();
if(cellValue.equals("")){
System.out.println(rowNum+"行的值為空");
}
}
*/
}
}
}catch (Exception e) {
e.printStackTrace();
}
return cellValue;
}
double轉換為int:int i_yhfw= (int) Double.parseDouble(Demo.getExcel(index, 13));
二.設定excel内容
public static void setExcel(String path, int sheet, int row, int col,String value) {
try {
File file = new File(path);
FileInputStream in = new FileInputStream(file);
HSSFWorkbook hw = new HSSFWorkbook(in);
HSSFSheet hsheet= hw.getSheetAt(sheet);//目标sheet的索引
HSSFRow hrow = hsheet.getRow(row-1);//目标行的索引
HSSFCell cell = hrow.createCell(col-1);//目标列的索引
HSSFRichTextString val = new HSSFRichTextString(value);
cell.setCellValue(val);
OutputStream out = new FileOutputStream(file);//擷取檔案輸出流
hw.write(out);//将内容寫到excel
out.close();
in.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
以上的方法的是HSFF隻能操作03的excel,通過官方api介紹,使用XSFF可以操作07的excel,故優化代碼如下,自動識别傳入的excel是03的還是07的。
package com.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
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 org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* @author QiaoJiafei
* @version 建立時間:2015年11月12日 上午10:35:03
* 類說明
*/
public class TestExcel037 {
public static void main(String args[]) {
System.out.println(getExcel("D:/03excel.xls",1,2,2));
System.out.println(getExcel("D:/07excel.xlsx",1,2,2));
}
public static String getExcel(String path,int index,int rowNum,int colNum) {
File file = new File(path);
String cellValue = "";
Workbook wb = null;
Sheet sheet = null;
Row row = null;
Cell cell = null;
try {
FileInputStream in = new FileInputStream(file);
if(path.endsWith(".xls")) {
wb = new HSSFWorkbook(in);
sheet = wb.getSheetAt(index-1);
}else if (path.endsWith(".xlsx")) {
wb = new XSSFWorkbook(in);
sheet = wb.getSheetAt(index-1);
}
row = sheet.getRow(rowNum);
cell = row.getCell(colNum);
if(cell!=null){
switch(cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
cellValue = cell.getStringCellValue().trim();
break;
case Cell.CELL_TYPE_NUMERIC:
if(HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
if (date != null) {
cellValue = new SimpleDateFormat("yyyy-MM-dd").format(date);
} else {
cellValue = "";
}
}else {
cellValue = new DecimalFormat("###.###").format(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_FORMULA:
if (!cell.getStringCellValue().equals("")) {
cellValue = cell.getStringCellValue();
} else {
cellValue = cell.getNumericCellValue() + "";
}
break;
case Cell.CELL_TYPE_BLANK:
break;
case Cell.CELL_TYPE_ERROR:
cellValue = "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
cellValue = (cell.getBooleanCellValue() == true ? "Y": "N");
break;
default:
cellValue = "";
}
}
in.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return cellValue;
}
}
所用jar包:

關于DecimalFormat的用法,參考http://www.cnblogs.com/lsun/archive/2011/06/22/2087116.html
DecimalFormat 是 NumberFormat 的一個具體子類,用于格式化十進制數字。
DecimalFormat 包含一個模式 和一組符号
符号含義:
0 一個數字
# 一個數字,不包括 0
. 小數的分隔符的占位符
, 分組分隔符的占位符
; 分隔格式。
- 預設負數字首。
% 乘以 100 和作為百分比顯示
? 乘以 1000 和作為千進制貨币符顯示;用貨币符号代替;如果雙寫,用
國際貨币符号代替。如果出現在一個模式中,用貨币十進制分隔符代
替十進制分隔符。
X 字首或字尾中使用的任何其它字元,用來引用字首或字尾中的特殊字元。
例子:
DecimalFormat df1 = new DecimalFormat("0.0");
DecimalFormat df2 = new DecimalFormat("#.#");
DecimalFormat df3 = new DecimalFormat("000.000");
DecimalFormat df4 = new DecimalFormat("###.###");
System.out.println(df1.format(12.34));
System.out.println(df2.format(12.34));
System.out.println(df3.format(12.34));
System.out.println(df4.format(12.34));
結果:
12.3
12.3
012.340
12.34