java使用poi读写excel

使用poi操作excel 代码
package com.tws.readexcel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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.ss.usermodel.WorkbookFactory;

/**
* @author wb_wensheng.tangws
*
*/
public class ReaderExcel {

/**
* @param args
*/
public static void main(String[] args) {
Workbook workbook=null;
//WorkBook
try {

File excelFile=new File("D:\\test\\Book.xlsx");
FileInputStream fis =new FileInputStream(excelFile);
workbook=WorkbookFactory.create(fis);   //这种方式不管是2003还是2007都是可以处理的

//2003   这种方式跟具体的文件进行关联了
/*HSSFWorkbook hssfwb=new HSSFWorkbook(fis);
int sheets=hssfwb.getNumberOfSheets();
for(int s=0;s<sheets;s++){
HSSFSheet sheet=hssfwb.getSheetAt(s);
int rows=sheet.getLastRowNum();
for(int r=0;r<rows;r++){
HSSFRow hssfrow=sheet.getRow(r);
for(int i=0;i<hssfrow.getPhysicalNumberOfCells(); i++){
Cell cell=hssfrow.getCell(i);
if(HSSFCell.CELL_TYPE_NUMERIC==cell.getCellType()){
System.out.println("number: " + cell.getNumericCellValue());
}else if(HSSFCell.CELL_TYPE_STRING==cell.getCellType()){
System.out.println("String : " + cell.getStringCellValue());
}
}
}
}*/

//2007这种方式只支持
/*XSSFWorkbook xssfwb=new XSSFWorkbook(fis);
int sheets=xssfwb.getNumberOfSheets();
for(int s=0;s<sheets;s++){
XSSFSheet xssfsheet=xssfwb.getSheetAt(s);
int rows= xssfsheet.getPhysicalNumberOfRows();
for(int r=1;r<rows;r++){
XSSFRow xssfRow=xssfsheet.getRow(r);
int cells=xssfRow.getPhysicalNumberOfCells();
for(int i=0;i<cells;i++){
XSSFCell cell=xssfRow.getCell(i);
if(XSSFCell.CELL_TYPE_NUMERIC==cell.getCellType()){
System.out.println("number: " + cell.getNumericCellValue());
}else if(XSSFCell.CELL_TYPE_STRING==cell.getCellType()){
System.out.println("String: " + cell.getStringCellValue());
}
}
}
}*/

ReaderExcel re=new ReaderExcel();
//re.readExcel(workbook);

re.writerExcel();
} catch (Exception e) {
System.out.println(e);
}
}


public void readExcel(Workbook workBook){
int sheets=workBook.getNumberOfSheets();
int totalRow=0;
for(int s=0;s<sheets;s++){
Sheet sheet=workBook.getSheetAt(s);
totalRow=sheet.getLastRowNum();
sheet.getPhysicalNumberOfRows();
for(int r=0;r<totalRow;r++){
Row row=sheet.getRow(r);
int cells=row.getPhysicalNumberOfCells();  //row.getLastCellNum();是一样的
for(int i=0;i<cells;i++){
Cell cell=row.getCell(i);
String str=getCellValue(cell);
System.out.println("str: " + str);
}
//row.get
}
}
}


public void writerExcel(){
//Workbook workBook=WorkbookFactory.
HSSFWorkbook hssfwb=new HSSFWorkbook();


Sheet sheet=hssfwb.createSheet("1");

//下面这些操作一遍在循环中
Cell cell=null;
Row row=sheet.createRow(0); //第1行
cell=row.createCell(0);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("第一列");

cell=row.createCell(1);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("第二列");

row=sheet.createRow(1); //第二行
cell=row.createCell(0);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("1");

cell=row.createCell(1);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("2");


try {
File f=new File("D:\\test\\abc.xls");
if(!f.exists()){
f.createNewFile();
}
OutputStream os=new FileOutputStream(f);
hssfwb.write(os);
os.close();
} catch (Exception e) {
e.printStackTrace();
}

}

public String getCellValue(Cell cell){
String cellValue="";
if(cell != null){


switch(cell.getCellType()){
case  Cell.CELL_TYPE_STRING: //String类型的
cellValue=cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
cellValue=String.valueOf(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_BLANK:
cellValue="";
break;
case Cell.CELL_TYPE_FORMULA: //公式
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cellValue=String.valueOf(cell.getNumericCellValue());
break;
default:
break;
}
}
return cellValue;
}


public Date getCellValueDate(Cell cell){
//判断是日期
Date date= null;
if(HSSFDateUtil.isCellDateFormatted(cell)){
date=cell.getDateCellValue();
}
return date;
}

}

你可能感兴趣的:(Excel)