zoukankan      html  css  js  c++  java
  • Java操作Excel之Poi和Jxl

    Java操作Excel的方式有Poi和Jxl两总,以下代码为简单实现:

    Poi

    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
    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.ss.usermodel.WorkbookFactory;
    import org.springframework.stereotype.Component;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    @Component
    public class ExcelByPoi {
    
        public static void writeExcelByPoi(String path) {
            try {
                // 创建工作薄
                HSSFWorkbook workbook = new HSSFWorkbook();
                // 创建工作表
                HSSFSheet sheet = workbook.createSheet("sheet1");
    
                for (int row = 0; row < 10; row++)
                {
                    HSSFRow rows = sheet.createRow(row);
                    for (int col = 0; col < 10; col++)
                    {
                        if(row == 0) {
                            //添加表头
                            rows.createCell(0).setCellValue("姓名");
                            rows.createCell(1).setCellValue("班级");
                            rows.createCell(2).setCellValue("性别");
                            rows.createCell(3).setCellValue("年龄");
                            rows.createCell(4).setCellValue("生日");
    
                            rows.createCell(5).setCellValue("年纪");
                            rows.createCell(6).setCellValue("学校");
                            rows.createCell(7).setCellValue("班主任");
                            rows.createCell(8).setCellValue("爸爸");
                            rows.createCell(9).setCellValue("妈妈");
                        } else {
                            // 向工作表中添加数据
                            rows.createCell(col).setCellValue("data" + row + col);
                        }
                    }
                }
    
                File xlsFile = new File(path);
                FileOutputStream xlsStream = new FileOutputStream(xlsFile);
                workbook.write(xlsStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    
        public static void readExcelByPoi(String path) {
            try {
                File xlsFile = new File(path);
                // 获得工作簿
                Workbook workbook = WorkbookFactory.create(xlsFile);
                // 获得工作表个数
                int sheetCount = workbook.getNumberOfSheets();
                // 遍历工作表
                for (int i = 0; i < sheetCount; i++)
                {
                    Sheet sheet = workbook.getSheetAt(i);
                    // 获得行数
                    int rows = sheet.getLastRowNum() + 1;
                    // 获得列数,先获得一行,在得到改行列数
                    Row tmp = sheet.getRow(0);
                    if (tmp == null)
                    {
                        continue;
                    }
                    int cols = tmp.getPhysicalNumberOfCells();
                    // 读取数据
                    for (int row = 0; row < rows; row++)
                    {
                        Row r = sheet.getRow(row);
                        for (int col = 0; col < cols; col++)
                        {
                            System.out.printf("%10s", r.getCell(col).getStringCellValue());
                        }
                        System.out.println();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InvalidFormatException e) {
                e.printStackTrace();
            }
        }
    
    }

    Jxl

           <dependency>
                <groupId>net.sourceforge.jexcelapi</groupId>
                <artifactId>jxl</artifactId>
                <version>2.6.10</version>
            </dependency>     
    import java.io.File;
    import java.io.IOException;
    import jxl.Workbook;
    import jxl.write.Label;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;
    import jxl.Sheet;
    import jxl.read.biff.BiffException;
    import org.springframework.stereotype.Component;
    
    @Component
    public class ExcelByJxl {
    
        public static void writeExcelByJxl(String path) {
            try {
                File xlsFile = new File(path);
                // 创建一个工作簿
                WritableWorkbook workbook = Workbook.createWorkbook(xlsFile);
                // 创建一个工作表
                WritableSheet sheet = workbook.createSheet("sheet1", 0);
                for (int row = 0; row < 10; row++)
                {
                    for (int col = 0; col < 10; col++)
                    {
                        // 向工作表中添加数据
                        sheet.addCell(new Label(col, row, "data" + row + col));
                    }
                }
    
                // 创建一个工作表
                WritableSheet sheet1 = workbook.createSheet("sheet2", 0);
                for (int row = 0; row < 10; row++)
                {
                    for (int col = 0; col < 10; col++)
                    {
                        if(row == 0) {
                            //添加表头
                            sheet1.addCell(new Label(0, 0, "班级"));
                            sheet1.addCell(new Label(1, 0, "姓名"));
                            sheet1.addCell(new Label(2, 0, "年级"));
                            sheet1.addCell(new Label(3, 0, "年龄"));
                            sheet1.addCell(new Label(4, 0, "生日"));
                            sheet1.addCell(new Label(5, 0, "住址"));
                            sheet1.addCell(new Label(6, 0, "性别"));
                            sheet1.addCell(new Label(7, 0, "爸爸"));
                            sheet1.addCell(new Label(8, 0, "妈妈"));
                            sheet1.addCell(new Label(9, 0, "班主任"));
                        }else {
                            // 向工作表中添加数据
                            sheet1.addCell(new Label(col, row, "data" + row + col));
                        }
                    }
                }
    
                workbook.write();
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (WriteException e) {
                e.printStackTrace();
            }
        }
    
        public static void readExcelByJxl(String path) {
            try {
                File xlsFile = new File(path);
                // 获得工作簿对象
                Workbook workbook = Workbook.getWorkbook(xlsFile);
                // 获得所有工作表
                Sheet[] sheets = workbook.getSheets();
                // 遍历工作表
                if (sheets != null)
                {
                    for (Sheet sheet : sheets)
                    {
                        // 获得行数
                        int rows = sheet.getRows();
                        // 获得列数
                        int cols = sheet.getColumns();
                        // 读取数据
                        for (int row = 0; row < rows; row++)
                        {
                            for (int col = 0; col < cols; col++)
                            {
                                System.out.printf("%10s", sheet.getCell(col, row)
                                        .getContents());
                            }
                            System.out.println();
                        }
                    }
                }
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (BiffException e) {
                e.printStackTrace();
            }
        }
    
    
    
    }

    第三种操作Excel方式 

  • 相关阅读:
    第十八章、使用集合
    第十九章、枚举集合
    第十七章、泛型概述
    第十六章、使用索引器
    第十五章、实现属性以访问字段
    第十四章、使用垃圾回收和资源管理
    第十三章、创建接口和定义抽象类
    AtCoder Grand Contest 018 E
    AtCoder Regular Contest 059 F Unhappy Hacking
    Codeforces 464E. The Classic Problem
  • 原文地址:https://www.cnblogs.com/mxh-java/p/12513692.html
Copyright © 2011-2022 走看看