Excel文件解析

POI

复制代码
//创建工作簿
XSSFWorkbook workbook = new XSSFWorkbook();

//创建工作表
XSSFSheet sheet=workbook.createSheet("Sheet1");

//创建行
XSSFRow row=sheet.createRow(0);

//根据行创建单元格
XSSFCell cell=row.createCell(0);
XSSFCell cell2=row.createCell(1);

//填充值
cell.setCellValue("Hello World");
cell2.setCellValue("Hello Java");

try(BufferedOutputStream outputStream=new BufferedOutputStream(new FileOutputStream("D:\\IOTest\\test.xlsx"))){
    workbook.write(outputStream);
}catch (IOException e){
    e.printStackTrace();
}finally {
    workbook.close();
}
;
java 复制代码
        //创建工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();

        //创建工作表
        XSSFSheet sheet=workbook.createSheet("Sheet1");

        //创建行
        XSSFRow row=sheet.createRow(0);

        //根据行创建单元格
        XSSFCell cell1=row.createCell(0);
        XSSFCell cell2=row.createCell(1);
        XSSFCell cell3=row.createCell(2);
        XSSFCell cell4=row.createCell(3);

        cell1.setCellValue("序号");
        cell2.setCellValue("优惠码");
        cell3.setCellValue("校验码");
        cell4.setCellValue("创建日期");
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");

        for(int i=1;i<=1000;i++){
            row=sheet.createRow(i);
            cell1=row.createCell(0);
            cell2=row.createCell(1);
            cell3=row.createCell(2);
            cell4=row.createCell(3);
            cell1.setCellValue(String.valueOf(i));
            cell2.setCellValue(UUID.randomUUID().toString().substring(0,8));
            cell3.setCellValue(UUID.randomUUID().toString().substring(0,4));
            cell4.setCellValue(LocalDateTime.now().format(formatter));
        }
        try(BufferedOutputStream outputStream=new BufferedOutputStream(new FileOutputStream("D:\\IOTest\\test2.xlsx"))){
            workbook.write(outputStream);
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            workbook.close();
        }
复制代码
try (//创建工作簿
     XSSFWorkbook workbook = new XSSFWorkbook();
     BufferedOutputStream outputStream=new BufferedOutputStream(new FileOutputStream("D:\\IOTest\\test3.xlsx"))
        ){
    //创建单元格表头样式
    XSSFCellStyle headerStyle = workbook.createCellStyle();

    //设置单元格的水平对齐类型为居中对齐
    headerStyle.setAlignment(HorizontalAlignment.CENTER);

    //设置单元格的垂直对齐方式为居中对齐
    headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);

    //设置字体样式
    Font headerFont = workbook.createFont();
    headerFont.setBold(true);  //是否加粗
    headerFont.setColor(Font.COLOR_RED); //颜色设置为红色
    headerStyle.setFont(headerFont);

    //创建工作表
    XSSFSheet sheet=workbook.createSheet("礼品金额表");

    //创建行
    Row row=sheet.createRow(0);

    //根据行创建单元格
    Cell cell1=row.createCell(0);
    cell1.setCellStyle(headerStyle);//设置单元格样式
    cell1.setCellValue("序号");  //设置单元格的值

    Cell cell2=row.createCell(1);
    cell2.setCellStyle(headerStyle);
    cell2.setCellValue("礼品金额");
    Cell cell3=row.createCell(2);
    cell3.setCellStyle(headerStyle);
    cell3.setCellValue("时间");

    Random random=new Random();

    //创建单元格样式
    XSSFCellStyle moneyStyle=workbook.createCellStyle();
    XSSFCellStyle dateStyle=workbook.createCellStyle();

    //设置样式
    moneyStyle.setAlignment(HorizontalAlignment.LEFT);
    dateStyle.setAlignment(HorizontalAlignment.LEFT);

    //创建数据格式对象
    DataFormat moneyFormat = workbook.createDataFormat();
    //给数据格式样式对象设置样式
    short shortMoney=moneyFormat.getFormat("¥###,#");

    DataFormat dateFormat = workbook.createDataFormat();
    short shortDate=moneyFormat.getFormat("yyyy年MM月dd日");

    //给单元格设置数据样式
    moneyStyle.setDataFormat(shortMoney);
    dateStyle.setDataFormat(shortDate);
    for(int i=1;i<=1000;i++){
        Row r=sheet.createRow(i);
        Cell c1=r.createCell(0);
        c1.setCellValue(i);

        Cell c2=r.createCell(1);
        c2.setCellStyle(moneyStyle);
        c2.setCellValue(random.nextInt(1000000));

        Cell c3=r.createCell(2);
        c3.setCellStyle(dateStyle);
        c3.setCellValue(new Date());

    }
    workbook.write(outputStream);
    System.out.println("程序结束");

}catch (IOException e){
    e.printStackTrace();
}
java 复制代码
        try (//加载,将输入流传递给Excel对象
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\IOTest\\test2.xlsx"));
        XSSFWorkbook wb = new XSSFWorkbook(bis)){
            //获取工作表
            XSSFSheet sheet = wb.getSheetAt(0);

            //获取行
            for(Row row : sheet){
                //通过获取单元格
                for(Cell cell : row){
                    System.out.print(cell.getStringCellValue() + " ");
                }
                System.out.println();
            }


        }catch (IOException e){
            e.printStackTrace();
        }
java 复制代码
try (//加载,将输入流传递给Excel对象
             BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\IOTest\\test2.xlsx"));
             XSSFWorkbook wb = new XSSFWorkbook(bis)) {
            //获取工作表的个数
            int numSheets = wb.getNumberOfSheets();
            System.out.println("Number of Sheets: " + numSheets);
            //获取工作表
            XSSFSheet sheet = wb.getSheet("Sheet1");
            System.out.println(sheet);
            System.out.println("总行数为:"+sheet.getPhysicalNumberOfRows());
            for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
                //通过工作表获取工作行对象
                Row row = sheet.getRow(i);
//                System.out.println("第"+i+"行一共有"+row.getPhysicalNumberOfCells()+"列");
                for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
                    Cell cell = row.getCell(j);
                    //获取单元格的值,单元格的值的类型
                    if(cell.getCellType()== CellType.NUMERIC){
                        System.out.print(cell.getNumericCellValue()+" ");
                    }else if(cell.getCellType()== CellType.STRING){
                        System.out.print(cell.getStringCellValue()+" ");
                    }

                }
                System.out.println();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
java 复制代码
//使用SXSSFWorkbook进行设置的最大行数
        try(
                Workbook wb = new SXSSFWorkbook(100);
        FileOutputStream fos = new FileOutputStream("D:\\IOTest\\big.xlsx");
        ){
            Sheet sheet=wb.createSheet("大文本");
            for(int i=0;i<100000; i++){
                Row row=sheet.createRow(i);
                row.createCell(0).setCellValue(i);
                row.createCell(1).setCellValue(UUID.randomUUID().toString().substring(0,4));
                row.createCell(2).setCellValue(Math.random()*10000);
            }
            wb.write(fos);
        }catch (IOException e) {
            e.printStackTrace();
        }

easyExcel

java 复制代码
    public static void main(String[] args) {
        //写出
//        writeExcel("D:\\IOTest\\easyExcel.xlsx");

        //读入
        readExcel("D:\\IOTest\\easyExcel.xlsx");
    }

    private static void readExcel(String s) {
        try(BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"))) {
            //参数1:读取表格的路径
            //参数2:映射的实体类
            //参数3:读取到数据的操作是什么
            EasyExcel.read(s, Data.class, new AnalysisEventListener<Data>() {
                //读取到此行数据做的操作
                @Override
                public void invoke(Data data, AnalysisContext analysisContext) {

                    try {
                        if(data.getUuid().contains("0")){
                            bw.write(data.getUuid());
                            bw.newLine();
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }

                //读取完数据做的操作,关闭写出资源
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                    try {
                        bw.close();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    System.out.println("读取分析结束");

                }
            }).sheet().doRead();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    private static void writeExcel(String s) {
        //参数1:写出的路径,参数2:写出时参考的实体类
        EasyExcel.write(s, Data.class).
        sheet().   //写到哪个表中
        doWrite(dataReady());    //执行写出操作,数据
    }

    private static List<Data> dataReady() {
        List<Data> list = new ArrayList<>();
        for (int i = 1; i < 100000; i++) {
            Data data = new Data(String.valueOf(i), UUID.randomUUID().toString().substring(0,8), new Date());
            list.add(data);
        }
        return list;
    }
相关推荐
chenchihwen10 小时前
大模型应用班-第3课 从Excel到大屏:AI编程实战全解析 HW3 从零到一:香港疫情数据看板开发实战指南
excel·ai编程
33255_40857_2805916 小时前
使用EasyPOI实现Java订单数据导出(含多物料信息)——模板语法详解与实战
java·excel
开开心心就好19 小时前
PDF转图片工具,一键转换高清无损
服务器·前端·智能手机·r语言·pdf·excel·batch
CodeCraft Studio1 天前
国产化Excel处理组件Spire.XLS教程:使用 Java 将 CSV 转换为 Excel
java·python·excel
宝山哥哥2 天前
python办自动化--利用vba或者python按需求读取excel文件指定列,更改列名后,按照要求将列排序,最后填充空白单元格
python·数据分析·自动化·excel·pandas
修心光2 天前
Excel常用函数大全,非常实用
excel
开开心心就好2 天前
Excel批量加密工具,一键保护多个文件
java·javascript·人工智能·安全·excel·音视频·语音识别
空中湖2 天前
【效率工具】255款工作计划表格Excel电子版模板:总结日月周报日历安排提醒时间管理
程序人生·excel·效率工具
angushine2 天前
Python将Word转换为Excel
python·word·excel