POI读取xls和xlsx

来源:互联网 发布:java 接口 调用接口 编辑:程序博客网 时间:2024/05/21 19:21
  1. import java.io.FileInputStream;  
  2. import java.io.FileOutputStream;  
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.io.OutputStream;  
  6.   
  7. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  8. import org.apache.poi.ss.usermodel.Cell;  
  9. import org.apache.poi.ss.usermodel.Row;  
  10. import org.apache.poi.ss.usermodel.Sheet;  
  11. import org.apache.poi.ss.usermodel.Workbook;  
  12. import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  13.   
  14. public class TestReadAndWrite {  
  15.     public static void main(String[] args) throws IOException {  
  16.         String path = "d:/";  
  17.         String fileName = "test";  
  18.         String fileType = "xlsx";  
  19.         writer(path, fileName, fileType);  
  20.         read(path, fileName, fileType);  
  21.     }  
  22.     private static void writer(String path, String fileName,String fileType) throws IOException {  
  23.         //创建工作文档对象  
  24.         Workbook wb = null;  
  25.         if (fileType.equals("xls")) {  
  26.             wb = new HSSFWorkbook();  
  27.         }  
  28.         else if(fileType.equals("xlsx"))  
  29.         {  
  30.             wb = new XSSFWorkbook();  
  31.         }  
  32.         else  
  33.         {  
  34.             System.out.println("您的文档格式不正确!");  
  35.         }  
  36.         //创建sheet对象  
  37.         Sheet sheet1 = (Sheet) wb.createSheet("sheet1");  
  38.         //循环写入行数据  
  39.         for (int i = 0; i < 5; i++) {  
  40.             Row row = (Row) sheet1.createRow(i);  
  41.             //循环写入列数据  
  42.             for (int j = 0; j < 8; j++) {  
  43.                 Cell cell = row.createCell(j);  
  44.                 cell.setCellValue("测试"+j);  
  45.             }  
  46.         }  
  47.         //创建文件流  
  48.         OutputStream stream = new FileOutputStream(path+fileName+"."+fileType);  
  49.         //写入数据  
  50.         wb.write(stream);  
  51.         //关闭文件流  
  52.         stream.close();  
  53.     }  
  54.     public static void read(String path,String fileName,String fileType) throws IOException  
  55.     {  
  56.         InputStream stream = new FileInputStream(path+fileName+"."+fileType);  
  57.         Workbook wb = null;  
  58.         if (fileType.equals("xls")) {  
  59.             wb = new HSSFWorkbook(stream);  
  60.         }  
  61.         else if (fileType.equals("xlsx")) {  
  62.             wb = new XSSFWorkbook(stream);  
  63.         }  
  64.         else {  
  65.             System.out.println("您输入的excel格式不正确");  
  66.         }  
  67.         Sheet sheet1 = wb.getSheetAt(0);  
  68.         for (Row row : sheet1) {  
  69.             for (Cell cell : row) {  
  70.                 System.out.print(cell.getStringCellValue()+"  ");  
  71.             }  
  72.             System.out.println();  
  73.         }  
  74.     }