zoukankan      html  css  js  c++  java
  • Java解析excel

    <dependencies>
          <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>3.14</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>3.14</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml-schemas</artifactId>
                <version>3.14</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-scratchpad</artifactId>
                <version>3.14</version>
            </dependency>
      </dependencies>
    package excel;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    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.xssf.usermodel.XSSFWorkbook;
    
    public class TT {
        public static void main(String[] args) {
            try {
                File file=new File("src/main/java/excel/1.xls");
                String name=file.getName();
                boolean is2007=true;
                if(name.matches("^.+\.(?i)(xls)$")){
                    is2007=false;
                }else if (name.matches("^.+\.(?i)(xlsx)$")) {
                    is2007=true;
                }else{
                    throw new Exception("未知的版本");
                }
                InputStream is=new FileInputStream(file);
                Workbook wb=null;
                if(is2007){
                    wb=new XSSFWorkbook(is);
                }else{
                    wb=new HSSFWorkbook(is);
                }
                Sheet sheet=wb.getSheetAt(0);
                //得到行数
                int totalRows=sheet.getPhysicalNumberOfRows();
                List<String> list=new ArrayList<String>();
                //得到列数
                int totalCells=sheet.getRow(0).getPhysicalNumberOfCells();
                for(int i=0;i<totalRows;i++){
                    Row row=sheet.getRow(i);
                    for(int j=0;j<totalCells;j++){
                        Cell cell=row.getCell(j);
                        if(i==0){
                            list.add(cell.getStringCellValue());
                        }else{
                            break;
                        }
                    }
                    break;
                }
                
                
                for(int i=0;i<totalRows;i++){
                    Row row=sheet.getRow(i);
                    System.out.println("============"+(i+1));
                    for(int j=0;j<totalCells;j++){
                        Cell cell=row.getCell(j);
                        String v="";
                        if(cell==null){
                            v="";
                        }else{
                           int t=cell.getCellType();
                            if(XSSFCell.CELL_TYPE_NUMERIC==t){
                                double d=cell.getNumericCellValue();
                                String value=Double.toString(d);
                                int len=value.length();
                                int index=value.indexOf(".");
                                int pointW=4;//保留小数位数
                                pointW++;
                                if(index>0&&(index+pointW)<len){
                                    v=value.substring(0,index+pointW);
                                }else{
                                    v=value;
                                }
                            }else{
                                cell.setCellType(Cell.CELL_TYPE_STRING);
                                v=cell.getStringCellValue();
                            } } System.out.print(list.get(j)
    +":"+v+"|"); } System.out.println(); } } catch (Exception e) { e.printStackTrace(); } } }
  • 相关阅读:
    C语言---堆的实现
    python的matplotlib---雷达图
    python的matplotlib饼状图
    python的matplotlib折线图
    python的matplotlib散点图学习
    python的matplotlib散点图
    C语言---队列(链表实现)
    hadoop集群启动与关闭需要输入密码
    hadoop集群启动时需要输入密码
    C语言---堆栈(链表实现)
  • 原文地址:https://www.cnblogs.com/guoyansi19900907/p/8578956.html
Copyright © 2011-2022 走看看