POI读取XLS、XLSX

来源:互联网 发布:腾讯软件管家mac版 编辑:程序博客网 时间:2024/05/16 01:53

转自:http://blog.csdn.net/lihaiyun718/article/details/8197596

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


0 0
原创粉丝点击