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

 

具体实例代码如下 :

 

[java]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.io.ByteArrayInputStream;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileOutputStream;  
  4.   
  5. import org.apache.poi.hssf.usermodel.HSSFCell;  
  6. import org.apache.poi.hssf.usermodel.HSSFRichTextString;  
  7. import org.apache.poi.hssf.usermodel.HSSFRow;  
  8. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  10. import org.apache.poi.hwpf.extractor.WordExtractor;  
  11. import org.apache.poi.poifs.filesystem.DirectoryEntry;  
  12. import org.apache.poi.poifs.filesystem.DocumentEntry;  
  13. import org.apache.poi.poifs.filesystem.POIFSFileSystem;  
  14.   
  15. /** 
  16.  *  
  17.  * @author Administrator 
  18.  * 
  19.  */  
  20. public class ExcelWord {  
  21.   
  22.     static public void main(String[] args) throws Exception {  
  23.         // ------------在xls中写入数据-----即导出excel  
  24.         FileOutputStream fos = new FileOutputStream("e:\\text.xls");  
  25.         HSSFWorkbook wb = new HSSFWorkbook();  
  26.         //给sheet命名  
  27.         HSSFSheet s = wb.createSheet("第一个sheet");  
  28. //      wb.setSheetName(0, "first sheet");  
  29.         //第一行  
  30.         HSSFRow row = s.createRow((short)0);  
  31.         //第一行第一列  
  32.         HSSFCell cell = row.createCell((short0);  
  33.         HSSFRichTextString hts = new HSSFRichTextString("nihao你好啊啊");  
  34.         cell.setCellValue(hts);  
  35.         //第一行第二列  
  36.         cell = row.createCell((short1);  
  37.         HSSFRichTextString hts1 = new HSSFRichTextString("中国龙");  
  38.         cell.setCellValue(hts1);  
  39.         /第二行  
  40.         HSSFRow row2 = s.createRow((short)1);  
  41.         //第二行第一列  
  42.         HSSFCell cell1 = row2.createCell((short0);  
  43.         HSSFRichTextString hts2 = new HSSFRichTextString("第二行数据1111");  
  44.         cell1.setCellValue(hts2);  
  45.         //第二行第二列  
  46.         cell1 = row2.createCell((short1);  
  47.         HSSFRichTextString hts3 = new HSSFRichTextString("第二列数据ddd");  
  48.         cell1.setCellValue(hts3);  
  49.           
  50.         //写入excel 关闭流  
  51.         wb.write(fos);  
  52.         fos.flush();  
  53.         fos.close();  
  54.           
  55.           
  56.           
  57.         // ------------从xls读出数据----即数据导入  
  58.         StringBuffer sb=new StringBuffer();  
  59.         wb = new HSSFWorkbook(new FileInputStream("e:\\text.xls"));  
  60.         //获得第一张sheet  
  61.         s = wb.getSheetAt(0);  
  62.         System.out.println("行数:"+s.getLastRowNum());  
  63.         //末行+1  
  64.         for(int i=s.getFirstRowNum();i<s.getLastRowNum()+1;i++){  
  65.             HSSFRow r = s.getRow(i);  
  66.              // 处理空行    
  67.             if(r == null){    
  68.                 continue ;    
  69.             }  
  70.             System.out.println("列数:"+r.getLastCellNum());  
  71.             //末列  
  72.             for(int j=r.getFirstCellNum();j<r.getLastCellNum();j++){  
  73.                 cell = r.getCell((short) j);  
  74.                 // 处理空列    
  75.                 if(r.getCell(j) == null){    
  76.                     continue ;    
  77.                 }    
  78.                 String content=r.getCell(j).toString();   
  79. //                System.out.println("第"+i+"行"+"第"+j+"列的值为:"+content);  
  80.                 sb.append(content+"||");  
  81. //              if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {  
  82. //                  System.out.println(cell.getRichStringCellValue());  
  83. //              }  
  84.             }  
  85.             sb.append("\n");  
  86.         }  
  87.         System.out.println(sb.toString());  
  88. //      s = wb.getSheetAt(0);  
  89. //      HSSFRow r = s.getRow(0);  
  90.           
  91.           
  92.           
  93.           
  94. //      // ------------------在doc中写入----导出word  
  95. //  
  96. //      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();  
  97. //      ByteArrayInputStream bs = new ByteArrayInputStream(a);  
  98. //      POIFSFileSystem fs = new POIFSFileSystem();  
  99. //      // /  
  100. //      DirectoryEntry directory = fs.getRoot();  
  101. //      DocumentEntry de = directory.createDocument("WordDocument", bs);  
  102. //      // 以上两句代码不能省略,否则输出的是乱码  
  103. //      fos = new FileOutputStream("e:\\text.doc");  
  104. //      fs.writeFilesystem(fos);  
  105. //      bs.close();  
  106. //      fos.flush();  
  107. //      fos.close();  
  108.           
  109.           
  110.           
  111.         // ------------从doc读出数据--word数据导入  
  112.         FileInputStream in = new FileInputStream("e:\\text.doc");  
  113.         WordExtractor extractor = new WordExtractor(in);  
  114.           
  115.         String text = extractor.getText().toString();  
  116.         // 对DOC文件进行提取  
  117.         System.out.println(text);  
  118.         in.close();  
  119.           
  120.           
  121.     }  
  122. }  



 

遇到的问题:

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

转载于:https://www.cnblogs.com/jiangyea/p/3512401.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值