java poi 导入导出Excel xsl xslx

  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. import org.apache.poi.hssf.usermodel.HSSFWorkbook;    
  7. import org.apache.poi.ss.usermodel.Cell;    
  8. import org.apache.poi.ss.usermodel.Row;    
  9. import org.apache.poi.ss.usermodel.Sheet;    
  10. import org.apache.poi.ss.usermodel.Workbook;    
  11. import org.apache.poi.xssf.usermodel.XSSFWorkbook;    
  12.     
  13. public class FFF {    
  14.     public static void main(String[] args) throws IOException {    
  15.         String path = "d:/";    
  16.         String fileName = "test";    
  17.         String fileType = "xlsx";    
  18.         writer(path, fileName, fileType);    
  19.         read(path, fileName, fileType);    
  20.     }    
  21.     private static void writer(String path, String fileName,String fileType) throws IOException {    
  22.         //创建工作文档对象     
  23.         Workbook wb = null;    
  24.         if (fileType.equals("xls")) {    
  25.             wb = new HSSFWorkbook();    
  26.         }    
  27.         else if(fileType.equals("xlsx"))    
  28.         {    
  29.             wb = new XSSFWorkbook();    
  30.         }    
  31.         else    
  32.         {    
  33.             System.out.println("您的文档格式不正确!");    
  34.         }    
  35.         //创建sheet对象     
  36.         Sheet sheet1 = (Sheet) wb.createSheet("sheet1");    
  37.         //循环写入行数据     
  38.         for (int i = 0; i < 5; i++) {    
  39.             Row row = (Row) sheet1.createRow(i);    
  40.             //循环写入列数据     
  41.             for (int j = 0; j < 8; j++) {    
  42.                 Cell cell = row.createCell(j);    
  43.                 cell.setCellValue("测试"+j);    
  44.             }    
  45.         }    
  46.         //创建文件流     
  47.         OutputStream stream = new FileOutputStream(path+fileName+"."+fileType);    
  48.         //写入数据     
  49.         wb.write(stream);    
  50.         //关闭文件流     
  51.         stream.close();    
  52.     }    
  53.     public static void read(String path,String fileName,String fileType) throws IOException    
  54.     {    
  55.         InputStream stream = new FileInputStream(path+fileName+"."+fileType);    
  56.         Workbook wb = null;    
  57.         if (fileType.equals("xls")) {    
  58.             wb = new HSSFWorkbook(stream);    
  59.         }    
  60.         else if (fileType.equals("xlsx")) {    
  61.             wb = new XSSFWorkbook(stream);    
  62.         }    
  63.         else {    
  64.             System.out.println("您输入的excel格式不正确");    
  65.         }    
  66.         Sheet sheet1 = wb.getSheetAt(0);    
  67.         for (Row row : sheet1) {    
  68.             for (Cell cell : row) {    
  69.                 System.out.print(cell.getStringCellValue()+"  ");    
  70.             }    
  71.             System.out.println();    
  72.         }    
  73.     }    
  74. }
原文地址:https://www.cnblogs.com/Neil223/p/6962525.html