58,441
社区成员
发帖
与我相关
我的任务
分享package poi;
import java.io.*;
import java.io.BufferedReader;
// a
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;
//import org.apache.lucene.analysis.*;
import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.pdfbox.pdmodel.PDDocument;
//import org.pdfbox.*;
import org.pdfbox.util.PDFTextStripper;
import org.pdfbox.searchengine.lucene.LucenePDFDocument;
import org.apache.lucene.search.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
//import org.apache.poi.hwpf.usermodel.TableRow;
import org.apache.poi.hssf.usermodel.*;
// b
public class ExcelReader {
private BufferedReader reader =null;
private String filetype;
private InputStream is = null;
private int currSheet;
private int currPosition;
private int numOfSheets;
HSSFWorkbook workbook = null;
private static String EXCEL_LINE_DELIMITER=" ";
private static int MAX_EXCEL_COLUMNS = 64;
public ExcelReader(String inputfile) throws IOException,Exception{
if(inputfile ==null || inputfile.trim().equals("")){
throw new IOException("no input file specified");
}
this.filetype = inputfile.substring(inputfile.lastIndexOf(".")+1);
currPosition = 0;
currSheet= 0;
is = new FileInputStream(inputfile);
if (filetype.equalsIgnoreCase("txt")){
reader = new BufferedReader(new InputStreamReader(is));
//
}
else if (filetype.equalsIgnoreCase("xls")){
workbook = new HSSFWorkbook(is);
numOfSheets = workbook.getNumberOfSheets();
}
else{
throw new Exception("File Tpye Not Supported");
}
}
public String readLine() throws IOException {
if(filetype.equalsIgnoreCase("txt")){
String str = reader.readLine();
while (str.trim().equals("")){
str=reader.readLine();
}
return str;
}
else if (filetype.equalsIgnoreCase("xls")){
HSSFSheet sheet = workbook.getSheetAt(currSheet);
if(currPosition > sheet.getLastRowNum()){
currPosition=0;
while(currSheet !=numOfSheets -1 ){
sheet =workbook.getSheetAt(currSheet + 1);
if (currPosition == sheet.getLastRowNum()){
currSheet++;
continue;
}
else{
int row =currPosition;
currPosition++;
return getLine(sheet,row);
}
}
return null;
}
int row =currPosition;
currPosition++;
return getLine(sheet,row);
}
//
return null;
}
private String getLine(HSSFSheet sheet,int row){
HSSFRow rowline = sheet.getRow(row);
StringBuffer buffer = new StringBuffer();
int filledColumns =rowline.getLastCellNum();
HSSFCell cell=null;
for(int i=0;i<filledColumns;i++){
cell = rowline.getcell((short)i); //getcell 不对。The method getcell(short) is underfined for the type HSSFRow
String cellvalue=null;
if(cell !=null){
switch(cell.getCellType()){
case HSSFCell.CELL_TYPE_NUMERIC:{
if(HSSFDateUtil.isCellDateFormatted(cell)){
Date date = cell.getDateCellValue(); // 这里有一个错Date:Date cannot be resolved to a type
cellvalue = cell.getDateCellValue().toLocaleString();
}
else{
Integer num = new Integer((int)cell.getNumericCellValue());
cellvalue = String.valueOf(num);
}
break;
}
case HSSFCell.CELL_TYPE_STRING:
cellvalue = cell.getStringCellValue().replaceAll("'","''");
break;
default:
cellvalue=" ";
}
} else{
cellvalue="";
}
buffer.append(cellvalue).append(EXCEL_LINE_DELIMITER);
}
return buffer.toString();
}
public void close(){
if (is != null){
try{
is.close();
}
catch(IOException e){
is=null;
}
}
if (reader != null){
try{
reader.close();
}
catch (IOException e){
reader=null;
}
}
}
public static void main(String[] args){
try{
ExcelReader er=new ExcelReader("C:\\xp.xls");
String line=er.readLine();
while(line != null){
System.out.println(line);
line=er.readLine();
}
er.close();
} catch(Exception e){
e.printStackTrace();
}
}
}