zoukankan      html  css  js  c++  java
  • Java代码导入导出 Excel 表格最简单的方法

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    
    public class Excel {
        
        public static void main(String[] args) {
            deriveTable();
            importTable();
        }
            //    导出Excel表格的方法    
        public static void deriveTable(){
            // 创建Excel文件对应的对象
            HSSFWorkbook hwk = new HSSFWorkbook();
            // 创建一个sheet表名
            HSSFSheet hssfSheet = hwk.createSheet("工作账单");        
            // 通过sheet创建一盒row(行) 范围0-65535
            HSSFRow hssfRow1 = hssfSheet.createRow(0);
            HSSFRow hssfRow2 = hssfSheet.createRow(1);
            HSSFRow hssfRow3= hssfSheet.createRow(2);
            HSSFRow hssfRow4= hssfSheet.createRow(3);
            HSSFRow hssfRow5 = hssfSheet.createRow(4);
            //通过row创建一个cell 一个cell就是一个单元格 范围0-255
            HSSFCell cell1 = hssfRow1.createCell(0);
            HSSFCell cell2 = hssfRow1.createCell(1);
            HSSFCell cell3 = hssfRow1.createCell(2);
            HSSFCell cell4 = hssfRow2.createCell(1);
            HSSFCell cell5 = hssfRow2.createCell(2);
            HSSFCell cell6 = hssfRow3.createCell(0);
            HSSFCell cell7 = hssfRow3.createCell(2);
            HSSFCell cell8 = hssfRow3.createCell(3);
            HSSFCell cell9 = hssfRow4.createCell(3);
            HSSFCell cell10 = hssfRow4.createCell(4);
            // 给单元格里写入类容
            cell1.setCellValue("第1行第1列");
            cell2.setCellValue("第1行第2列");
            cell3.setCellValue("第1行第3列");
            cell4.setCellValue("第2行第2列");
            cell5.setCellValue("第2行第3列");
            cell6.setCellValue("第3行第1列");
            cell7.setCellValue("第3行第3列");
            cell8.setCellValue("第3行第4列");
            cell9.setCellValue("第4行第4列");
            cell10.setCellValue("第4行第5列");
            FileOutputStream fos=null;
            try {
                fos = new FileOutputStream("e:/Excel.xls");
                hwk.write(fos);    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }    
        }
            //    导入Excel表格的方法    
        public static void  importTable(){
            
            FileInputStream fis = null;
            try {
             fis = new FileInputStream("e:/Excel.xls");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            try {
                HSSFWorkbook hwk = new HSSFWorkbook(fis);
                HSSFSheet sheet = hwk.getSheetAt(0);
                //遍历表格中所有的行 sheet.getLastCellNum 是获取最后一个不为空的行是第几个。 
                for (int i = 0; i<sheet.getLastRowNum(); i++) {
                    if(sheet.getRow(i)==null){
                        continue;
                    }
                    
                    //遍历表格中所有的单元格 sheet.getRow(i).getLastCellNum() 是获取最后一个不为空的列是第几个。 
                    
                    for (int j = 0; j<sheet.getRow(i).getLastCellNum() ; j++) {
                        if(sheet.getRow(i).getCell(j)==null){
                            continue;
                        }
                        System.out.println(sheet.getRow(i).getCell(j).getStringCellValue());    
                    }
                    System.out.println();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }    
        }
    }
  • 相关阅读:
    htpasswd命令
    GitHub访问速度慢的解决方法
    easyui datagrid 首次不加载做法
    Excel日常操作
    补偿接口中循环一直执行sql的问题
    rabbitMq无法消费发送的q的问题
    Unicode与中文转换工具类方法(转)
    idea 一些插件配置
    线程安全的集合类、CopyOnWrite机制介绍(转)
    java websocket学习
  • 原文地址:https://www.cnblogs.com/wodebokezhijian/p/7757896.html
Copyright © 2011-2022 走看看