POI提取word,读写excel

本文展示了如何利用Apache POI库进行Word文档的内容提取及Excel表格的数据读取和写入操作。通过示例代码,介绍了如何从Word文档中获取文本内容,并演示了如何读取Excel工作表中的单元格数据以及如何更新这些数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

poi提取word:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.hwpf.extractor.WordExtractor;

public class TikaTest {

public static void main(String[] args) {
File file = new File("E:\\expo_qa.doc");
try {
FileInputStream fis = new FileInputStream(file);
WordExtractor wordExtractor = new WordExtractor(fis);
System.out.println("使用getText()方法提取的Word文件的内容:");
System.out.println("==============================");
System.out.println(wordExtractor.getText());
//System.out.println(wordExtractor.getFileSystem());

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}


poi读写excel:


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;


public class TikaExcel {

/**
* @param args
* @throws Exception
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, Exception {
// TODO Auto-generated method stub

POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("E:/develop/staff.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fs);


HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(0);
HSSFCell cell = row.getCell((short)0);
String msg = cell.getStringCellValue();

System.out.println(msg);
HSSFRow row2 = sheet.getRow(1);
System.out.println(row2.getCell(0)+" "+row2.getCell(1)+" "+row2.getCell(2)+" "+row2.getCell(3));
HSSFRow row3 = sheet.getRow(11);
System.out.println(row3.getCell(0)+" "+row3.getCell(1)+" "+row3.getCell(2)+" "+row3.getCell(3));

// POIFSFileSystem fs =new POIFSFileSystem(new FileInputStream("workbook.xls"));

//HSSFWorkbook wb = new HSSFWorkbook(fs);

// HSSFSheet sheet = wb.getSheetAt(0);

HSSFRow rowX = sheet.getRow(11);

HSSFCell cellX = rowX.getCell((short)0);

cellX.setCellValue("李宁(James)");

// Write the output to a file

FileOutputStream fileOut = new FileOutputStream("E:/develop/staff.xls");

wb.write(fileOut);

fileOut.close();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值