原文地址 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>
读取
- package jxl.zhanhj;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import jxl.Sheet;
- import jxl.Workbook;
- import jxl.read.biff.BiffException;
- public class GetExcelInfo {
- public static void main(String[] args) {
- GetExcelInfo obj = new GetExcelInfo();
- // 此处为我创建Excel路径:E:/zhanhj/studysrc/jxl下
- File file = new File("E:/zhanhj/studysrc/jxl/getExcleinfo.xls");
- obj.readExcel(file);
- }
- // 去读Excel的方法readExcel,该方法的入口参数为一个File对象
- public void readExcel(File file) {
- try {
- // 创建输入流,读取Excel
- InputStream is = new FileInputStream(file.getAbsolutePath());
- // jxl提供的Workbook类
- Workbook wb = Workbook.getWorkbook(is);
- // Excel的页签数量
- int sheet_size = wb.getNumberOfSheets();
- for (int index = 0; index < sheet_size; index++) {
- // 每个页签创建一个Sheet对象
- Sheet sheet = wb.getSheet(index);
- // sheet.getRows()返回该页的总行数
- for (int i = 0; i < sheet.getRows(); i++) {
- // sheet.getColumns()返回该页的总列数
- for (int j = 0; j < sheet.getColumns(); j++) {
- String cellinfo = sheet.getCell(j, i).getContents();
- System.out.println(cellinfo);
- }
- }
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (BiffException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
写入:
- public void readExcelWrite2TXT(File file) {
- // 创建文件输出流
- FileWriter fw = null;
- PrintWriter out = null;
- try {
- // 指定生成txt的文件路径
- String fileName = file.getName().replace(".xls", "");
- fw = new FileWriter(file.getParent() + "/" + fileName + ".txt");
- out = new PrintWriter(fw);
- // 创建输入流,读取Excel
- InputStream is = new FileInputStream(file.getAbsolutePath());
- // jxl提供的Workbook类
- Workbook wb = Workbook.getWorkbook(is);
- // Excel的页签数量
- int sheet_size = wb.getNumberOfSheets();
- for (int index = 0; index < sheet_size; index++) {
- // 每个页签创建一个Sheet对象
- Sheet sheet = wb.getSheet(index);
- // sheet.getRows()返回该页的总行数
- for (int i = 0; i < sheet.getRows(); i++) {
- // sheet.getColumns()返回该页的总列数
- for (int j = 0; j < sheet.getColumns(); j++) {
- String cellinfo = sheet.getCell(j, i).getContents();
- // 将从Excel中读取的数据写入到txt中
- out.println(cellinfo);
- }
- }
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (BiffException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- // 记得关闭流
- out.close();
- fw.close();
- // 由于此处用到了缓冲流,如果数据量过大,不进行flush操作,某些数据将依旧
- // 存在于内从中而不会写入文件,此问题一定要注意
- out.flush();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }