zoukankan      html  css  js  c++  java
  • POI读取xls和xlsx

    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.     }  
    75. }  
     
     
  • 相关阅读:
    JavaScript原型链详解
    Js作用域与闭包
    tjs 在嵌套函数中this关键字引用head对象
    NodeJS stream 一:Buffer
    NodeJS Stream 二:什么是 Stream
    NodeJS Stream 三:readable
    NodeJS Stream 四:Writable
    VSS又一次出错了,神出鬼没的
    VS2005的关于母版页嵌套的一个小技巧
    【转】SQL Server数据库开发的二十一条军规
  • 原文地址:https://www.cnblogs.com/pangpanghuan/p/6402101.html
Copyright © 2011-2022 走看看