目前,比较常用的实现Java导入、导出Excel的技术有两种Jakarta POI和Java Excel直接上代码:
一,POI
POI是apache的项目,可对微软的Word,Excel,Ppt进行操作,包括office2003和2007,Excl2003和2007。poi现在一直有更新。所以现在主流使用POI。
xls:
pom:
?
|
1
2
3
4
5
6
7
8
9
10
|
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
|
导出:
- publicclassPoiCreateExcel{
- publicstaticvoidmain(String[]args){
- //创建表头
- String[]title={"id","name","sex"};
- //创建Excel工作薄
- HSSFWorkbookworkbook=newHSSFWorkbook();
- //创建一个工作表sheet
- HSSFSheetsheet=workbook.createSheet();
- //创建第一行
- HSSFRowrow=sheet.createRow(0);
- HSSFCellcell=null;
- //插入第一行
- for(inti=0;i<title.length;i++){
- cell=row.createCell(i);
- cell.setCellValue(title[i]);
- }
- //追加数据
- for(inti=1;i<10;i++){//这里的int起始是1也就是第二行开始
- HSSFRownexTrow=sheet.createRow(i);
- HSSFCellcell2=nexTrow.createCell(0);
- cell2.setCellValue("a"+i);
- cell2=nexTrow.createCell(1);
- cell2.setCellValue("user");
- cell2=nexTrow.createCell(2);
- cell2.setCellValue("男");
- }
- //创建一个文件
- Filefile=newFile("d:/poi.xls");
- try{
- file.createNewFile();
- //将内容存盘
- FileOutputStreamstream=FileUtils.openOutputStream(file);
- workbook.write(stream);
- stream.close();
- }catch(Exceptione){
- e.printStackTrace();
- }
- }
- }
导入:
- publicclassPoiReadExcel{
- publicstaticvoidmain(String[]args){
- //引入需要解析的文件
- Filefile=newFile("d:/poi.xls");
- try{
- //创建Excel读取文件内容
- HSSFWorkbookworkbook=newHSSFWorkbook(FileUtils.openInputStream(file));
- /**
- *第一种方式读取Sheet页
- */
- //HSSFSheetsheet=workbook.getSheet("Sheet0");
- /**
- *第二种方式读取Sheet页
- */
- HSSFSheetsheet=workbook.getSheetAt(0);
- intfirstRowNum=0;//起始行第0行
- intlasrRowNum=sheet.getLastRowNum();//一直读到最后一行
- for(inti=0;i<lasrRowNum;i++){
- HSSFRowrow=sheet.getRow(i);
- //获取当前最后单元格列号
- intlastCellNum=row.getLastCellNum();
- for(intj=0;j<lastCellNum;j++){
- HSSFCellcell=row.getCell(j);
- Stringvalue=cell.getStringCellValue();//注意!如果Excel里面的值是String那么getStringCellValue如果是其他类型则需要修改
- System.out.print(value+"");
- }
- System.out.println();
- }
- }catch(Exceptione){
- e.printStackTrace();
- }
- }
- }
xlsx:
pom:
- <!–poi高版本额外包–>
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi-examples</artifactId>
- <version>3.9</version>
- </dependency>
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi-excelant</artifactId>
- <version>3.9</version>
- </dependency>
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi-ooxml</artifactId>
- <version>3.9</version>
- </dependency>
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi-ooxml-schemas</artifactId>
- <version>3.9</version>
- </dependency>
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi-scratchpad</artifactId>
- <version>3.9</version>
- </dependency>
导出:
- publicclassPoiCreateExcel{
- publicstaticvoidmain(String[]args){
- //创建表头
- String[]title={"id","name","sex"};
- //创建Excel工作薄
- XSSFWorkbookworkbook=newXSSFWorkbook();
- //创建一个工作表shheet
- Sheetsheet=workbook.createSheet();
- //创建第一行
- Rowrow=sheet.createRow(0);
- Cellcell=null;
- //插入第一行
- for(inti=0;i<title.length;i++){
- cell=row.createCell(i);
- cell.setCellValue(title[i]);
- }
- //追加数据
- for(inti=1;i<10;i++){//这里的int起始是1也就是第二行开始
- RownexTrow=sheet.createRow(i);
- Cellcell2=nexTrow.createCell(0);
- cell2.setCellValue("a"+i);
- cell2=nexTrow.createCell(1);
- cell2.setCellValue("user");
- cell2=nexTrow.createCell(2);
- cell2.setCellValue("男");
- }
- //创建一个文件
- Filefile=newFile("d:/poi.xlsx");//这里可以修改成高版本的
- try{
- file.createNewFile();
- //将内容存盘
- FileOutputStreamstream=FileUtils.openOutputStream(file);
- workbook.write(stream);
- stream.close();
- }catch(Exceptione){
- e.printStackTrace();
- }
- }
- }
导入:
- publicclassPoiReadExcel{
- publicList<Double>readExcels(InputStreamis)throwsException{
- List<Double>xlsxList=newArrayList<Double>();
- try{
- if(is==null){
- thrownewIOException("文件不正确!");
- }
- Workbookworkbook=WorkbookFactory.create(is);
- FormulaEvaluatorfe=workbook.getCreationHelper().createFormulaEvaluator();
- //获取第一张表
- Sheetsheet=workbook.getSheetAt(0);
- if(sheet==null){
- thrownewIOException("传入的excel的第一张表为空!");
- }
- for(introwNum=0;rowNum<=sheet.getLastRowNum();rowNum++){
- Rowrow=sheet.getRow(rowNum);
- if(row!=null){
- //获得当前行的开始列
- intfirstCellNum=row.getFirstCellNum();
- //获得当前行的列数
- intlastCellNum=row.getPhysicalNumberOfCells();
- Stringresult="";
- //循环当前行
- for(intcellNum=firstCellNum;cellNum<lastCellNum;cellNum++){
- Cellcell=row.getCell(cellNum);
- doublevalue=0;
- StringvalueString=cell.getStringCellValue();
- if(null!=fe.evaluate(cell)){
- value=fe.evaluate(cell).getNumberValue();
- }
- //result=result+cellNum+":"+value+"—-";
- result=result+cellNum+":"+valueString+"—-";
- }
- System.out.println(result+"");
- }
- }
- is.close();
- }catch(FileNotFoundExceptione){
- thrownewException("文件不正确!");
- }
- returnxlsxList;
- }
- publicstaticvoidmain(String[]args)throwsException{
- InputStreamis=newFileInputStream("d:/poi.xlsx");
- PoiReadExcelre=newPoiReadExcel();
- re.readExcels(is);
- }
- }
二,JXL
JXL只能对Excel进行操作,属于比较老的框架,它只支持到Excel 95-2000的版本。现在已经停止更新和维护。
pom:
- <!–jxl–>
- <dependency>
- <groupId>net.sourceforge.jexcelapi</groupId>
- <artifactId>jxl</artifactId>
- <version>2.6.10</version>
- </dependency>
导出:
- publicclassJxlCreateExcel{
- publicstaticvoidmain(String[]args){
- //首先设置表格第一行表格头名称也就是列名
- String[]title={"id","name","sex"};
- //创建Excel文件存入路径
- Filefile=newFile("d:/jxl.xls");
- try{
- file.createNewFile();
- //创建工作薄
- WritableWorkbookworkbook=Workbook.createWorkbook(file);
- //创建sheet
- WritableSheetsheet=workbook.createSheet("sheet1",0);
- //添加数据
- Labellabel=null;
- //第一行设置列名
- for(inti=0;i<title.length;i++){
- label=newLabel(i,0,title[i]);
- sheet.addCell(label);
- }
- //追加数据从第二行开始i从1开始
- for(inti=1;i<9;i++){
- label=newLabel(0,i,"id:"+i);
- sheet.addCell(label);
- label=newLabel(1,i,"user");
- sheet.addCell(label);
- label=newLabel(2,i,"男");
- sheet.addCell(label);
- }
- //写入并在最后关闭流
- workbook.write();
- workbook.close();
- }catch(Exceptione){
- e.printStackTrace();
- }
- }
- }
导入:
- publicclassJxlReadExcel{
- publicstaticvoidmain(String[]args){
- try{
- //创建Workbook
- Workbookworkbook=Workbook.getWorkbook(newFile("d:/jxl.xls"));
- //获取工作表sheet
- Sheetsheet=workbook.getSheet(0);
- //获取数据
- for(inti=0;i<sheet.getRows();i++){//获取行
- for(intj=0;j<sheet.getColumns();j++){//获取列
- Cellcell=sheet.getCell(j,i);
- System.out.print(cell.getContents()+"");//得到单元格的内容
- }
- System.out.println();
- }
- workbook.close();
- }catch(Exceptione){
- e.printStackTrace();
- }
- }
- }
到此,代码可直接部署运行,希望可以帮助到你~
总结
到此这篇关于Java实现导入导出Excel文件的方法(poi,jxl)的文章就介绍到这了,更多相关java实现导入导出excel文件内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!
原文链接:https://blog.csdn.net/qq_45150222/article/details/105012569
相关文章
猜你喜欢
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 86
-
2025-05-25 30
-
2025-05-29 12
-
2025-05-25 22
-
2025-06-04 102
热门评论

