zoukankan      html  css  js  c++  java
  • java导出和读取excel数据

    使用的是poi的jar包

    下载地址http://poi.apache.org/download.html

    主要是把jar包导入,直接新建一个列子测试一下就明白了。使用起来还是比较方便的,代码里面的原理也很好懂。

    下面的代码中一共给出三个方法,第一个主要是用在写成xlsx后缀的文件使用的,后面两个方法是直接照搬网上的,通用方法无论什么版本均可以使用。

    可以根据自己的需要对里面的东西进行修改

    package excelTest;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    /**
     * 读取写入excel的demo
     * @author XX
     *
     */
    public class Test {
        
        public static void main(String[] args) throws IOException {    
            String path = "E:/";    
            String fileName = "test";    
            String fileType = "xlsx";    
            writeXLSX(path, fileName, fileType);    
            read(path, fileName, fileType);    
        }    
        
        /**
         * 写入xlsx文件的方法
         * @param path
         * @param fileName
         * @param fileType
         */
        private static void writeXLSX(String path, String fileName,String fileType)
        {
            XSSFWorkbook workBook = new XSSFWorkbook();
            XSSFSheet sheet1 = workBook.createSheet("sheet1");
            
            //创建第一行表头
            XSSFRow row = sheet1.createRow(0);
            for (int j = 0; j < 8; j++) {    
                XSSFCell cell = row.createCell(j);    
                cell.setCellValue("功能"+j);    
            }
            
            for (int i = 1; i < 10; i++) {    
                XSSFRow row2 = sheet1.createRow(i);    
                //循环写入列数据     
                for (int j = 0; j < 8; j++) {    
                    XSSFCell cell = row2.createCell(j);    
                    cell.setCellValue("数据"+ 123456789 + j);    
                }    
            }    
            OutputStream stream = null;
            try {
                stream = new FileOutputStream(path+fileName+"."+fileType);
                workBook.write(stream);    
                workBook.close();
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                try {
                    stream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
        /**
         * 可以使用两种不同格式的创建excel的方法
         * @param path
         * @param fileName
         * @param fileType
         * @throws IOException
         */
        private static void writer(String path, String fileName,String fileType) throws IOException {    
            //创建工作文档对象     
            Workbook wb = null;    
            if (fileType.equals("xls")) {    
                wb = new HSSFWorkbook();    
            }    
            else if(fileType.equals("xlsx"))    
            {    
                wb = new XSSFWorkbook();    
            }    
            else    
            {    
                System.out.println("您的文档格式不正确!");    
            }    
            //创建sheet对象     
            Sheet sheet1 = (Sheet) wb.createSheet("sheet1");    
            //循环写入行数据     
            for (int i = 0; i < 65539; i++) {    
                Row row = (Row) sheet1.createRow(i);    
                //循环写入列数据     
                for (int j = 0; j < 8; j++) {    
                    Cell cell = row.createCell(j);    
                    cell.setCellValue("测试"+j);    
                }    
            }    
            //创建文件流     
            OutputStream stream = new FileOutputStream(path+fileName+"."+fileType);    
            //写入数据     
            wb.write(stream);    
            //关闭文件流     
            stream.close();    
        }    
        
        /**
         * 使用两种不同格式的读取excel的方法
         * @param path
         * @param fileName
         * @param fileType
         * @throws IOException
         */
        public static void read(String path,String fileName,String fileType) throws IOException    
        {    
            InputStream stream = new FileInputStream(path+fileName+"."+fileType);    
            Workbook wb = null;    
            if (fileType.equals("xls")) {    
                wb = new HSSFWorkbook(stream);    
            }    
            else if (fileType.equals("xlsx")) {    
                wb = new XSSFWorkbook(stream);    
            }    
            else {    
                System.out.println("您输入的excel格式不正确");    
            }    
            Sheet sheet1 = wb.getSheetAt(0);    
            for (Row row : sheet1) {    
                for (Cell cell : row) {    
                    System.out.print(cell.getStringCellValue()+"  ");    
                }    
                System.out.println();    
            }    
        } 
    }


    后面两个方法转载自:http://blog.csdn.net/ptzrbin/article/details/8751229
  • 相关阅读:
    Mysql子查询、关联查询
    Mysql 注意细节
    IE和FF区别关于css和js
    php 使用curl模拟登录人人(校内)网
    解析php mysql 事务处理回滚操作
    《Linux内核设计的艺术》学习笔记(二)INT 0x13中断
    《Linux内核设计的艺术》学习笔记(一)从开机加电到加载三个汇编源码
    CSS笔记(一)CSS规则
    HTML笔记(七)head相关元素<base> & <meta>
    HTML笔记(六)文档类型
  • 原文地址:https://www.cnblogs.com/linkstar/p/5663339.html
Copyright © 2011-2022 走看看