62,614
社区成员
发帖
与我相关
我的任务
分享
Java codecell = row.getCell((short) 6);
String a = cell.getStringCellValue();
HSSFCell cell = row.getCell(0);
// 输出单元内容,cell.getNumericCellValue()就是取所在单元的值
System.out.println("左上端单元是: " + cell.getNumericCellValue());
package com.java91.parser;
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.extractor.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
//import LuceneBook.ChineseAnalyzer;
import java.io.FileInputStream;
import java.io.File;
public class WordParserTest {
public static String excelfile = "F:\\docs\\Book1.xls";
public static String wordfile = "F:\\docs\\Site Linkmap Table of Contents.doc";
public static void main(String argv[]) throws Exception {
//getWordText(wordfile);
//getWordText(wordfile);
//getExcelText(excelfile);
}
public static void getWordText(String filename) throws Exception {
FileInputStream in = new FileInputStream(new File(filename));
HWPFDocument doc = new HWPFDocument(in);
Range range = doc.getRange();
String text = range.text();
for (int i = 0; i < range.numParagraphs(); i++) {
Paragraph p = range.getParagraph(i); // 取得每个段落
// 组合文字并添加换行
text = p.text();
text.trim();
// text = " <br> " ;
System.out.println(text);
}
}
// 获取doc内纯文本信息
public static void getWordText2(String filename) throws Exception {
FileInputStream in = new FileInputStream(new File(filename));
WordExtractor extractor = new WordExtractor(in);
String text = extractor.getText();
System.out.println(text);
}
// 获取Excel内纯文本信息
public static void getExcelText(String filename) throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filename));
HSSFSheet sheet = workbook.getSheetAt(0); // 创建对工作表的引用。
for (int i = 0; i < workbook.getNumberOfSheets(); i++) // 循环取表单对象
{
System.out.print("########## sheet:--" + i + " --########## ");
sheet = workbook.getSheetAt(i); // 查阅文档的Sheet属性
if (sheet != null) {
for (int m = 0; m < sheet.getLastRowNum(); m++) // 按行循环取行对象
{
HSSFRow row = sheet.getRow(m);
if (row == null) {
break;
}
System.out.println("");
if (row.getLastCellNum() <= 0)
break;
System.out.println("-----line:--" + m + " ---- ,col num:" + row.getLastCellNum());
for (int n = 0; n < row.getLastCellNum(); n++) // 按列循环取单元格对象
{
HSSFCell cell = row.getCell((short) n);
if (cell == null) {
break;
}
int type = cell.getCellType();
switch (type) {
case 0:
System.out.print(cell.getNumericCellValue() + " , ");
break;
case 1:
System.out.print(cell.getStringCellValue() + " , ");
break;
case 2:
break;
case 3:
System.out.print(" , ");
break;
default:
System.out.print("未知的单元类型" + type + " , ");
}
}
}
}
System.out.println();
}
}
}