首页 > 代码库 > Java实现EXCEL操作(1)

Java实现EXCEL操作(1)

1、实现方法:

现在有三种方法去实现:jxl 、poi 、 FastExcel97~2003

在这里只讲poi实现方法。poi的包可以去Apache官网上去下载:http://poi.apache.org/download.html

2、poi实现

【1】低版本的导入导出方法:

需要导入jar包:

技术分享

技术分享

 

生成EXCEL:

 1 import java.io.File; 2 import java.io.FileOutputStream; 3 import java.io.IOException; 4  5 import org.apache.commons.io.FileUtils; 6 import org.apache.poi.hssf.usermodel.HSSFCell; 7 import org.apache.poi.hssf.usermodel.HSSFRow; 8 import org.apache.poi.hssf.usermodel.HSSFSheet; 9 import org.apache.poi.hssf.usermodel.HSSFWorkbook;10 11 12 public class PoiExpExcel {13 14     /**15      * 生成Excel文件16      */17     public static void main(String[] args) {18         String[] title = {"id", "name", "sex"};19         //创建工作薄20         HSSFWorkbook workbook = new HSSFWorkbook();21         //创建一个工作表sheet22         HSSFSheet sheet = workbook.createSheet();23         //创建爱你第一行24         HSSFRow row = sheet.createRow(0);25         HSSFCell cell = null;26         //插入第一行数据id、name、sex27         for (int i = 0; i < title.length; i++) {28             cell = row.createCell(i);29             cell.setCellValue(title[i]);30             31         }32         //追加数据33         for(int i = 1; i <= 10; i++) {34             HSSFRow nextrow = sheet.createRow(i);35             HSSFCell cell2 = nextrow.createCell(0);36             cell2.setCellValue("" + i);37             cell2 =nextrow.createCell(1);38             cell2.setCellValue("user" + i);39             cell2 =nextrow.createCell(2);40             cell2.setCellValue("男");41         }42         43         44         //创建一个文件45         File file =new File("C:/Users/Administrator/Desktop/demo1.xls");  //在这里填写存放路径46         try {47             file.createNewFile();48             FileOutputStream stream = FileUtils.openOutputStream(file);49             workbook.write(stream);50             51         } catch (Exception e) {52             // TODO Auto-generated catch block53             e.printStackTrace();54         }55     }56 }

 

解析EXCEL:

 1 import java.io.File; 2 import java.io.IOException; 3  4 import org.apache.commons.io.FileUtils; 5 import org.apache.poi.hssf.usermodel.HSSFCell; 6 import org.apache.poi.hssf.usermodel.HSSFRow; 7 import org.apache.poi.hssf.usermodel.HSSFSheet; 8 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 9 10 11 public class PoiReadExcel {12 13     public static void main(String[] args) {14         /**15          * 解析Excel文件16          */17         File file = new File("C:/Users/Administrator/Desktop/demo1.xls");18         try {19             HSSFWorkbook workbook = 20                     new HSSFWorkbook(FileUtils.openInputStream(file));21             //获取第一个工作表workbook.getSheet("Sheet0");22 //            HSSFSheet sheet = workbook.getSheet("Sheet0");23             //读取默认第一个工作表sheet24             HSSFSheet sheet = workbook.getSheetAt(0);25             int firstRowNum = 0;26             //获取sheet中最后一行行号27             int lastRowNum = sheet.getLastRowNum();28             for(int i = firstRowNum; i <= lastRowNum; i++) {29                 HSSFRow row = sheet.getRow(i);30                 //获取当前行最后单元格列号31                 int lastCellNum = row.getLastCellNum();32                 for(int j = 0; j < lastCellNum; j++) {33                     HSSFCell cell = row.getCell(j);34                     String value =http://www.mamicode.com/ cell.getStringCellValue();35                     System.out.print(value + "  ");36                 }37                 System.out.println();38             }39         } catch (IOException e) {40             // TODO Auto-generated catch block41             e.printStackTrace();42         }43     }44 }

 

【2】高版本导入导出的方法:

需要导入包:

技术分享

技术分享

生成EXCEL:

 1 import java.io.File; 2 import java.io.FileOutputStream; 3  4 import org.apache.commons.io.FileUtils; 5 import org.apache.poi.xssf.usermodel.*; 6  7  8 public class CopyOfPoiExp07Excel { 9 10     /**11      * 生成Excel文件12      */13     public static void main(String[] args) {14         String[] title = {"id", "name", "sex"};15         //创建工作薄16         XSSFWorkbook workbook = new XSSFWorkbook();17         //创建一个工作表sheet18         XSSFSheet sheet = workbook.createSheet();19         //创建爱你第一行20         XSSFRow row = sheet.createRow(0);21         XSSFCell cell = null;22         //插入第一行数据id、name、sex23         for (int i = 0; i < title.length; i++) {24             cell = row.createCell(i);25             cell.setCellValue(title[i]);26             27         }28         //追加数据29         for(int i = 1; i <= 10; i++) {30             XSSFRow nextrow = sheet.createRow(i);31             XSSFCell cell2 = nextrow.createCell(0);32             cell2.setCellValue("" + i);33             cell2 =nextrow.createCell(1);34             cell2.setCellValue("user" + i);35             cell2 =nextrow.createCell(2);36             cell2.setCellValue("男");37         }38         39         40         //创建一个文件41         File file =new File("C:/Users/Administrator/Desktop/demo1.xlsx");42         try {43             file.createNewFile();44             FileOutputStream stream = FileUtils.openOutputStream(file);45             workbook.write(stream);46             47         } catch (Exception e) {48             // TODO Auto-generated catch block49             e.printStackTrace();50         }51     }52 }

 

解析EXCEL:

 1 import java.io.File; 2 import java.io.IOException; 3  4 import org.apache.commons.io.FileUtils; 5 import org.apache.poi.xssf.usermodel.XSSFCell; 6 import org.apache.poi.xssf.usermodel.XSSFRow; 7 import org.apache.poi.xssf.usermodel.XSSFSheet; 8 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 9 10 11 public class CopyOfPoiRead07Excel {12 13     public static void main(String[] args) {14         //需要解析的Excel文件15         File file = new File("C:/Users/Administrator/Desktop/demo1.xls");16         try {17             XSSFWorkbook workbook = 18                     new XSSFWorkbook(FileUtils.openInputStream(file));19             //获取第一个工作表workbook.getSheet("Sheet0");20 //            XSSFSheet sheet = workbook.getSheet("Sheet0");21             //读取默认第一个工作表sheet22             XSSFSheet sheet = workbook.getSheetAt(0);23             int firstRowNum = 0;24             //获取sheet中最后一行行号25             int lastRowNum = sheet.getLastRowNum();26             for(int i = firstRowNum; i <= lastRowNum; i++) {27                 XSSFRow row = sheet.getRow(i);28                 //获取当前行最后单元格列号29                 int lastCellNum = row.getLastCellNum();30                 for(int j = 0; j < lastCellNum; j++) {31                     XSSFCell cell = row.getCell(j);32                     String value =http://www.mamicode.com/ cell.getStringCellValue();33                     System.out.print(value + "  ");34                 }35                 System.out.println();36             }37         } catch (IOException e) {38             // TODO Auto-generated catch block39             e.printStackTrace();40         }41     }42 }

生成文件:

            技术分享                             技术分享

读取结果均为:

技术分享

Java实现EXCEL操作(1)