zoukankan      html  css  js  c++  java
  • POI(java 操作excel,word等)编程

    一、下载所需jar包

    下载地址:http://poi.apache.org/download.html  

                  http://download.csdn.net/detail/likai22/534250

    二、上代码

    package com.sxdx.excelpoi.action;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import jxl.Cell;
    import jxl.CellType;
    import jxl.NumberCell;
    import jxl.Sheet;
    import jxl.Workbook;
    import jxl.read.biff.BiffException;
    
    
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    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.ss.usermodel.HorizontalAlignment;
    import org.apache.poi.ss.usermodel.VerticalAlignment;
    import org.apache.poi.ss.util.CellRangeAddress;
    /**
     *  HSSF - 提供读写Microsoft Excel XLS格式档案的功能。
        XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。
        HWPF - 提供读写Microsoft Word DOC97格式档案的功能。
        XWPF - 提供读写Microsoft Word DOC2003格式档案的功能。
        HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
        HDGF - 提供读Microsoft Visio格式档案的功能。
        HPBF - 提供读Microsoft Publisher格式档案的功能。
        HSMF - 提供读Microsoft Outlook格式档案的功能。
     *
     */
    public class PoiAction {
        /**
         * 生成excel
         * @param args
         */
        public static void main(String[] args) {
            
            HSSFWorkbook wb = new HSSFWorkbook();// 创建HSSFWorkbook对象
            HSSFSheet sheet = wb.createSheet("sheet0");// 创建HSSFSheet对象
            
            //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列  
            sheet.addMergedRegion(new CellRangeAddress(0,0,0,10));  
            sheet.setDefaultRowHeightInPoints(20);//设置缺省列高
            sheet.setDefaultColumnWidth(8);//设置缺省列宽  
            //设置指定列的列宽,256 * 50这种写法是因为width参数单位是单个字符的256分之一  
            sheet.setColumnWidth(0, 256 * 30);  
            
            // 设置单元格的横向和纵向对齐方式
            HSSFCellStyle cellStyle = wb.createCellStyle();    
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            
            
            //-----------------------------------------------------------------------
            HSSFRow row0 = sheet.createRow(0);// 创建HSSFRow对象
            row0.setHeight((short) 600);//设置行高
            HSSFCell cell0 = row0.createCell(0);
            cell0.setCellValue("考勤结果表");
            cell0.setCellStyle(cellStyle);
            
            
            HSSFRow row1 = sheet.createRow(1);// 创建HSSFRow对象
            // 创建HSSFCell对象 HSSFCell cell = row.createCell(0)
            // 设置单元格的值
            for(int i=0;i<31;i++){
                HSSFCell cell1 = row1.createCell(i);
                cell1.setCellValue(i+1);
                cell1.setCellStyle(cellStyle);
            }
            HSSFRow row2 = sheet.createRow(2);
            for(int i=0;i<31;i++){
                HSSFCell cell2 = row2.createCell(i);
                cell2.setCellValue("正常");
                cell2.setCellStyle(cellStyle);
            }
            
            HSSFRow row3 = sheet.createRow(3);
            for(int i=0;i<31;i++){
                HSSFCell cell3 = row3.createCell(i);
                cell3.setCellValue("迟到");
                cell3.setCellStyle(cellStyle);
            }
            
            HSSFRow row4 = sheet.createRow(4);
            for(int i=0;i<31;i++){
                HSSFCell cell4 = row4.createCell(i);
                cell4.setCellValue("请假");
                cell4.setCellStyle(cellStyle);
            }
            
            try {
                // 输出Excel文件
                FileOutputStream output = new FileOutputStream("d:\workbook.xls");
                wb.write(output);
                output.flush();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        /**
         * 读取excel
         */
        public static void readExcel(){
            //导入已存在的Excel文件,获得只读的工作薄对象  
            FileInputStream fis = null;
            try {
                fis = new FileInputStream("d:\workbook.xls");
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
            Workbook wk = null;
            try {
                wk = Workbook.getWorkbook(fis);
            } catch (BiffException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
            //获取第一张Sheet表   
            Sheet sheet = (Sheet) wk.getSheet(0);  
            //获取总行数  
            int rowNum = sheet.getRows();
            //从数据行开始迭代每一行  
            for(int i=0;i<rowNum;i++){
                
                System.out.println(sheet.getCell(0, i).getContents());
            }
            try {
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
            wk.close();  
        }
    }

    三、main()方法为生成excel, readExcel()为读取excel。效果图如下

    1、生成文件

    2、excel内容

    3、读取excel

     

  • 相关阅读:
    Yield Usage Understanding
    Deadclock on calling async methond
    How to generate file name according to datetime in bat command
    Run Unit API Testing Which Was Distributed To Multiple Test Agents
    druid的关键参数+数据库连接池运行原理
    修改idea打开新窗口的默认配置
    spring boot -thymeleaf-url
    @pathvariable和@RequestParam的区别
    spring boot -thymeleaf-域对象操作
    spring boot -thymeleaf-遍历list和map
  • 原文地址:https://www.cnblogs.com/Garnett-Boy/p/6909152.html
Copyright © 2011-2022 走看看