java利用poi 读写(导入导出)word、excel(数组越界求解决办法)


java 读写 word excel 简单的实例

需要的jar包有

poi-3.8-20120326.jar

poi-examples-3.8-20120326.jar

poi-excelant-3.8-20120326.jar

poi-ooxml-3.8-20120326.jar

poi-ooxml-schemas-3.8-20120326.jar

poi-scratchpad-3.8-20120326.jar


具体实例代码如下 :

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.hwpf.extractor.WordExtractor;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/**
 * 
 * @author Administrator
 *
 */
public class ExcelWord {

	static public void main(String[] args) throws Exception {
		// ------------在xls中写入数据-----即导出excel
		FileOutputStream fos = new FileOutputStream("e:\\text.xls");
		HSSFWorkbook wb = new HSSFWorkbook();
		//给sheet命名
		HSSFSheet s = wb.createSheet("第一个sheet");
//		wb.setSheetName(0, "first sheet");
		//第一行
		HSSFRow row = s.createRow((short)0);
		//第一行第一列
		HSSFCell cell = row.createCell((short) 0);
		HSSFRichTextString hts = new HSSFRichTextString("nihao你好啊啊");
		cell.setCellValue(hts);
		//第一行第二列
		cell = row.createCell((short) 1);
		HSSFRichTextString hts1 = new HSSFRichTextString("中国龙");
		cell.setCellValue(hts1);
		/第二行
		HSSFRow row2 = s.createRow((short)1);
		//第二行第一列
		HSSFCell cell1 = row2.createCell((short) 0);
		HSSFRichTextString hts2 = new HSSFRichTextString("第二行数据1111");
		cell1.setCellValue(hts2);
		//第二行第二列
		cell1 = row2.createCell((short) 1);
		HSSFRichTextString hts3 = new HSSFRichTextString("第二列数据ddd");
		cell1.setCellValue(hts3);
		
		//写入excel 关闭流
		wb.write(fos);
		fos.flush();
		fos.close();
		
		
		
		// ------------从xls读出数据----即数据导入
		StringBuffer sb=new StringBuffer();
		wb = new HSSFWorkbook(new FileInputStream("e:\\text.xls"));
		//获得第一张sheet
		s = wb.getSheetAt(0);
		System.out.println("行数:"+s.getLastRowNum());
		//末行+1
		for(int i=s.getFirstRowNum();i<s.getLastRowNum()+1;i++){
			HSSFRow r = s.getRow(i);
			 // 处理空行  
            if(r == null){  
                continue ;  
            }
            System.out.println("列数:"+r.getLastCellNum());
            //末列
			for(int j=r.getFirstCellNum();j<r.getLastCellNum();j++){
				cell = r.getCell((short) j);
				// 处理空列  
                if(r.getCell(j) == null){  
                    continue ;  
                }  
                String content=r.getCell(j).toString(); 
//                System.out.println("第"+i+"行"+"第"+j+"列的值为:"+content);
                sb.append(content+"||");
//				if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
//					System.out.println(cell.getRichStringCellValue());
//				}
			}
			sb.append("\n");
		}
		System.out.println(sb.toString());
//		s = wb.getSheetAt(0);
//		HSSFRow r = s.getRow(0);
		
		
		
		
//		// ------------------在doc中写入----导出word
//
//		byte[] a = new String("用到的JAR包如下(可以直接到POI官网上下载也可以在文章的附件中下载):poi-3.9-20121203.jarpoi-ooxml-3.9-20121203.jarpoi-ooxml-schemas-3.9-20121203.jarxmlbeans-2.3.0.jar可能有冲突的JAR包,如果工程lib中存在,需要删除。").getBytes();
//		ByteArrayInputStream bs = new ByteArrayInputStream(a);
//		POIFSFileSystem fs = new POIFSFileSystem();
//		// /
//		DirectoryEntry directory = fs.getRoot();
//		DocumentEntry de = directory.createDocument("WordDocument", bs);
//		// 以上两句代码不能省略,否则输出的是乱码
//		fos = new FileOutputStream("e:\\text.doc");
//		fs.writeFilesystem(fos);
//		bs.close();
//		fos.flush();
//		fos.close();
		
		
		
		// ------------从doc读出数据--word数据导入
		FileInputStream in = new FileInputStream("e:\\text.doc");
		WordExtractor extractor = new WordExtractor(in);
		
		String text = extractor.getText().toString();
		// 对DOC文件进行提取
		System.out.println(text);
		in.close();
		
		
	}
}


遇到的问题:

poi 导出数据到word后,再读取该word文档中的数据会抛出数组越界异常,直接读取正常编辑的word文件没有抛错。希望大神指点一二。




评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

prefectjava

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值