java读写excel文档

使用JXL读取Excel并转换为TXT

原文地址 http://blog.youkuaiyun.com/a214919447/article/details/54601237 

所需要的jar包:

<!-- excel读取 -->
<!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl -->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>

读取

  1. package jxl.zhanhj;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import jxl.Sheet;  
  8. import jxl.Workbook;  
  9. import jxl.read.biff.BiffException;  
  10. public class GetExcelInfo {  
  11.     public static void main(String[] args) {  
  12.         GetExcelInfo obj = new GetExcelInfo();  
  13.         // 此处为我创建Excel路径:E:/zhanhj/studysrc/jxl下  
  14.         File file = new File("E:/zhanhj/studysrc/jxl/getExcleinfo.xls");  
  15.         obj.readExcel(file);  
  16.     }  
  17.     // 去读Excel的方法readExcel,该方法的入口参数为一个File对象  
  18.     public void readExcel(File file) {  
  19.         try {  
  20.             // 创建输入流,读取Excel  
  21.             InputStream is = new FileInputStream(file.getAbsolutePath());  
  22.             // jxl提供的Workbook类  
  23.             Workbook wb = Workbook.getWorkbook(is);  
  24.             // Excel的页签数量  
  25.             int sheet_size = wb.getNumberOfSheets();  
  26.             for (int index = 0; index < sheet_size; index++) {  
  27.                 // 每个页签创建一个Sheet对象  
  28.                 Sheet sheet = wb.getSheet(index);  
  29.                 // sheet.getRows()返回该页的总行数  
  30.                 for (int i = 0; i < sheet.getRows(); i++) {  
  31.                     // sheet.getColumns()返回该页的总列数  
  32.                     for (int j = 0; j < sheet.getColumns(); j++) {  
  33.                         String cellinfo = sheet.getCell(j, i).getContents();  
  34.                         System.out.println(cellinfo);  
  35.                     }  
  36.                 }  
  37.             }  
  38.         } catch (FileNotFoundException e) {  
  39.             e.printStackTrace();  
  40.         } catch (BiffException e) {  
  41.             e.printStackTrace();  
  42.         } catch (IOException e) {  
  43.             e.printStackTrace();  
  44.         }  
  45.     }  


写入:

  1. public void readExcelWrite2TXT(File file) {  
  2.         // 创建文件输出流  
  3.         FileWriter fw = null;  
  4.         PrintWriter out = null;  
  5.         try {  
  6.             // 指定生成txt的文件路径  
  7.             String fileName = file.getName().replace(".xls""");  
  8.             fw = new FileWriter(file.getParent() + "/" + fileName + ".txt");  
  9.             out = new PrintWriter(fw);  
  10.             // 创建输入流,读取Excel  
  11.             InputStream is = new FileInputStream(file.getAbsolutePath());  
  12.             // jxl提供的Workbook类  
  13.             Workbook wb = Workbook.getWorkbook(is);  
  14.             // Excel的页签数量  
  15.             int sheet_size = wb.getNumberOfSheets();  
  16.             for (int index = 0; index < sheet_size; index++) {  
  17.                 // 每个页签创建一个Sheet对象  
  18.                 Sheet sheet = wb.getSheet(index);  
  19.                 // sheet.getRows()返回该页的总行数  
  20.                 for (int i = 0; i < sheet.getRows(); i++) {  
  21.                     // sheet.getColumns()返回该页的总列数  
  22.                     for (int j = 0; j < sheet.getColumns(); j++) {  
  23.                         String cellinfo = sheet.getCell(j, i).getContents();  
  24.                         // 将从Excel中读取的数据写入到txt中  
  25.                         out.println(cellinfo);  
  26.                     }  
  27.                 }  
  28.             }  
  29.         } catch (FileNotFoundException e) {  
  30.             e.printStackTrace();  
  31.         } catch (BiffException e) {  
  32.             e.printStackTrace();  
  33.         } catch (IOException e) {  
  34.             e.printStackTrace();  
  35.         } finally {  
  36.             try {  
  37.                 // 记得关闭流  
  38.                 out.close();  
  39.                 fw.close();  
  40.                 // 由于此处用到了缓冲流,如果数据量过大,不进行flush操作,某些数据将依旧  
  41.                 // 存在于内从中而不会写入文件,此问题一定要注意  
  42.                 out.flush();  
  43.             } catch (IOException e) {  
  44.                 e.printStackTrace();  
  45.             }  
  46.         }  
  47.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值