java 读取 excel数据_Java读取Excel内容一

为了从Excel中读取数据,学习了使用Java如何从Excel中读取数据。

Java程序读取Excel内容,需要jar包:poi-3.8.jar,poi-ooxml.jar,poi-ooxml-schemas.jar

简单的程序如下(学习中):

package com.test.readfile;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

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;

public class ExcelReaderToWrite {

public static void main(String[] args) {

// TODO Auto-generated method stub

ExcelReaderToWrite ertw = new ExcelReaderToWrite();

ertw.writeExcel();

ertw.readExcel();

}

//在Excel中写入数据

public void writeExcel()

{

try {

FileOutputStream fos = new FileOutputStream("F:\\bbb.xls");

HSSFWorkbook wb = new HSSFWorkbook();

HSSFSheet s = wb.createSheet();

wb.setSheetName(0, "first sheet");

HSSFRow row = s.createRow(0);

HSSFCell cell = row.createCell(0, 0);

HSSFRichTextString hts = new HSSFRichTextString("row0 第一行第一列数据");//第一行第一列数据

cell.setCellValue(hts);

HSSFRow row1 = s.createRow(1);

HSSFCell cell1 = row1.createCell(0,1);

HSSFRichTextString hts1 = new HSSFRichTextString("row1 第二行第一列数据");//第二行第一列数据

cell1.setCellValue(hts1);

//HSSFCell cell2 = row.createCell((int)1, 0);

//cell2.setCellValue(222);//写入整型

wb.write(fos);

fos.flush();

fos.close();

System.out.println("excel中成功添加数据");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

//从Excel中读数据

public void readExcel()

{

try {

FileInputStream fis = new FileInputStream("F:\\bbb.xls");

HSSFWorkbook wb = new HSSFWorkbook(fis);

HSSFSheet s = wb.getSheetAt(0);

HSSFRow r = s.getRow(0);

HSSFCell c = r.getCell(0);

if(c.getCellType() == HSSFCell.CELL_TYPE_STRING)

{

System.out.println(c.getRichStringCellValue());

System.out.println("------------------------------");

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29056818/viewspace-1816364/,如需转载,请注明出处,否则将追究法律责任。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值