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:数据库优化,看这篇就够了
    不使用synchronized和lock,如何实现一个线程安全的单例
    理解Spring:IOC的原理及手动实现
    终于放弃了单调的swagger-ui了,选择了这款神器—knife4j
    TP5.0.24 验证器内置规则中max 如果输入中文 验证长度错误的问题
    laravel 5.5 api接口开发:JWT安装+实现API token 认证
    homestead 代码与本地代码不同步的解决方法
    laravel 5.5 api接口开发: 安装dingo/api
    php base_decode 函数将base64编码转换图片遇到的问题
    thinkphp 5.0 部署新网空间隐藏index.php入口
  • 原文地址:https://www.cnblogs.com/linkstar/p/5663339.html
Copyright © 2011-2022 走看看