Java读取Excel中的单元格数据

目前网上能找到Web平台下的读取Excel表格中数据的两种比较好的方案:PageOffice好用开发效率高;POI免费。供大家参考,针对具体情况选择具体方案。

1. PageOffice读取excel
import com.zhuozhengsoft.pageoffice.*;
import com.zhuozhengsoft.pageoffice.excelreader.*;

//读取Excel单元格数据
Workbook workBook = new Workbook(request, response);
Sheet sheet = workBook.openSheet("Sheet1");
Table table = sheet.openTable("A1:F5");
int row = 1;
while (!table.getEOF()) {
//获取提交的数值
if (!table.getDataFields().getIsEmpty()) {
String content = "";
content += "A”+String.valueOf(row)+”:"+ table.getDataFields().get(0).getText();
content += "B”+String.valueOf(row)+”:"+ table.getDataFields().get(1).getText();
content += "C”+String.valueOf(row)+”:"+ table.getDataFields().get(2).getText();
content += "D”+String.valueOf(row)+”:"+ table.getDataFields().get(3).getText();
content += "E”+String.valueOf(row)+”:"+ table.getDataFields().get(4).getText();
content += "F”+String.valueOf(row)+”:"+ table.getDataFields().get(5.getText();
System.out.println(content);//输出一行的数据
}
//循环进入下一行
table.nextRow();
}
table.close();
workBook.close();

注意:虽然编程是JAVA代码,但是这些代码只是在服务器端接收客户端提交的excel单元格数据,PageOffice真正的读取单元格数据工作是在客户端执行的,要求客户端必须安装office软件(客户端执行的时候无需显示界面)。服务器端这些对象只接受一下数据就行,无需耗费大量资源去处理文件,也无需处理多个客户并发请求的问题,因为每个客户端都各自读取自己的数据,服务器端只接收数据保存到数据库。代码逻辑清晰易读、易于理解。

2.poi读取excel
package com.test.poi;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.HSSFCell;
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.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

public class ReadExcel {

public static void main(String[] args) throws Exception {
HSSFWorkbook wb = null;
POIFSFileSystem fs = null;
try {
fs = new POIFSFileSystem(new FileInputStream("e:\\workbook.xls"));
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}

HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(0);
HSSFCell cell = row.getCell(0);
String msg = cell.getStringCellValue();
System.out.println(msg);
}
public static void method2() throws Exception {

InputStream is = new FileInputStream("e:\\workbook.xls");
HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(is));

ExcelExtractor extractor = new ExcelExtractor(wb);
extractor.setIncludeSheetNames(false);
extractor.setFormulasNotResults(false);
extractor.setIncludeCellComments(true);

String text = extractor.getText();
System.out.println(text);
}

public static void method3() throws Exception {
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream("e:\\workbook.xls"));
HSSFSheet sheet = wb.getSheetAt(0);

for (Iterator<Row> iter = (Iterator<Row>) sheet.rowIterator(); iter.hasNext();) {
Row row = iter.next();
for (Iterator<Cell> iter2 = (Iterator<Cell>) row.cellIterator(); iter2.hasNext();) {
Cell cell = iter2.next();
String content = cell.getStringCellValue();// 除非是sring类型,否则这样迭代读取会有错误
System.out.println(content);
}
}
}
}

注意:HSSFWorkbook,XSSFWorkbook的区别:前者是解析出来excel 2007 以前版本的,后缀名为xls的,后者是解析excel 2007 版的,后缀名为xlsx。需要针对不同的excel版本编写不同的程序,判断该用哪个workbook来对其进行解析处理,而且通常需要开发人员自己把这些方法都做相应封装,使其更面向对象,工作量有点大,还有就是要求开发高质量代码,因为是在服务器上执行的,必须考虑多个客户同时请求的并发阻塞问题,和万一程序执行异常的处理问题,上例只是main方法的简单示例而已,仅供参考!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值