• POI技术


    1.excel左上角有绿色小图标说明单元格格式不匹配 
    2.模板中设置自动计算没效果,需要加上sheet.setForceFormulaRecalculation(true);

    FileInputStream fs = new FileInputStream(path);  //使用导出模板文件
    POIFSFileSystem ps = new POIFSFileSystem(fs);               //使用POI提供的方法得到excel的信息
    FileOutputStream out = new FileOutputStream(outFileName);   //向outFileName中写数据
    
     HSSFWorkbook wb = new HSSFWorkbook(ps);
     HSSFSheet sheet = wb.getSheetAt(0);            //获取到工作表
     sheet.setForceFormulaRecalculation(true);      //设置自动计算
    
     //隐藏:根据条件设置颜色:但是由于设置颜色后,就无法通过模板里面的公式做出计算,所以去掉
     //字体:绿色、13号大小、水平垂直居中、保留两位小数
     HSSFDataFormat df = wb.createDataFormat(); // 此处设置数据格式
     Font font1 = wb.createFont();
     font1.setColor(HSSFColor.SEA_GREEN.index);
     font1.setFontHeightInPoints((short)13); //字体大小
     font1.setFontName("宋体");
    
     HSSFCellStyle style1 = wb.createCellStyle();
     style1.setFont(font1);
      style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
      style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
      style1.setDataFormat(df.getBuiltinFormat("0.00"));//保留两位小数点



    区域数据导入(Apache POI

    操作office办公文档;

     

    区域:国家划分行政单位。 省市区构成;

     

    1.1 Jquery OCUpload一键上传插件

     

    1、 传统文件上传:页面三要素

    a) 表单提交的方式POST

    b) 表单中enctype=multipart/form-data

    c) 在表单中存在<input  type=file name=test>

    2、 ocUpload使用:

    a) 引入js文件

    <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>

    <script type="text/javascript" src="${pageContext.request.contextPath }/js/ocupload/jquery.ocupload-1.1.2.js"></script>

    b) 在页面提供任意的元素 给出id

    c) 在页面加载完成后调用uplaod方法:动态修改html元素

     -

    1.2 在区域页面使用ocUpload

    1、 页面:/pages/base/area.jsp

     

     

    1.3 在服务端接收上传文件

     

    a) 创建三层接口,类

    1. 通过spring组件扫描创建service,action的对象
    2. 通过spring-data-jpa创建dao的对象

    b) 完成注入

    c) 配置struts2注解

    d) 添加方法

    e) 配置结果视图

    1.4 Apache POI

     

     

    官网:http://poi.apache.org/

     

    导入依赖:

    <dependency>

    <groupId>org.apache.poi</groupId>

    <artifactId>poi</artifactId>

    <version>${poi.version}</version>

    </dependency>

    1.4.1 Demo

     

    HSSF:操作07版本之前 后缀名xxx.xls

    XSSF:操作07版本之后 后缀名xx.xlsx

     

     

     

    需求:解析本地磁盘excel文件中内容:

     

     

    public static void main(String[] args) throws Exception {

    //解析本地磁盘exel文件 后缀名:xls

    //文件目录

    String pathname = "H:\北京Java271_速运快递\速运快递项目-day04\资料\03_区域测试数据\区域导入测试数据.xls";

    //使用POI提供API读取文件

    //:excel文件对象

    //07版本  XSSFWorkbook

    HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(new File(pathname)));

    //解析文件中数据

    //从文件对象中获取标签页

    HSSFSheet sheet = workbook.getSheetAt(0);

    //遍历标签页中行

    for (Row row : sheet) {

    //遍历每一行中单元格中数据

    System.out.println();

    for (Cell cell : row) {

    System.out.print(cell.getStringCellValue()+"  ");

    }

    }

     

    }

     

    1.4.2 在项目中使用POI

    //定义变量接收上传文件

    private File upload;

    //接收文件MIME类型,文件名称

    private String uploadContentType;

    private String uploadFileName;

    public void setUpload(File upload) {

    this.upload = upload;

    }

     

     

    public void setUploadContentType(String uploadContentType) {

    this.uploadContentType = uploadContentType;

    }

     

     

    public void setUploadFileName(String uploadFileName) {

    this.uploadFileName = uploadFileName;

    }

     

     

    /**

      * @Description: 通过POI解析excel文件中数据

     */

    @Action("areaAction_importXls")

    public String importXls() throws Exception {

    List<Area> list = new ArrayList<>();

    //创建excel文件对象

    HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(upload));

    //获取标签页

    HSSFSheet sheet = workbook.getSheetAt(0);

    //遍历标签页获取行

    for (Row row : sheet) {

    //忽略标题行

    if(row.getRowNum()==0){

    continue;

    }

    String id = row.getCell(0).getStringCellValue();

    String province = row.getCell(1).getStringCellValue();

    String city = row.getCell(2).getStringCellValue();

    String district = row.getCell(3).getStringCellValue();

    String postcode = row.getCell(4).getStringCellValue();

    //创建区域对象

    Area area = new Area(id, province, city, district, postcode, null, null);

    list.add(area);

    }

    areaService.save(list);

     

    //释放资源

    workbook.close();

    //由于提交表单到Iframe中,故配置结果视图也看不到

    return NONE;

    }




  • 相关阅读:
    推销员问题
    string类实现
    链表倒数第k个节点
    设计模式之单例模式大全
    空类 sizeof 为什么是1
    类的三种继承方式
    单例模式典型创建方法(三种)
    虚函数实现
    链表删除结点
    TCP的状态转移
  • 原文地址:https://www.cnblogs.com/shan1393/p/9334749.html
Copyright © 2020-2023  润新知