java怎么读取很大的excel(20w条数据),怎么通过字节流获取到单元格的内容
1个回答
2016-04-23 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
使用java poi
package webservice;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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;
public class ExcelController {
@SuppressWarnings("deprecation")
public void excel() throws FileNotFoundException, IOException{
String filename = "d:\\excel.xls";
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filename));
//按名引用excel工作表
// HSSFSheet sheet = workbook.getSheet("JSP");
//也可以用以下方式来获取excel的工作表,采用工作表的索引值
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFRow row ;
HSSFCell cell1;
int rows=sheet.getLastRowNum();
for(int icount=0;icount<rows;icount++){
row = sheet.getRow(icount);
int line=row.getPhysicalNumberOfCells();
for(int j=0;j<line;j++){
cell1= row.getCell(j);
System.out.println(cell1+"--"+icount+"---"+j);
}
}
//打印读取值
// System.out.println(cell.getStringCellValue());
//新建一输出流
FileOutputStream fout = new FileOutputStream(filename); //PS:filename 是你另存为的路径,不处理直接写入模版文件
//存盘
workbook.write(fout);
fout.flush();
//结束关闭
fout.close();
}
public HSSFCell getCell(HSSFRow row, int index) {
// 取得分发日期单元格
HSSFCell cell = row.getCell(index);
// 如果单元格不存在
if (cell == null) {
// 创建单元格
cell = row.createCell(index);
}
// 返回单元格
return cell;
}
public static void main(String[] args) {
ExcelController ec = new ExcelController();
try {
ec.excel();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
package webservice;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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;
public class ExcelController {
@SuppressWarnings("deprecation")
public void excel() throws FileNotFoundException, IOException{
String filename = "d:\\excel.xls";
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filename));
//按名引用excel工作表
// HSSFSheet sheet = workbook.getSheet("JSP");
//也可以用以下方式来获取excel的工作表,采用工作表的索引值
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFRow row ;
HSSFCell cell1;
int rows=sheet.getLastRowNum();
for(int icount=0;icount<rows;icount++){
row = sheet.getRow(icount);
int line=row.getPhysicalNumberOfCells();
for(int j=0;j<line;j++){
cell1= row.getCell(j);
System.out.println(cell1+"--"+icount+"---"+j);
}
}
//打印读取值
// System.out.println(cell.getStringCellValue());
//新建一输出流
FileOutputStream fout = new FileOutputStream(filename); //PS:filename 是你另存为的路径,不处理直接写入模版文件
//存盘
workbook.write(fout);
fout.flush();
//结束关闭
fout.close();
}
public HSSFCell getCell(HSSFRow row, int index) {
// 取得分发日期单元格
HSSFCell cell = row.getCell(index);
// 如果单元格不存在
if (cell == null) {
// 创建单元格
cell = row.createCell(index);
}
// 返回单元格
return cell;
}
public static void main(String[] args) {
ExcelController ec = new ExcelController();
try {
ec.excel();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询