使用POI处理EXCEL文件。逐行提取 其中的文字。写入文本文件。可是代码有问题。请赐教。

blueink_200451 2008-12-21 03:38:31
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();
}
}
}


有问题就写在这里。我一直在。这个程序是在一本名为开发自己的搜索引擎的书第7章的代码。与程序相关的包都添加。只是代码有点问题。不知道该怎么修正。请赐教。
...全文
231 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ooily 2008-12-24
  • 打赏
  • 举报
回复
不懂,帮顶
blueink_200451 2008-12-24
  • 打赏
  • 举报
回复
请问一下。现在使用java,开源方面的图像浏览程序是什么使用的最多?
yukang_ky 2008-12-24
  • 打赏
  • 举报
回复
//cell = rowline.getcell((short)i); //getcell 不对。The method getcell(short) is underfined for the type HSSFRow
这个有错,应该该为:cell = rowline.getCell((short)i);

Date date = cell.getDateCellValue(); // 这里有一个错Date:Date cannot be resolved to a type
这里要导入包:import java.util.Date;
shoat112 2008-12-21
  • 打赏
  • 举报
回复
getCell Cell要大写

下面的错误应该跟这个有关
blueink_200451 2008-12-21
  • 打赏
  • 举报
回复
我是楼主:

开发环境是Eclipse.主要需要一个 poi-2.5.1-final-20040804.jar 包。

源程序就是上面。不知道怎么改。请多多赐教。

58,441

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧