Java实现导入导出Excel文件的方法(poi,jxl)

2025-05-29 0 16

目前,比较常用的实现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>

导出:

  1. publicclassPoiCreateExcel{
  2. publicstaticvoidmain(String[]args){
  3. //创建表头
  4. String[]title={"id","name","sex"};
  5. //创建Excel工作薄
  6. HSSFWorkbookworkbook=newHSSFWorkbook();
  7. //创建一个工作表sheet
  8. HSSFSheetsheet=workbook.createSheet();
  9. //创建第一行
  10. HSSFRowrow=sheet.createRow(0);
  11. HSSFCellcell=null;
  12. //插入第一行
  13. for(inti=0;i<title.length;i++){
  14. cell=row.createCell(i);
  15. cell.setCellValue(title[i]);
  16. }
  17. //追加数据
  18. for(inti=1;i<10;i++){//这里的int起始是1也就是第二行开始
  19. HSSFRownexTrow=sheet.createRow(i);
  20. HSSFCellcell2=nexTrow.createCell(0);
  21. cell2.setCellValue("a"+i);
  22. cell2=nexTrow.createCell(1);
  23. cell2.setCellValue("user");
  24. cell2=nexTrow.createCell(2);
  25. cell2.setCellValue("男");
  26. }
  27. //创建一个文件
  28. Filefile=newFile("d:/poi.xls");
  29. try{
  30. file.createNewFile();
  31. //将内容存盘
  32. FileOutputStreamstream=FileUtils.openOutputStream(file);
  33. workbook.write(stream);
  34. stream.close();
  35. }catch(Exceptione){
  36. e.printStackTrace();
  37. }
  38. }
  39. }

导入:

  1. publicclassPoiReadExcel{
  2. publicstaticvoidmain(String[]args){
  3. //引入需要解析的文件
  4. Filefile=newFile("d:/poi.xls");
  5. try{
  6. //创建Excel读取文件内容
  7. HSSFWorkbookworkbook=newHSSFWorkbook(FileUtils.openInputStream(file));
  8. /**
  9. *第一种方式读取Sheet页
  10. */
  11. //HSSFSheetsheet=workbook.getSheet("Sheet0");
  12. /**
  13. *第二种方式读取Sheet页
  14. */
  15. HSSFSheetsheet=workbook.getSheetAt(0);
  16. intfirstRowNum=0;//起始行第0行
  17. intlasrRowNum=sheet.getLastRowNum();//一直读到最后一行
  18. for(inti=0;i<lasrRowNum;i++){
  19. HSSFRowrow=sheet.getRow(i);
  20. //获取当前最后单元格列号
  21. intlastCellNum=row.getLastCellNum();
  22. for(intj=0;j<lastCellNum;j++){
  23. HSSFCellcell=row.getCell(j);
  24. Stringvalue=cell.getStringCellValue();//注意!如果Excel里面的值是String那么getStringCellValue如果是其他类型则需要修改
  25. System.out.print(value+"");
  26. }
  27. System.out.println();
  28. }
  29. }catch(Exceptione){
  30. e.printStackTrace();
  31. }
  32. }
  33. }

xlsx:

pom:

  1. <!–poi高版本额外包–>
  2. <dependency>
  3. <groupId>org.apache.poi</groupId>
  4. <artifactId>poi-examples</artifactId>
  5. <version>3.9</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.poi</groupId>
  9. <artifactId>poi-excelant</artifactId>
  10. <version>3.9</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.apache.poi</groupId>
  14. <artifactId>poi-ooxml</artifactId>
  15. <version>3.9</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.apache.poi</groupId>
  19. <artifactId>poi-ooxml-schemas</artifactId>
  20. <version>3.9</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.apache.poi</groupId>
  24. <artifactId>poi-scratchpad</artifactId>
  25. <version>3.9</version>
  26. </dependency>

导出:

  1. publicclassPoiCreateExcel{
  2. publicstaticvoidmain(String[]args){
  3. //创建表头
  4. String[]title={"id","name","sex"};
  5. //创建Excel工作薄
  6. XSSFWorkbookworkbook=newXSSFWorkbook();
  7. //创建一个工作表shheet
  8. Sheetsheet=workbook.createSheet();
  9. //创建第一行
  10. Rowrow=sheet.createRow(0);
  11. Cellcell=null;
  12. //插入第一行
  13. for(inti=0;i<title.length;i++){
  14. cell=row.createCell(i);
  15. cell.setCellValue(title[i]);
  16. }
  17. //追加数据
  18. for(inti=1;i<10;i++){//这里的int起始是1也就是第二行开始
  19. RownexTrow=sheet.createRow(i);
  20. Cellcell2=nexTrow.createCell(0);
  21. cell2.setCellValue("a"+i);
  22. cell2=nexTrow.createCell(1);
  23. cell2.setCellValue("user");
  24. cell2=nexTrow.createCell(2);
  25. cell2.setCellValue("男");
  26. }
  27. //创建一个文件
  28. Filefile=newFile("d:/poi.xlsx");//这里可以修改成高版本的
  29. try{
  30. file.createNewFile();
  31. //将内容存盘
  32. FileOutputStreamstream=FileUtils.openOutputStream(file);
  33. workbook.write(stream);
  34. stream.close();
  35. }catch(Exceptione){
  36. e.printStackTrace();
  37. }
  38. }
  39. }

导入:

  1. publicclassPoiReadExcel{
  2. publicList<Double>readExcels(InputStreamis)throwsException{
  3. List<Double>xlsxList=newArrayList<Double>();
  4. try{
  5. if(is==null){
  6. thrownewIOException("文件不正确!");
  7. }
  8. Workbookworkbook=WorkbookFactory.create(is);
  9. FormulaEvaluatorfe=workbook.getCreationHelper().createFormulaEvaluator();
  10. //获取第一张表
  11. Sheetsheet=workbook.getSheetAt(0);
  12. if(sheet==null){
  13. thrownewIOException("传入的excel的第一张表为空!");
  14. }
  15. for(introwNum=0;rowNum<=sheet.getLastRowNum();rowNum++){
  16. Rowrow=sheet.getRow(rowNum);
  17. if(row!=null){
  18. //获得当前行的开始列
  19. intfirstCellNum=row.getFirstCellNum();
  20. //获得当前行的列数
  21. intlastCellNum=row.getPhysicalNumberOfCells();
  22. Stringresult="";
  23. //循环当前行
  24. for(intcellNum=firstCellNum;cellNum<lastCellNum;cellNum++){
  25. Cellcell=row.getCell(cellNum);
  26. doublevalue=0;
  27. StringvalueString=cell.getStringCellValue();
  28. if(null!=fe.evaluate(cell)){
  29. value=fe.evaluate(cell).getNumberValue();
  30. }
  31. //result=result+cellNum+":"+value+"—-";
  32. result=result+cellNum+":"+valueString+"—-";
  33. }
  34. System.out.println(result+"");
  35. }
  36. }
  37. is.close();
  38. }catch(FileNotFoundExceptione){
  39. thrownewException("文件不正确!");
  40. }
  41. returnxlsxList;
  42. }
  43. publicstaticvoidmain(String[]args)throwsException{
  44. InputStreamis=newFileInputStream("d:/poi.xlsx");
  45. PoiReadExcelre=newPoiReadExcel();
  46. re.readExcels(is);
  47. }
  48. }

二,JXL

JXL只能对Excel进行操作,属于比较老的框架,它只支持到Excel 95-2000的版本。现在已经停止更新和维护。

pom:

  1. <!–jxl–>
  2. <dependency>
  3. <groupId>net.sourceforge.jexcelapi</groupId>
  4. <artifactId>jxl</artifactId>
  5. <version>2.6.10</version>
  6. </dependency>

导出:

  1. publicclassJxlCreateExcel{
  2. publicstaticvoidmain(String[]args){
  3. //首先设置表格第一行表格头名称也就是列名
  4. String[]title={"id","name","sex"};
  5. //创建Excel文件存入路径
  6. Filefile=newFile("d:/jxl.xls");
  7. try{
  8. file.createNewFile();
  9. //创建工作薄
  10. WritableWorkbookworkbook=Workbook.createWorkbook(file);
  11. //创建sheet
  12. WritableSheetsheet=workbook.createSheet("sheet1",0);
  13. //添加数据
  14. Labellabel=null;
  15. //第一行设置列名
  16. for(inti=0;i<title.length;i++){
  17. label=newLabel(i,0,title[i]);
  18. sheet.addCell(label);
  19. }
  20. //追加数据从第二行开始i从1开始
  21. for(inti=1;i<9;i++){
  22. label=newLabel(0,i,"id:"+i);
  23. sheet.addCell(label);
  24. label=newLabel(1,i,"user");
  25. sheet.addCell(label);
  26. label=newLabel(2,i,"男");
  27. sheet.addCell(label);
  28. }
  29. //写入并在最后关闭流
  30. workbook.write();
  31. workbook.close();
  32. }catch(Exceptione){
  33. e.printStackTrace();
  34. }
  35. }
  36. }

导入:

  1. publicclassJxlReadExcel{
  2. publicstaticvoidmain(String[]args){
  3. try{
  4. //创建Workbook
  5. Workbookworkbook=Workbook.getWorkbook(newFile("d:/jxl.xls"));
  6. //获取工作表sheet
  7. Sheetsheet=workbook.getSheet(0);
  8. //获取数据
  9. for(inti=0;i<sheet.getRows();i++){//获取行
  10. for(intj=0;j<sheet.getColumns();j++){//获取列
  11. Cellcell=sheet.getCell(j,i);
  12. System.out.print(cell.getContents()+"");//得到单元格的内容
  13. }
  14. System.out.println();
  15. }
  16. workbook.close();
  17. }catch(Exceptione){
  18. e.printStackTrace();
  19. }
  20. }
  21. }

到此,代码可直接部署运行,希望可以帮助到你~

总结

到此这篇关于Java实现导入导出Excel文件的方法(poi,jxl)的文章就介绍到这了,更多相关java实现导入导出excel文件内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

原文链接:https://blog.csdn.net/qq_45150222/article/details/105012569

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java实现导入导出Excel文件的方法(poi,jxl) https://www.kuaiidc.com/114125.html

相关文章

发表评论
暂无评论