zoukankan      html  css  js  c++  java
  • excel导入到java/导出到excel

    package com.test.order.config;
    
    import com.test.order.domain.HavalDO;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.xssf.usermodel.*;
    
    import java.io.*;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @program: TestDemo
     * @Date: 2018/8/19 12:55
     * @Author: Mr.Niu
     * @Description:
     */
    public class ExcelHelper {
    
        public List<HavalDO> excleIn() {
            List<HavalDO> list = new ArrayList<HavalDO>();
            HavalDO havalDO = null;
            try {
                InputStream is = new FileInputStream("E:\bzk.xlsx");
                XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
                // 获取选项卡对象 第0个选项卡 , 因为们这里只有一个选项卡,如果你每个选项卡的内容是一样,可以通过循环取出
                XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
                // 循环取出每行的值
                for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
                    XSSFRow xssfRow = xssfSheet.getRow(rowNum);
                    havalDO = new HavalDO();
                    //注意Poi读取的内容是有类型的,处理起来也jxl有所不同
                    String str = null;
                    if(xssfRow.getCell(0)!=null){
                        xssfRow.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                        str = xssfRow.getCell(0).getStringCellValue();
                    }
                    String [] strs=str.split("-");
                    String append = null;
                    for (String s1 :strs){
                        append+=s1;
                    }
                    String strss = append.substring(4,append.length());
                    havalDO.setOem(strss);
                    havalDO.setName(xssfRow.getCell(1).getStringCellValue());
                    havalDO.setPrice(xssfRow.getCell(2).getNumericCellValue());
                    list.add(havalDO);
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return list;
        }
    
        /**
         * 针对Book类进行导出的操作
         *
         * @param list
         */
        public void excleOut(List<HavalDO> list) {
            // 创建Excel文档
            XSSFWorkbook hwb = new XSSFWorkbook();
            // 通过excle对象创建一个选项卡对象
            XSSFSheet sheet = hwb.createSheet("sheet1");
            HavalDO havalDO = null;
            // 循环list创建行
            // 新建一行
            XSSFRow row = sheet.createRow(0);
                // 设置i+1行第0列的数据
                row.createCell(0).setCellValue("oem");
                // 设置i+1行第1列的数据
                row.createCell(1).setCellValue("name");
                // 设置i+1行第2列的数据
                row.createCell(2).setCellValue("price_4s");
            //
            for (int i = 1; i < list.size(); i++) {
                // 新建一行
                row = sheet.createRow(i);
                havalDO = list.get(i);
                // 设置i+1行第0列的数据
                row.createCell(0).setCellValue(havalDO.getOem());
                // 设置i+1行第1列的数据
                row.createCell(1).setCellValue(havalDO.getName());
                // 设置i+1行第2列的数据
                row.createCell(2).setCellValue(havalDO.getPrice());
            }
            OutputStream out = null;
            try {
                out = new FileOutputStream("E:/bookPoi1.xlsx");
                hwb.write(out);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    out.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        public static void main(String[] args) {
                ExcelHelper eh = new ExcelHelper();
                List<HavalDO> lh = eh.excleIn();
                Integer i = 0;
                for (HavalDO hd : lh) {
                    i++;
                    System.out.println("第" + i + "行数据:" + hd.toString());
                }
                eh.excleOut(lh);
            }
    }
    
  • 相关阅读:
    URL传递的参数是UTF-8编码,在打开的页面正常显示(GB2312)的方法
    JS windows.open打开窗口并居中
    一个tomcat如何部署多个项目运行
    redis 服务配置开机自启动
    解决端口被占用问题
    MySQL中concat以及group_concat的使用
    java 使用OpenOffice文件实现预览
    eclipse安装maven插件
    数据库三范式
    mysql 查询的字段值太长显示不全 group_concat
  • 原文地址:https://www.cnblogs.com/EveningWind/p/9501415.html
Copyright © 2011-2022 走看看